我试图在RandomForestClassifier中访问与每棵树相关联的袋子样本而没有运气。我找到了其他信息,例如Gini得分和每个节点的拆分功能,看着那里:https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/tree/_tree.pyx
有没有人知道是否有可能获得与树相关的袋样品?如果没有,也许有可能获得'in bag'样本(用于特定树的数据集的子集),然后使用原始数据集计算OOB?
提前致谢
答案 0 :(得分:4)
您可以从源代码中自己解决这个问题,看看随机林的私有_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)