默认情况下,scikit learn DecisionTreeRegressor返回给定叶节点中的训练集中所有目标值的平均值。
但是,我有兴趣从我的训练集中获取落入预测叶节点的目标值列表。这将允许我量化分布,并计算其他指标,如标准差。
使用scikit learn可以吗?

最佳答案

我想你要找的是apply对象的tree方法。See here for the source。下面是一个例子:

import numpy as np
from sklearn.tree import DecisionTreeRegressor

rs = np.random.RandomState(1234)
x  = rs.randn(10,2)
y  = rs.randn(10)

md  = rs.randint(1, 5)
dtr = DecisionTreeRegressor(max_depth=md)
dtr.fit(x, y)

# The `tree_` object's methods seem to complain if you don't use `float32.
leaf_ids = dtr.tree_.apply(x.astype(np.float32))

print leaf_ids
# => [5 6 6 5 2 6 3 6 6 3]

# Should be probably be equal for small depths.
print 2**md, np.unique(leaf_ids).shape[0]
# => 4, 4

08-24 20:35