[英]Logistic Regression: how to compare predicted value with a threshold and get the classification done
[英]How to use calculated Threshold Value in Logistic Regression?
我使用 Python 中的以下代码计算了最高准确度的最佳阈值 0.61:
# probability
y_pred_prob = tv_lr.predict_proba(tv_x_test_vector)
# fpr, tpr, threshold
fpr, tpr, threshold = roc_curve(y_test, y_pred_prob[:,1])
# accuracy score for threshold
accuracy_ls = []
for thresh in threshold:
y_pred = np.where(y_pred_prob[:,1]>thresh, 1, 0)
accuracy_ls.append(accuracy_score(y_test, y_pred))
# Dataframe
acc_thr_df = pd.concat([pd.Series(threshold), pd.Series(accuracy_ls)], axis=1, )
acc_thr_df.columns = ['thresh', 'acc']
acc_thr_df.sort_values(by='acc', ascending=False) # Chose the 1st value
当我使用tv_lr.predict(tv_x_test_vector)
它使用 0.5 作为阈值。
请告知如何将阈值更改为0.61? 此处显示的代码是否正确执行此操作而不是使用tv_lr.predict(tv_x_test_vector)
?
y_pred = np.where(y_pred_prob[:,1]>0.61, 1, 0)
LogisticRegression
估计器的predict
方法不允许您将阈值作为参数传递,只允许您使用 0.5 作为阈值。 因此,正如您所说,您必须自己将概率转换为硬预测,以获得阈值的自定义值。
你的代码似乎是正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.