【发布时间】:2020-04-11 10:17:47
【问题描述】:
我想使用 Python 和 sklearn 绘制随机森林分类器的 out-of-bag (oob) 真阳性和假阳性率的 ROC 曲线。
我知道这在 R 中是可能的,但似乎找不到任何有关如何在 Python 中执行此操作的信息。
【问题讨论】:
标签: python scikit-learn random-forest
我想使用 Python 和 sklearn 绘制随机森林分类器的 out-of-bag (oob) 真阳性和假阳性率的 ROC 曲线。
我知道这在 R 中是可能的,但似乎找不到任何有关如何在 Python 中执行此操作的信息。
【问题讨论】:
标签: python scikit-learn random-forest
.oob_decision_function_,它返回拟合后袋外样本的预测概率。P.S:这在scikit-learn==0.22中可用
小例子:
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import plot_roc_curve
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
X, y = load_wine(return_X_y=True)
y = y == 2
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
rfc = RandomForestClassifier(n_estimators=10, random_state=42, oob_score=True)
rfc.fit(X_train, y_train)
from sklearn import metrics
pred_train = np.argmax(rfc.oob_decision_function_,axis=1)
metrics.roc_auc_score(y_train, pred_train)
【讨论】:
probas = rfc.oob_decision_function_fpr, tpr, thresholds = metrics.roc_curve(y_train, probas[:, 1])plt.plot(fpr, tpr)
rfc.oob_decision_function_
rfc.oob_decision_function_returns out of bag 样本的预测概率,对吧?