【问题标题】:Controlling the threshold in Logistic Regression in Scikit Learn在 Scikit Learn 中控制逻辑回归的阈值
【发布时间】:2015-04-27 06:47:17
【问题描述】:

我在一个高度不平衡的数据集上使用scikit-learn 中的LogisticRegression() 方法。我什至将class_weight 功能转为auto

我知道在逻辑回归中,应该可以知道特定类对的阈值是多少。

是否有可能知道LogisticRegression() 方法设计的每个 One-vs-All 类中的阈值是多少?

我在文档页面中没有找到任何内容。

不管参数值如何,它是否默认应用0.5 值作为所有类的阈值?

【问题讨论】:

  • 嗯,既然 LR 是一个概率分类器,也就是说,它返回一个类的概率,那么使用 0.5 作为阈值是有意义的。

标签: machine-learning scikit-learn classification logistic-regression


【解决方案1】:

我使用了一个小技巧,而不是使用model.predict(test_data) 使用model.predict_proba(test_data)。然后使用阈值范围来分析对预测的影响;

pred_proba_df = pd.DataFrame(model.predict_proba(x_test))
threshold_list = [0.05,0.1,0.15,0.2,0.25,0.3,0.35,0.4,0.45,0.5,0.55,0.6,0.65,.7,.75,.8,.85,.9,.95,.99]
for i in threshold_list:
    print ('\n******** For i = {} ******'.format(i))
    Y_test_pred = pred_proba_df.applymap(lambda x: 1 if x>i else 0)
    test_accuracy = metrics.accuracy_score(Y_test.as_matrix().reshape(Y_test.as_matrix().size,1),
                                           Y_test_pred.iloc[:,1].as_matrix().reshape(Y_test_pred.iloc[:,1].as_matrix().size,1))
    print('Our testing accuracy is {}'.format(test_accuracy))

    print(confusion_matrix(Y_test.as_matrix().reshape(Y_test.as_matrix().size,1),
                           Y_test_pred.iloc[:,1].as_matrix().reshape(Y_test_pred.iloc[:,1].as_matrix().size,1)))

最好的!

【讨论】:

  • 我喜欢这个答案。我正在努力理解的是如何将其与 GridSearchCV 联系起来?当我运行 GridSearchCV 时,我在众多模型中找到了最好的模型。据推测,逻辑回归的默认阈值 0.5 正在内部使用,那么在进行评分以选择最佳模型时,我将如何覆盖此默认阈值。
【解决方案2】:

逻辑回归选择概率最大的类。在 2 个类别的情况下,阈值为 0.5:如果 P(Y=0) > 0.5,那么显然 P(Y=0) > P(Y=1)。多类设置也是如此:同样,它选择概率最大的类(参见例如Ng's lectures,底线)。

引入特殊阈值仅影响误报/误报的比例(因此影响精度/召回率的权衡),但它不是 LR 模型的参数。另见the similar question

【讨论】:

    【解决方案3】:

    是的,Sci-Kit learn 使用 P>=0.5 的阈值进行二元分类。我将在已经发布的一些答案的基础上使用两个选项来检查:

    一个简单的选择是使用下面代码的 model.predict_proba(test_x) 段的输出以及类预测(下面的代码的 model.predict(test_x) 段的输出)来提取每个分类的概率。然后,将类预测及其概率附加到您的测试数据帧中作为检查。

    作为另一种选择,可以使用以下代码以图形方式查看各种阈值下的精度与召回率。

    ### Predict test_y values and probabilities based on fitted logistic 
    regression model
    
    pred_y=log.predict(test_x) 
    
    probs_y=log.predict_proba(test_x) 
      # probs_y is a 2-D array of probability of being labeled as 0 (first 
      column of 
      array) vs 1 (2nd column in array)
    
    from sklearn.metrics import precision_recall_curve
    precision, recall, thresholds = precision_recall_curve(test_y, probs_y[:, 
    1]) 
       #retrieve probability of being 1(in second column of probs_y)
    pr_auc = metrics.auc(recall, precision)
    
    plt.title("Precision-Recall vs Threshold Chart")
    plt.plot(thresholds, precision[: -1], "b--", label="Precision")
    plt.plot(thresholds, recall[: -1], "r--", label="Recall")
    plt.ylabel("Precision, Recall")
    plt.xlabel("Threshold")
    plt.legend(loc="lower left")
    plt.ylim([0,1])
    

    【讨论】:

    • 在 sklearn 中实例化逻辑回归,确保您有一个划分并标记为 test_x、test_y 的测试和训练数据集,在此数据上运行(拟合)逻辑回归模型,其余的应该从这里开始。
    • 使用sklearn.metrics.plot_precision_recall_curve可以节省一点编码。
    猜你喜欢
    • 2017-03-31
    • 2016-02-21
    • 2018-11-05
    • 2016-12-27
    • 2016-07-31
    • 2017-11-20
    • 2018-03-01
    • 2012-06-27
    相关资源
    最近更新 更多