Scikitlearn随机森林出袋取样

2024-07-03 06:51:26 发布

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

我试图访问随机森林分类器中与每棵树相关联的袋装外样本,但没有运气。我找到了其他信息,比如每个节点的Gini分数和split feature,在那里查找:https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/tree/_tree.pyx

有人知道是否有可能得到与一棵树有关的袋装样品?如果没有,也许可以获取“in-bag”样本(用于特定树的数据集子集),然后使用原始数据集计算OOB?在

提前谢谢


Tags: https信息tree节点分类器森林scikitlearn
1条回答
网友
1楼 · 发布于 2024-07-03 06:51:26

您只需自己从源代码中就可以知道这一点,看看random forest的私有_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)

相关问题 更多 >