gpt4 book ai didi

python - 如何设置scikit学习随机森林模型的阈值

转载 作者:行者123 更新时间:2023-11-28 22:19:24 25 4
gpt4 key购买 nike

看到precision_recall_curve后,如果我想设置threshold = 0.4,如何将0.4实现到我的随机森林模型(二分类)中,对于任何<0.4的概率,将其标记为0,对于> = 0.4的任何概率,将其标记作为 1.

from sklearn.ensemble import RandomForestClassifier
random_forest = RandomForestClassifier(n_estimators=100, oob_score=True, random_state=12)
random_forest.fit(X_train, y_train)
from sklearn.metrics import accuracy_score
predicted = random_forest.predict(X_test)
accuracy = accuracy_score(y_test, predicted)

文档 Precision recall

最佳答案

假设您正在进行二元分类,这很容易:

threshold = 0.4

predicted_proba = random_forest.predict_proba(X_test)
predicted = (predicted_proba [:,1] >= threshold).astype('int')

accuracy = accuracy_score(y_test, predicted)

关于python - 如何设置scikit学习随机森林模型的阈值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49785904/

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