绘制多类ROC曲线



我是SciKit和Python的新手
目前我正在尝试从csv文件生成一个多类(3类(ROC曲线,如下所示:

probability,predclass,dist0,dist1,dist2,actualclass
99.94571208953857,1,0.00022618949060415616,99.94571208953857,0.054055178770795465,1
99.99398589134216,0,99.99398589134216,0.001082851395040052,0.004925658140564337,0
99.97997879981995,1,0.015142260235734284,99.97997879981995,0.004879535117652267,1
93.58544945716858,2,5.507804825901985,0.9067309089004993,93.58544945716858,2
92.31788516044617,1,7.572370767593384,92.31788516044617,0.10974484030157328,1
62.839555740356445,1,2.3740695789456367,62.839555740356445,34.786370396614075,2
... 

我现在的代码是:

df = pd.read_csv('mydata.csv')
pred = (  df.loc[:,['dist0','dist1','dist2']])/100
actual = df['actualclass']
fpr, tpr, _ = roc_curve(actual, pred)

我试着解决这个问题:https://stackoverflow.com/a/45335434/14482749

通过做:

for i in range(n_classes):
fpr[i], tpr[i], _ = roc_curve(actual, pred[:, i]) 
roc_auc[i] = auc(fpr[i], tpr[i])

但收到错误:TypeError:"(slice(None,None,None(,0("是上面第2行的无效键。

我相信问题是我的var"实际",但我不确定是什么

Alrighty我(我的方式(找到了

因此我们需要将dist0..2actualclass输入到roc_curve函数中,如:

for i in range(n_classes):
fpr[i], tpr[i], _ = roc_curve(actual[:,i], pred[:, i])
roc_auc[i] = auc(fpr[i], tpr[i])

所以我们可以为每n个类迭代

对于actual,我做了:

actual2Dict = { 0: [1,0,0] , 1 : [0,1,0]  , 2 : [0,0,1] } 
actual2 = df['actualclass'].map(actual2Dict) 

actualnew = []
for i in actual2:
i = np.array(i)
actualnew.append(i)

actualnew = np.array(actualnew)

对于pred,我做了:

pred = pred.to_numpy()

从那里我开始绘图:

for i in range(n_classes):
fpr[i], tpr[i], _ = roc_curve(actualnew[:,i], pred[:, i])
roc_auc[i] = auc(fpr[i], tpr[i])
lw = 2
# Compute micro-average ROC curve and ROC area
fpr["micro"], tpr["micro"], _ = roc_curve(actualnew.ravel(), pred.ravel())
roc_auc["micro"] = auc(fpr["micro"], tpr["micro"])
# First aggregate all false positive rates
all_fpr = np.unique(np.concatenate([fpr[i] for i in range(n_classes)]))
# Then interpolate all ROC curves at this points
mean_tpr = np.zeros_like(all_fpr)
for i in range(n_classes):
mean_tpr += interp(all_fpr, fpr[i], tpr[i])
# Finally average it and compute AUC
mean_tpr /= n_classes
fpr["macro"] = all_fpr
tpr["macro"] = mean_tpr
roc_auc["macro"] = auc(fpr["macro"], tpr["macro"])

# Plot all ROC curves
plt.figure()
plt.plot(fpr["micro"], tpr["micro"],
label='micro-average ROC curve (area = {0:0.2f})'
''.format(roc_auc["micro"]),
color='deeppink', linestyle=':', linewidth=4)
plt.plot(fpr["macro"], tpr["macro"],
label='macro-average ROC curve (area = {0:0.2f})'
''.format(roc_auc["macro"]),
color='navy', linestyle=':', linewidth=4)
colors = cycle(['aqua', 'darkorange', 'cornflowerblue'])
for i, color in zip(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]))
plt.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 Receiver operating characteristic to multi-class')
plt.legend(loc="lower right")
plt.show()

用于绘制多类别CCD_ 5的代码摘录来自:https://scikit-learn.org/stable/auto_examples/model_selection/plot_roc.html

如果你有不同的方法,请分享!

相关内容

  • 没有找到相关文章

最新更新