【问题标题】:Scikit-learn Random Forest out of bag sampleScikit-learn 随机森林 out of bag 样本
【发布时间】:2016-01-21 21:52:09
【问题描述】:

我正在尝试访问与 RandomForestClassifier 中的每棵树相关联的袋外样本,但没有成功。我找到了其他信息,例如每个节点的 Gini 分数和拆分功能,请看:https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/tree/_tree.pyx

有谁知道是否有可能获得与树相关的现成样品?如果不是,也许可以获取“in bag”样本(用于特定树的数据集的子集),然后使用原始数据集计算 OOB?

提前致谢

【问题讨论】:

    标签: python machine-learning scikit-learn random-forest


    【解决方案1】:

    您可以从源代码中自己弄清楚这一点,看看随机森林的私有_set_oob_score方法是如何工作的。 scikit-learn 中的每个树估计器都有自己的伪随机数生成器种子,它存储在 estimator.random_state 字段中。

    在拟合过程中,每个估计器都在训练集子集上学习,训练集子集的索引将使用 PRNG 生成,种子来自estimator.random_state

    这应该可行:

    from sklearn.ensemble.forest import _generate_unsampled_indices
    # X here - training set of examples
    n_samples = X.shape[0]
    for tree in rf.estimators_:
        # Here at each iteration we obtain out of bag samples for every tree.
        unsampled_indices = _generate_unsampled_indices(
        tree.random_state, n_samples)
    

    【讨论】:

    • _generate_unsampled_indices 不在我的 sklearn.ensemble.forest 文件中,但我从源代码复制了它,它现在可以工作了。谢谢!
    • @wootwot,它在哪个源文件中?它适用于我的 Python 3.4 安装。
    • 我使用 python 3.5,我有 scikit-learn 16.1,并且 _generate_sample_indices _generate_unsampled_indices 不在我的源文件中。我从 github 存储库中复制了它们并将它们添加到 forest.py 中,它起作用了。
    猜你喜欢
    • 2015-03-28
    • 2013-04-26
    • 2015-09-16
    • 2014-04-20
    • 2016-06-22
    • 2015-08-28
    • 2012-03-10
    • 2017-03-26
    • 2017-12-10
    相关资源
    最近更新 更多