"在袋"对于RandomForest *对象

时间:2016-03-06 21:24:54

标签: python scikit-learn

是否有某种方法可以识别用于构建RandomForest{Classifier, Regressor}对象中每棵树的样本?

我正在寻找相当于" keep.inbag"在此R实现中:http://math.furman.edu/~dcs/courses/math47/R/library/randomForest/html/randomForest.html

1 个答案:

答案 0 :(得分:0)

回答我自己的问题(在@amueller的scikit-learn邮件列表的帮助下 - 谢谢!),这是一个计算袋内矩阵的函数:

from sklearn.ensemble.forest import _generate_sample_indices
def calc_inbag(n_samples, forest):
        n_trees = forest.n_estimators
        inbag = np.zeros((n_samples, n_trees))
        for t_idx in range(n_trees):
            sample_idx = _generate_sample_indices(forest.estimators_[t_idx].random_state, 
                                                  n_samples)
            inbag[:, t_idx] = np.bincount(sample_idx, minlength=n_samples)
        return inbag