H2O分布式随机森林所有树的预测

3
我使用Python的H2O(版本3.22.1.3),我想知道是否可以观察每棵树在随机森林中的预测结果,就像我们在scikit-learn的RandomForestRegressor.estimators_方法中所做的那样。我尝试使用h2o.predict_leaf_node_assignment(),但它只提供了每棵树的预测路径或者(据说)基于哪个叶子节点进行预测的id。在最新版本中,H2O添加了Tree类,但不幸的是,它没有任何predict()方法。虽然我可以访问随机森林中任何一棵树中的任何节点,但是使用树的最近实现的API来实现树的预测函数(即使正确),速度非常慢。因此,我的问题是:
(a) 我能否本地获取树的预测结果,如果可以,应该如何操作?
(b) 如果不能,H2O开发人员是否计划在未来的版本中实现此功能?
非常感谢您的回答。
更新:感谢Joe的回复。目前(在直接实现该功能之前),这是我能想到的唯一解决方法,可以生成树的预测结果。
# Suppose we have random forest model called drf with ntrees=70 and want to make predictions on df_valid
# After executing the code below, we get a dataframe tree_predictions with ntrees (in our case 70) columns, where i-th column corresponds to the predictions of i-th tree, and the same number of rows as df_valid.
# Extract the trees to create prediction intervals
# Number of trees
ntrees = 70

from h2o.tree import H2OTree
# Extract all the tree of drf, create the list of prediction trees
list_of_trees = [H2OTree(model = drf, tree_number = t, tree_class = None) for t in range(ntrees)]

# leaf_nodes contains the node_id's of tree leaves with predictions
leaf_nodes = drf.predict_leaf_node_assignment(df_valid, type='Node_ID').as_data_frame()

# tree_predictions is the dataframe with predictions for all the 70 trees
tree_predictions = pd.DataFrame(columns=['T'+str(t+1) for t in range(ntrees)])
for t in range(ntrees):
    tr = list_of_trees[t]
    node_ids = np.array(tr.node_ids)
    treePred = lambda n: tr.predictions[np.where(node_ids==n)[0][0]] 
    tree_predictions['T'+str(t+1)] = leaf_nodes['T'+str(t+1)].apply(treePred)enter code here
1个回答

0

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