二分类模型ROC和KS曲线的绘制方法

计算 AUC和KS值

import matplotlib.pyplot as plt
from sklearn.metrics import RocCurveDisplay, roc_curve, roc_auc_score

y_true = [1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0]
y_pre = [0.42, 0.73, 0.55, 0.37, 0.57, 0.70, 0.25, 0.23, 0.46, 0.62, 0.76, 0.46, 0.55, 0.56, 0.56, 0.38, 0.37, 0.73,
         0.77, 0.21]
auc = roc_auc_score(y_true, y_pre)
fpr, tpr, thresholds = roc_curve(y_true, y_pre, pos_label=1)
ks = max(tpr - fpr)

print(f"Auc: {auc:.2f}; Ks: {ks}")

Auc: 0.92; Ks: 0.75

绘制曲线

# 画AUC曲线
plt.plot([0, 1], [0, 1], "k--", label="chance level (AUC = 0.5)")
plt.axis("square")
plt.xlabel("False Positive Rate")
plt.ylabel("True Positive Rate")
plt.title("ROC curves")
plt.legend()
plt.show()

# 画ks曲线
plt.plot(tpr, label='tpr')
plt.plot(fpr, label='fpr')
plt.plot(tpr-fpr, label='diff (KS curves)')
plt.xlabel("Interval")
plt.ylabel("Prob")
plt.title("KS curves")
plt.legend()
plt.show()

在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NLP_wendi

谢谢您的支持。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值