如何在Python scikit-learn中输出随机森林每棵树的回归预测结果?

4

我刚接触scikit-learn和随机森林回归,想知道是否有一种简单的方法可以获得随机森林中每个树的预测结果,而不仅仅是组合后的预测。

基本上,我希望能够像在R中使用predict.all = True选项那样。


# Import the model we are using
from sklearn.ensemble import RandomForestRegressor
# Instantiate model with 1000 decision trees
rf = RandomForestRegressor(n_estimators = 1000, random_state = 1337)
# Train the model on training data
rf.fit(train_features, train_labels)
# Use the forest's predict method on the test data
predictions = rf.predict(test_features)
print(len(predictions)) #6565 which is the number of observations my test set has.

我希望获得每棵树的每个预测结果,而不仅仅是每个预测的平均值。

在Python中是否有可能实现?

1个回答

8
使用
import numpy as np
predictions_all = np.array([tree.predict(X) for tree in rf.estimators_])
print(predictions_all.shape) #(1000, 6565) 1000 rows: one for every Tree, 6565 columns, one for every target

这里使用 estimators_ 属性(请参见 文档),该属性是所有经过训练的 DecisionTreeRegressors 的列表。然后我们可以在每个回归器上调用预测方法并将其保存到一个数组中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接