【问题标题】:Is there a way, using scikit-learn, to plot the OOB ROC curve for random forest?有没有办法使用 scikit-learn 绘制随机森林的 OOB ROC 曲线?
【发布时间】:2020-04-11 10:17:47
【问题描述】:

我想使用 Python 和 sklearn 绘制随机森林分类器的 out-of-bag (oob) 真阳性和假阳性率的 ROC 曲线。

我知道这在 R 中是可能的,但似乎找不到任何有关如何在 Python 中执行此操作的信息。

【问题讨论】:

    标签: python scikit-learn random-forest


    【解决方案1】:

    您需要.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)
    

    【讨论】:

    • 谢谢!但我想绘制 ROC 曲线。这样的事情有意义吗? 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 样本的预测概率,对吧?
    猜你喜欢
    • 2013-11-27
    • 2012-09-04
    • 2022-01-12
    • 2017-02-20
    • 2015-03-28
    • 2017-12-10
    • 2016-08-24
    • 2016-05-18
    相关资源
    最近更新 更多