如何在 Python scikit-learn 中输出随机森林中每棵树的回归预测? [英] How do I output the regression prediction from each tree in a Random Forest in Python scikit-learn?

查看:64
本文介绍了如何在 Python scikit-learn 中输出随机森林中每棵树的回归预测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了组合预测之外,有没有办法从随机森林中的每棵树获得预测?我想输出列表中的所有预测而不是查看整个树.我知道我可以使用 apply 方法获取叶子索引,但我不确定如何使用它从叶子中获取值.

Is there is a way to get the predictions from every tree in a random forest in addition to the combined prediction? I would like to output all of the predictions in a list and not view the entire tree. I know that I can get the leaf indices using the apply method, but I'm not sure how to use that to get the value from the leaf.

这是我到目前为止从下面的评论中得到的.之前我不清楚可以调用 estimators_ 属性中的树,但似乎可以在使用该属性的每棵树上使用 predict 方法.不过,这是最好的方法吗?

Here's what I have so far from comments below. It wasn't clear to me before that the trees in the estimators_ attribute could be called, but it seems that the predict method can be used on each tree using that attribute. Is this the best way to do this, though?

numberTrees = 100
clf = RandomForestRegressor(n_estimators=numberTrees)
clf.fit(X,Y)
for tree in range(numberTrees):
    print(clf.estimators_[tree].predict(val.irow(1)))

推荐答案

我很确定你所拥有的就是你能做到的最好的.正如您所指出的,predict() 返回整个 RF 的预测,但不返回其组件树的预测.它可以返回一个矩阵,但这仅适用于同时学习多个目标的情况.在这种情况下,它为每个目标返回一个预测,它不会为每棵树返回一个预测.您可以使用 predict.all = True 在 R 的随机森林中获得单个树的预测,但 sklearn 没有.如果您尝试使用 apply(),您将获得一个叶子索引矩阵,然后您仍然需要遍历树以找出该树/叶子组合的预测结果.所以我认为你所拥有的已经是最好的了.

I'm pretty sure that what you have up there is about the best you can do. As you noted, predict() returns the prediction for the whole RF, but not for its component trees. It can return a matrix, but that's only for the case where there are multiple targets being learned together. In that case it returns one prediction per target, it doesn't return predictions for each tree. You can get the individual tree predictions in R's random forest using predict.all = True, but sklearn doesn't have that. If you tried using apply(), you'd get a matrix of leaf indices, and then you'd still have to iterate over the trees to find out what the prediction for that tree/leaf combination was. So I think what you have is about as good as it gets.

这篇关于如何在 Python scikit-learn 中输出随机森林中每棵树的回归预测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆