我想选择一个能最大限度提高准确性的最佳阈值。使用 for 循环,我找到并附加到列表中的所有精度值,并且可以打印并查看哪个阈值最适合最大精度。但是,我想制作一个代码,该代码将仅输出最大精度及其阈值,或者在变量下分别采用最大精度及其阈值。lm__pred_train = lm_.predict(X_train)def fn_accuracy(actuals, predictions): return np.mean(actuals == predictions)thresholds = np.arange(0, 1, 0.001)accuracy = []for th in thresholds: acc = np.round(fn_accuracy(lm__pred_train > th, y_train), 3) accuracy.append(acc) print(th, acc)我有点新手,到目前为止迷失在 for 循环中,因此非常感谢对我的问题的任何帮助。
1 回答
绝地无双
TA贡献1946条经验 获得超4个赞
lm__pred_train = lm_.predict(X_train)
def fn_accuracy(actuals, predictions):
return np.mean(actuals == predictions)
thresholds = np.arange(0, 1, 0.001)
accuracy = []
max_acc=0
max_th=threshold[0]
for th in thresholds:
acc = np.round(fn_accuracy(lm__pred_train > th, y_train), 3)
accuracy.append(acc)
if acc>max_acc:
max_acc=acc
max_th=th
print(max_th, max_acc)
添加回答
举报
0/150
提交
取消