Python 画二分类的AUC

就是给一组数据,跟据预测的概率和数据对应的真实值,画一个图,直接代码说话

from sklearn import metrics
import matplotlib.pyplot as plt

y_test = []
pred = []
# test_par假装就是我们做好的测试数据dataset
for x1, labels in test_par:
	# 预测值
	predictions = model(x1, training=False)
	# 因为一定要两个单纯的数组,所以在这里变一下
	for i in labels:
	    y_test.append(i)
	for i in predictions:
	    pred.append(i[0])

# 这些才是真正有用的,是使用了sklearn库,蛮方便的
fpr, tpr, threshold = metrics.roc_curve(y_test, pred)
roc_auc = metrics.auc(fpr, tpr)
plt.figure(figsize=(6,6))
plt.title('test ROC')
plt.plot(fpr, tpr, 'b', label = 'Test AUC = %0.3f' % roc_auc)
plt.legend(loc = 'lower right')
plt.plot([0, 1], [0, 1],'r--')
plt.xlim([0, 1])
plt.ylim([0, 1])
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.show()
posted @ 2022-09-27 11:58  赫凯  阅读(54)  评论(0)    收藏  举报