gpt4 book ai didi

python - 控制 Scikit Learn 中逻辑回归的阈值

转载 作者:行者123 更新时间:2023-11-30 08:21:37 24 4
gpt4 key购买 nike

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

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

是否可以知道 LogisticRegression() 方法设计的每个一对一类的阈值是多少?

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

默认情况下,无论参数值如何,它是否都会应用 0.5 值作为所有类的阈值?

最佳答案

我使用了一个小技巧,而不是使用 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)))

最好!

关于python - 控制 Scikit Learn 中逻辑回归的阈值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28716241/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com