Home > other >  Python with gaussian naive bayes model draw three classification ROC curve error code, hope you help
Python with gaussian naive bayes model draw three classification ROC curve error code, hope you help

Time:03-26

# to introduce the necessary library
The import pandas as pd
The import numpy as np
The import matplotlib. Pyplot as PLT
The from itertools import cycle
The from sklearn. Model_selection import train_test_split
The from sklearn. Naive_bayes import GaussianNB
The from sklearn. Metrics import roc_curve, auc
The from sklearn. Preprocessing import label_binarize
The from sklearn. Multiclass import OneVsRestClassifier
The from scipy import interp

# load data
Df=pd read_excel (' c: \ \ Users \ \ asus \ Desktop \ \ mm XLS ')
X=df. Iloc [: 142, 0:38]
Y=df [' y ']
X=np. Array (X.v alues)
Y=np. Array (y.v alues)
# label binarization
Y=label_binarize (y, classes=[2, 1, 0])
# set type
N_classes=y.s hape [1]

# shuffle and split training and test sets
X_train X_test, y_train, y_test=train_test_split (X, y, test_size=5, random_state=0)

# learn to predict the each class against the other
GNB=GaussianNB ()
GNB. Fit (X_train y_train)
Y_score=GNB. Predict (X_test)


# to calculate each kind of ROC
FPR=dict ()
TPR=dict ()
Roc_auc=dict ()
For I in range (n_classes) :
FPR [I], [I] TPR, _=roc_curve (y_test [, I], y_score [, I])
Roc_auc [I]=auc (FPR [I], [I] TPR)

# compute micro - business, ROC curve and ROC area
FPR [" micro "], [" micro "] TPR, _=roc_curve (y_test. Ravel (), y_score. Ravel ())
Roc_auc [" micro "]=auc (FPR [" micro "], TPR [" micro "])


# the plot all roc curves
Lw=2
PLT. Figure ()
PLT. The plot (FPR [" micro "], TPR, "micro" label='micro - business, ROC curve (area={0-0. 2 f})'
'. The format (roc_auc [" micro "]), color=deeppink ', graphics.linestyle=':' our linewidth=4)

Colors=cycle ([' aqua ', 'darkorange', 'cornflowerblue'])
For I, the color in the zip (range (n_classes), colors) :
PLT. The plot (FPR [I], [I], TPR color=color, lw=lw, label='ROC curve of class {0} (area={1-0. 2 f})'
"'. The format (I, roc_auc [I]))

PLT. The plot ([0, 1], [0, 1], "k", lw=lw)
PLT. Xlim ([0.0, 1.0])
PLT. Ylim ([0.0, 1.05])
PLT. Xlabel (' False Positive Rate ')
PLT. Ylabel (' True Positive Rate ')
PLT. Title (' Some extension of the Receiver operating characteristic to multi - class ')
PLT. Legend (loc="lower right")
plt.show()
  • Related