画ROC和AUC曲线

317 阅读1分钟
import pandas as pd
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt

# 假设你的DataFrame名为df,其中包含两列:'prediction'(模型预测的概率)和'is_fx_csut'(实际的二分类标签)
# df = pd.read_csv('your_data.csv')  # 如果你的数据存储在CSV文件中,可以这样读取数据

# 提取预测概率和实际标签
y_true = df['is_fx_csut']  # 实际的二分类标签
y_scores = df['prediction']  # 模型预测的概率

# 计算ROC曲线的真假阳性率、真假阴性率和阈值
fpr, tpr, thresholds = roc_curve(y_true, y_scores)

# 计算AUC值
roc_auc = auc(fpr, tpr)

# 绘制ROC曲线
plt.figure()
plt.plot(fpr, tpr, color='darkorange', lw=2, label='ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], color='navy', lw=2, 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')
plt.legend(loc="lower right")
plt.show()