python - AUC 曲线甚至没有显示在图上
问题描述
在使用逻辑回归进行预测后,这是我得到的混淆矩阵:
True Positives: 3
False Positives: 1309
True Negatives: 12361
False Negatives: 4
roc_auc_score 在这里:
roc_auc_score(y_test, log_preds)
0.6664071480823492
所以我想用这段代码来可视化它:
probas = lg.predict_proba(X_test)[:, 1]
def get_preds(threshold, probabilities):
return [1 if prob > threshold else 0 for prob in probabilities]
roc_values = []
for thresh in np.linspace(0, 1, 100):
preds = get_preds(thresh, probas)
tn, fp, fn, tp = confusion_matrix(y_test, log_preds).ravel()
tpr = tp/(tp+fn)
fpr = fp/(fp+tn)
roc_values.append([tpr, fpr])
tpr_values, fpr_values = zip(*roc_values)
fig, ax = plt.subplots(figsize=(10,7))
ax.plot(fpr_values, tpr_values)
ax.plot(np.linspace(0, 1, 100),
np.linspace(0, 1, 100),
label='baseline',
linestyle='--')
plt.title('Receiver Operating Characteristic Curve', fontsize=18)
plt.ylabel('TPR', fontsize=16)
plt.xlabel('FPR', fontsize=16)
plt.legend(fontsize=12);
下面是输出,它只有一个基线,我不明白。(我的声誉仍然不足以嵌入图像,请随时编辑。谢谢!)
解决方案
好的,现在我有点弄清楚发生了什么。
我写下了这行代码,看看发生了什么:
print(tpr_values)
print(fpr_values)
输出:
(0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855, 0.42857142857142855)
(0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006, 0.09575713240673006)
所有的值都是一样的。所以他们都集中在同一个点上。
我解决了我自己的问题。现在输出正常:
错误在这里:
roc_values = []
for thresh in np.linspace(0, 1, 100):
preds = get_preds(thresh, probas)
tn, fp, fn, tp = confusion_matrix(y_test, log_preds).ravel()
tpr = tp/(tp+fn)
fpr = fp/(fp+tn)
roc_values.append([tpr, fpr])
tpr_values, fpr_values = zip(*roc_values)
在我修复之后log_preds
,它看起来像这样:
roc_values = []
for thresh in np.linspace(0, 1, 100):
preds = get_preds(thresh, probas) #~~~~~~~~
tn, fp, fn, tp = confusion_matrix(y_test, preds).ravel()
tpr = tp/(tp+fn) #^^^^^^^^
fpr = fp/(fp+tn)
roc_values.append([tpr, fpr])
tpr_values, fpr_values = zip(*roc_values)
这很令人沮丧,但无论如何,它终于奏效了。
推荐阅读
- angular - 如何处理 NGRX 控制流
- reactjs - 在 useEffect 内模拟 React 设置 2 个状态
- amazon-s3 - 在 S3 中的 Presto 中查询 JSON 文件
- css - 有没有办法在 iframe 中使用我网站上的字体
- excel - 有没有办法使用 SUMIF 计算比率?
- c++ - 在 Visual Studio 编译器中将多个文件编译为一个可执行文件 - C++
- proxy - 如何使用 artifactory 作为代理来获取包?
- python - Discord.py - 经济系统机器人中的存款命令无法正常工作
- php - 在 PHP 中从 Object 中提取数据
- python - 使用 Python 对 Excel 中的数据执行内部连接