我正在尝试理解predict
Python statsmodels 中用于 Logit 模型的函数。它的文档是here https://www.statsmodels.org/stable/generated/statsmodels.discrete.discrete_model.LogitResults.predict.html.
当我构建 Logit 模型并使用predict
,它返回从 0 到 1 的值,而不是 0 或 1。现在我读到这句话说这些是概率,我们需要一个阈值。Python statsmodel.api 逻辑回归 (Logit) https://stackoverflow.com/questions/26528019/python-statsmodel-api-logistic-regression-logit
现在,我想生成 AUC 数字,我使用roc_auc_score
来自 sklearn (docs https://scikit-learn.org/stable/modules/generated/sklearn.metrics.roc_auc_score.html).
这是我开始感到困惑的时候。
- 当我将 Logit 模型中的原始预测值(概率)放入
roc_auc_score
作为第二个参数y_score
,我得到的合理 AUC 值约为 80%。如何roc_auc_score
函数知道我的哪个概率等于 1,哪个概率等于 0?我没有机会设定门槛。
- 当我使用 0.5 的阈值手动将概率转换为 0 或 1 时,我得到的 AUC 约为 50%。为什么会出现这种情况呢?
这是一些代码:
m1_result = m1.fit(disp = False)
roc_auc_score(y, m1_result.predict(X1))
AUC: 0.80
roc_auc_score(y, [1 if X >=0.5 else 0 for X in m1_result.predict(X1)])
AUC: 0.50
为什么会这样呢?
你计算 AUC 的第二种方法是错误的;根据定义,AUC 需要概率,而不是阈值处理后生成的硬类预测 0/1,如您在此处所做的那样。所以,你的 AUC 是 0.80。
您在AUC计算中不自行设定阈值;粗略地说,正如我所解释的别处 https://stackoverflow.com/questions/47104129/getting-a-low-roc-auc-score-but-a-high-accuracy/47111246#47111246,AUC 衡量二元分类器的性能对所有可能的决策阈值进行平均.
如果在这里再次解释 AUC 计算的基本原理和细节就显得有些过分了;相反,这些其他 SE 线程(以及其中的链接)将帮助您了解这个想法:
- 在分类中,测试准确率和 AUC 分数有什么区别? https://stackoverflow.com/questions/60905517/in-classification-what-is-the-difference-between-the-test-accuracy-and-the-auc
- AUC 与标准准确度的优点 https://datascience.stackexchange.com/questions/806/advantages-of-auc-vs-standard-accuracy
- ROC AUC 分数较低但准确率较高 https://stackoverflow.com/questions/47104129/getting-a-low-roc-auc-score-but-a-high-accuracy
- 比较模型之间的 AUC、对数损失和准确度分数 https://stackoverflow.com/questions/58610117/comparing-auc-log-loss-and-accuracy-scores-between-models
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)