roc曲线绘制

“””
import numpy as np
from scipy import interp
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 StratifiedKFold
# #############################################################################
# Data IO and generation
# Import some data to play with
iris = datasets.load_iris()
x = iris.data
y = iris.target
x, y = x[y != 2], y[y != 2]
n_samples, n_features = x.shape
# Add noisy features
random_state = np.random.RandomState(0)#用0表示每次产生的随机数相同
X = np.c_[x, random_state.randn(n_samples, 200 * n_features)]#两个矩阵相链接,行数不变,列数相加;
#randn产生正态分布分布的数值
# #############################################################################
## Classification and ROC analysis
#
## Run classifier with cross-validation and plot ROC curves
cv = StratifiedKFold(n_splits=6)
classifier = svm.SVC(kernel=’linear’, probability=True,
random_state=random_state)
tprs = []
aucs = []
mean_fpr = np.linspace(0, 1, 100)
plt.figure()
i = 0
for train, test in cv.split(X, y):
print(‘test:’,test.shape)
probas_ = classifier.fit(X[train], y[train]).predict_proba(X[test])
# Compute ROC curve and area the curve
fpr, tpr, thresholds = roc_curve(y[test], probas_[:, 1])
# print(thresholds.shape)
# print(thresholds)
# print(‘1:’,tpr)
tprs.append(interp(mean_fpr, fpr, tpr))#插值运算,6*100
# print(‘2:’,len(tpr))
# print(‘3:’,len(tprs[0]))#100
tprs[-1][0] = 0.0#保证每一条的第一个位置都是0
roc_auc = auc(fpr, tpr)#auc值,是一个值
aucs.append(roc_auc)#aucs:6*1
plt.plot(fpr, tpr, lw=1, alpha=0.3,
label=’ROC fold %d (AUC = %0.2f)’ % (i, roc_auc))
i += 1
plt.plot([0, 1], [0, 1], linestyle=’–’, lw=2, color=’r’,
label=’Luck’, alpha=.8)
mean_tpr = np.mean(tprs, axis=0)#axis=0,求每列的平均值,所以返回一个1*100的数组
mean_tpr[-1] = 1.0
mean_auc = auc(mean_fpr, mean_tpr)
std_auc = np.std(aucs)
plt.plot(mean_fpr, mean_tpr, color=’b’,
label=r’Mean ROC (AUC = %0.2f ± %0.2f)’ % (mean_auc, std_auc),
lw=2, alpha=.8)
std_tpr = np.std(tprs, axis=0)
tprs_upper = np.minimum(mean_tpr + std_tpr, 1)
tprs_lower = np.maximum(mean_tpr - std_tpr, 0)
plt.fill_between(mean_fpr, tprs_lower, tprs_upper, color=’grey’, alpha=.2,
label=r’ ± 1 std. dev.’)#填充区域颜色
#
#plt.xlim([-0.05, 1.05])
#plt.ylim([-0.05, 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()

猜你喜欢

转载自blog.csdn.net/sinat_26492471/article/details/81159678