您可以从scikit-learn中的DecisionTreeRegressor中获取选定的叶子吗 [英] Can you get the selected leaf from a DecisionTreeRegressor in scikit-learn

查看:140
本文介绍了您可以从scikit-learn中的DecisionTreeRegressor中获取选定的叶子吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只需阅读好论文并尝试实施此方法:

just reading this great paper and trying to implement this:

...我们对待每个人 树作为一种分类特征,以 实例最终落入的叶子的索引.我们使用1- 这种类型的特征的of-K编码.例如,考虑 图1中带有2个子树的增强树模型,其中 第一个子树有3个叶子,第二个子树有2个叶子.如果 实例最终出现在第一个子树的第2个叶子中,而实例1中的第1个叶子结束 第二个子树,线性分类器的整体输入将 是二进制向量[0,1,0,1,0],其中前3个条目 对应于第一个子树的叶子,最后2个到 第二个子树的那些...

... We treat each individual tree as a categorical feature that takes as value the index of the leaf an instance ends up falling in. We use 1- of-K coding of this type of features. For example, consider the boosted tree model in Figure 1 with 2 subtrees, where the first subtree has 3 leafs and the second 2 leafs. If an instance ends up in leaf 2 in the first subtree and leaf 1 in second subtree, the overall input to the linear classifier will be the binary vector [0, 1, 0, 1, 0], where the first 3 entries correspond to the leaves of the first subtree and last 2 to those of the second subtree ...

任何人都知道我如何预测一排行,对于这些行中的每一行,如何获得整体中每棵树的选定叶子?对于这个用例,我并不在乎节点代表什么,只是在乎它的索引.看了一下源,我无法很快看到任何明显的东西.我可以看到我需要遍历树木并执行以下操作:

Anyone know how I can predict a bunch of rows and for each of those rows get the selected leaf for each tree in the ensemble? For this use case I don't really care what the node represents, just its index really. Had a look at the source and I could not quickly see anything obvious. I can see that I need to iterate the trees and do something like this:

for sample in X_test:
  for tree in gbc.estimators_:
    leaf = tree.leaf_index(sample) # This is the function I need but don't think exists.
    ...

任何指针都表示赞赏.

推荐答案

apply ,似乎可以找到相应的叶子ID:

DecisionTreeRegressor has tree_ property which gives you access to the underlying decision tree. It has method apply, which seemingly finds corresponding leaf id:

dt.tree_.apply(X)

请注意,apply希望其输入的类型为float32.

Note that apply expects its input to have type float32.

这篇关于您可以从scikit-learn中的DecisionTreeRegressor中获取选定的叶子吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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