H2O DistributedRandomForest 所有树预测

H2O DistributedRandomForest all tree predictions

我使用 Python 的 H2O(版本 3.22.1.3),我想知道是否可以观察随机森林中每棵树的预测,就像我们在 scikit-learn 的情况下所做的那样RandomForestRegressor.estimators_ 方法。我尝试使用 h2o.predict_leaf_node_assignment(),但它会带来每棵树的预测路径或(据推测)做出预测所依据的叶节点的 ID。在上一个版本中,H2O 添加了 Tree class,但不幸的是,它没有任何 predict() 方法。尽管我可以访问任何随机森林树中的任何节点,但我使用树最近实现的 API(即使正确)实现树预测功能仍然非常慢。所以,我的问题是:

(a) 我可以在本地获得树预测吗?如果可以,那么如何?

(b) 如果没有,H2O 开发人员是否计划在未来的版本中实现此功能?

如有任何回复,我们将不胜感激。

更新:乔,谢谢你的回复。至于现在(在直接实现该功能之前),这是我能想到的生成树预测的唯一解决方法。

# 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

目前答案是否定的。我们创建了一个用于在树 API 中实现新功能的问题。您可以在此处跟踪进度:https://0xdata.atlassian.net/browse/PUBDEV-6322.