【问题标题】:Getting the distribution of values at the leaf node for a DecisionTreeRegressor in scikit-learn在 scikit-learn 中获取 DecisionTreeRegressor 的叶节点的值分布
【发布时间】:2016-11-12 22:23:04
【问题描述】:
默认情况下,scikit-learn DecisionTreeRegressor 返回给定叶节点中训练集中所有目标值的平均值。
但是,我有兴趣从我的训练集中取回落入预测叶节点的目标值列表。这将使我能够量化分布,并计算标准偏差等其他指标。
这可以使用 scikit-learn 吗?
【问题讨论】:
标签:
python
machine-learning
scikit-learn
random-forest
decision-tree
【解决方案1】:
我认为您正在寻找的是 apply 对象的 apply 方法。 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