“Inbag”表示RandomForest*对象

2024-09-28 20:49:44 发布

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

有没有什么方法可以识别用来构造RandomForest{Classifier, Regressor}对象中每棵树的样本?在

我要找的是保持.inbag“在这个R实现中:http://math.furman.edu/~dcs/courses/math47/R/library/randomForest/html/randomForest.html


Tags: 对象方法httphtmlmath样本educlassifier
1条回答
网友
1楼 · 发布于 2024-09-28 20:49:44

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

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

相关问题 更多 >