本文介绍了如何为 scikit 学习随机森林模型设置阈值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
看到precision_recall_curve后,如果我想设置threshold = 0.4,如何将0.4实现到我的随机森林模型中(二分类),对于任何概率=0.4,标记将其设为 1.
After seeing the precision_recall_curve, if I want to set threshold = 0.4, how to implement 0.4 into my random forest model (binary classification), for any probability <0.4, label it as 0, for any >=0.4, label it as 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)
文档精确召回
推荐答案
假设你在做二元分类,这很容易:
Assuming you are doing binary classification, it's quite easy:
threshold = 0.4
predicted_proba = random_forest.predict_proba(X_test)
predicted = (predicted_proba [:,1] >= threshold).astype('int')
accuracy = accuracy_score(y_test, predicted)
这篇关于如何为 scikit 学习随机森林模型设置阈值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!