#使用python程序绘制二分类任务的ROC曲线,以及求AUC的值 from sklearn.metrics import roc_curve,auc import matplotlib.pyplot as plt
y_label = ([1, 1, 1, 2, 2, 2]) # 非二进制需要pos_label y_pre = ([0.3, 0.5, 0.9, 0.8, 0.4, 0.6]) #与y_label一一对应 fpr, tpr, thersholds = roc_curve(y_label, y_pre, pos_label=2) #thersholds可以将不同阈值进行降序排列
for i, value in enumerate(thersholds): print("%f %f %f" % (fpr[i], tpr[i], value)) roc_auc = auc(fpr,tpr)
plt.plot(fpr, tpr, 'b^--', label='ROC (area = {0:.2f})'.format(roc_auc), lw=1)
plt.xlim([0.0,1.0])
plt.ylim([0.0, 1.0])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate') # 可以使用中文,但需要导入一些库即字体
plt.title('ROC Curve')
plt.legend(loc=0)
plt.show()