【问题标题】:Logistic Regression in python. probability thresholdpython中的逻辑回归。概率阈值
【发布时间】:2017-11-20 10:00:21
【问题描述】:

因此,我正在使用逻辑回归算法解决分类问题,并且获得了“1”类测试集的所有预测。该集合非常不平衡,因为它有超过 200k 的输入,或多或少 92% 来自“1”类。如果 P(Y=1|X)>0.5,逻辑回归通常将输入分类为“1”类。因此,由于测试集中的所有观察结果都被分类为第 1 类,我认为也许有一种方法可以更改此阈值并将其设置为例如 0.75,以便只有 P(Y=1|X)>0.75 的观察结果是分类为 1 类,否则为 0 类。如何在 python 中实现?

model= LogisticRegression(penalty='l2', C=1) 
model.fit(X_train, y_train)
score=accuracy_score(y_test, model2.predict(X_test))
fpr, tpr, thresholds = roc_curve(y_test, model2.predict_proba(X_test)[:,1])
roc=roc_auc_score(y_test, model2.predict_proba(X_test)[:,1])
cr=classification_report(y_test, model2.predict(X_test))

PS。由于测试集的所有观察值都被分类为 1 类,因此分类报告中的 F1 分数和召回率为 0。也许通过更改阈值可以解决此问题。

【问题讨论】:

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


    【解决方案1】:

    您可能想尝试的是平衡类而不是更改阈值。 Scikit-learn 通过class_weights 支持这一点。例如,您可以尝试model = LogisticRegression(penalty='l2', class_weight='balanced', C=1)。查看文档了解更多详情:

    http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html

    【讨论】:

    • 谢谢。有效。你知道如何在 MLP 分类器中处理同样的问题吗?在这种情况下,f1 分数和召回率也是 0。
    • 如果您为此使用 keras,您可以在这里找到答案:datascience.stackexchange.com/questions/13490/…。它的工作原理类似。 Scikit-learn 的 MLPClassifier 不支持这一点。或者,您可以对最小的类进行过采样或对最大的类进行欠采样。
    猜你喜欢
    • 2013-12-24
    • 2015-05-04
    • 1970-01-01
    • 2019-04-04
    • 2013-06-05
    • 1970-01-01
    • 2019-04-06
    • 1970-01-01
    • 2016-02-24
    相关资源
    最近更新 更多