计算 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()