有没有一种方法,使用scikitlearn,为随机森林绘制OOB ROC曲线?

2024-10-05 13:08:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用Python和sklearn绘制随机森林分类器的出袋(oob)真阳性率和假阳性率的ROC曲线。你知道吗

我知道这在R中是可能的,但似乎找不到任何关于如何在Python中实现这一点的信息。你知道吗


Tags: 信息分类器森林绘制sklearn曲线rocoob
1条回答
网友
1楼 · 发布于 2024-10-05 13:08:25

您需要.oob_decision_function_,它在拟合后返回袋外样本的预测概率

附言:这可以在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)

相关问题 更多 >

    热门问题