【问题标题】:Sklearn logistic regression - adjust cutoff pointSklearn逻辑回归 - 调整截止点
【发布时间】:2019-05-19 16:36:57
【问题描述】:

我有一个逻辑回归模型试图预测以下两个类别之一:A 或 B。

  • 我的模型在预测 A 时的准确度约为 85%。
  • 模型在预测 B 时的准确度约为 50%。
  • B 的预测并不重要,但 A 的预测非常重要。

我的目标是在预测 A 时最大限度地提高准确率。有没有办法在确定类别时调整默认决策阈值?

classifier = LogisticRegression(penalty = 'l2',solver = 'saga', multi_class = 'ovr')
classifier.fit(np.float64(X_train), np.float64(y_train))

谢谢! RB

【问题讨论】:

  • 引入特殊阈值只影响误报/误报的比例,从而影响精度/召回率的权衡,但这不是LR模型的参数。

标签: python-3.x scikit-learn logistic-regression


【解决方案1】:

如 cmets 中所述,选择阈值的过程是在训练后完成的。您可以找到最大化您选择的效用函数的阈值,例如:

from sklearn import metrics
preds = classifier.predict_proba(test_data)
tpr, tpr, thresholds = metrics.roc_curve(test_y,preds[:,1])
print (thresholds)

accuracy_ls = []
for thres in thresholds:
    y_pred = np.where(preds[:,1]>thres,1,0)
    # Apply desired utility function to y_preds, for example accuracy.
    accuracy_ls.append(metrics.accuracy_score(test_y, y_pred, normalize=True))

然后,选择最大化所选效用函数的阈值。在您的情况下,选择在y_pred 中最大化1 的阈值。

【讨论】:

    猜你喜欢
    • 2015-05-04
    • 2016-03-06
    • 2014-01-20
    • 2021-03-11
    • 2013-12-24
    • 2019-07-26
    • 1970-01-01
    • 2014-01-19
    • 2020-07-04
    相关资源
    最近更新 更多