Lecture03_roc_curve
Copyright By PowCoder代写 加微信 powcoder
import numpy as np
import matplotlib.pyplot as plt
from itertools import cycle
from sklearn import svm, datasets
from sklearn.metrics import roc_curve, auc
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import label_binarize
from sklearn.multiclass import OneVsRestClassifier
from scipy import interp
from sklearn.metrics import roc_auc_score
# Import some data to play with
iris = datasets.load_iris()
X = iris.data
y = iris.target
# Binarize the output
y = label_binarize(y, classes=[0, 1, 2])
n_classes = y.shape[1]
# Add noisy features to make the problem harder
random_state = np.random.RandomState(0)
n_samples, n_features = X.shape
X = np.c_[X, random_state.randn(n_samples, 200 * n_features)]
# 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 each class against the other
classifier = OneVsRestClassifier(svm.SVC(kernel=’linear’, probability=True,
random_state=random_state))
y_score = classifier.fit(X_train, y_train).decision_function(X_test)
print(y_score.shape)
print(y_score[:5,:])
[[-0.76301132 -0.36482547 0.12386354]
[-0.20224493 -0.63144366 -0.16612302]
[ 0.11801481 -0.80263073 -0.32055874]
[-0.90780855 -0.12395478 0.02199789]
[-0.01116192 -0.27913475 -0.71889214]]
# Compute ROC curve and ROC area for each class
fpr = dict()
tpr = dict()
roc_auc = dict()
print(‘total number of classes:’,n_classes)
for i in range(n_classes):
fpr[i], tpr[i], _ = roc_curve(y_test[:, i], y_score[:, i])
roc_auc[i] = auc(fpr[i], tpr[i])
# Compute micro-average ROC curve and ROC area
fpr[“micro”], tpr[“micro”], _ = roc_curve(y_test.ravel(), y_score.ravel())
roc_auc[“micro”] = auc(fpr[“micro”], tpr[“micro”])
plt.figure()
plt.plot(fpr[k], tpr[k], color=’darkorange’,
lw=lw, label=’ROC curve (area = %0.2f)’ % roc_auc[k])
plt.plot([0, 1], [0, 1], color=’navy’, lw=lw, linestyle=’–‘)
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel(‘False Positive Rate’)
plt.ylabel(‘True Positive Rate’)
plt.title(‘Receiver operating characteristic example’)
plt.legend(loc=”lower right”)
plt.show()
total number of classes: 3
#ROC curves for class 0
plt.figure()
plt.plot(fpr[k], tpr[k], color=’darkorange’,
lw=lw, label=’ROC curve (area = %0.2f)’ % roc_auc[k])
plt.plot([0, 1], [0, 1], color=’navy’, lw=lw, linestyle=’–‘)
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel(‘False Positive Rate’)
plt.ylabel(‘True Positive Rate’)
plt.title(‘Receiver operating characteristic example’)
plt.legend(loc=”lower right”)
plt.show()
#ROC curves for class 1
plt.figure()
plt.plot(fpr[k], tpr[k], color=’darkorange’,
lw=lw, label=’ROC curve (area = %0.2f)’ % roc_auc[k])
plt.plot([0, 1], [0, 1], color=’navy’, lw=lw, linestyle=’–‘)
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel(‘False Positive Rate’)
plt.ylabel(‘True Positive Rate’)
plt.title(‘Receiver operating characteristic example’)
plt.legend(loc=”lower right”)
plt.show()
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com