# -*- coding: utf-8 -*- """ ------------------------------------------------- File Name: roc-multi on daily-test Description : Author : wayne Date: 18-10-11 Create by : PyCharm Check status: https://waynehfut.github.io ------------------------------------------------- """ 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
# 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_test) print(y_score) # 读取各个子类的fpr和tpr,其计算时数据格式为y_test=[[1,0,0],[0,0,1],...,[0,1,0]],y_score=[[score1,score2,score3],...,[scoren1,scoren2,scoren3]] fpr = dict() tpr = dict() roc_auc = dict() for i inrange(n_classes): fpr[i], tpr[i], _ = roc_curve(y_test[:, i], y_score[:, i]) roc_auc[i] = auc(fpr[i], tpr[i]) lw = 2#线条粗细 colors = cycle(['aqua', 'darkorange', 'cornflowerblue']) for i, color inzip(range(n_classes), colors): plt.plot(fpr[i], tpr[i], color=color, lw=lw, label='ROC curve of class {0} (area = {1:0.2f})' ''.format(i, roc_auc[i]))