Scikit-learn随机森林的袋外样本

7
我正在尝试访问RandomForestClassifier中每个树木相关的袋外样本,但没有成功。我找到了其他信息,如每个节点的Gini分数和拆分特征,在此处查看:https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/tree/_tree.pyx
有人知道是否可以获取与树相关的袋外示例吗?如果不行,也许可以获取“袋内”示例(用于特定树的数据集子集),然后使用原始数据集计算OOB?
提前致谢。
1个回答

9
您可以从源代码中自行查看此内容,看一下随机森林的私有方法_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 文件中,但是我从源代码中复制了它,现在可以工作了。谢谢! - wootwoot
@wootwoot,它在哪个源文件中?它可以在我的Python 3.4安装上运行。 - Ibraim Ganiev
1
我使用Python 3.5,拥有scikit-learn 16.1版本,但我的源文件中没有_generate_sample_indices和_generate_unsampled_indices。我从GitHub仓库复制了它们,并将其添加到forest.py中,这样就可以正常工作了。 - wootwoot

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接