如何为 scikit 学习随机森林模型设置阈值

Big*_*ata 9 python scikit-learn

看到precision_recall_curve后,如果我想设置阈值=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)
Run Code Online (Sandbox Code Playgroud)

文档精确召回

Ste*_*tev 24

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

threshold = 0.4

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

accuracy = accuracy_score(y_test, predicted)
Run Code Online (Sandbox Code Playgroud)