ROC曲线模板 绘图&求解

1,068 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。


import pandas as pd

import matplotlib.pyplot as plt
from sklearn.metrics import roc_curve, auc  ###计算roc和auc

inputfile = "predict.csv"

# data = np.loadtxt(open(inputfile), delimiter=',')
pre = pd.read_csv(inputfile, sep=',', header='infer')
pre = pre.values[0::, 0::]
y_score = pre[:, -1]
# print(pre)
# print(y_score)

data = pd.read_csv("DM.csv", sep=',', header='infer')
data = data.values[0::, 0::]
# print(data)
y_test = data[:, -1]
# print(y_test)

fpr, tpr, threshold = roc_curve(y_test, y_score)
roc_auc = auc(fpr, tpr)

plt.figure()
lw = 2
plt.figure(figsize=(10, 10))
plt.plot(fpr, tpr, color='darkorange',
         lw=lw, label='ROC curve (area = %0.2f)' % roc_auc)  ###假正率为横坐标,真正率为纵坐标做曲线
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 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()

在这里插入图片描述

记录在医学实验中使用到的