为什么这个决策树在每一步的值总和不等于样本数?

Why does this decision tree's values at each step not sum to the number of samples?

我正在阅读有关决策树和装袋分类器的信息,我正在尝试展示装袋分类器中使用的第一个决策树。我对输出感到困惑。

from sklearn.model_selection import train_test_split
from sklearn.datasets import make_moons
from sklearn.ensemble import BaggingClassifier
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import export_graphviz
from graphviz import Source

X, y = make_moons(n_samples=500, noise=0.30, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

bag_clf = BaggingClassifier(
    DecisionTreeClassifier(), 
    n_estimators=500,
    max_samples=100, 
    bootstrap=True, 
    n_jobs=-1)
bag_clf.fit(X_train, y_train)

Source(tree.export_graphviz(bag_clf.estimators_[0], out_file=None))

这是输出中的一个片段

据我了解,value 应该显示每个类别中有多少样本。在那种情况下,value 字段中的数字不应该加起来等于 samples 字段吗?为什么这里不是这种情况?

不错。

额外的 bootstrap 个样本似乎包含在 value 中,但不在总数 samples 中;逐字重复您的代码但更改为 bootstrap=False 消除差异:

分类器和回归器在随机森林中的行为相似 - 分别参见:

  • sklearn RandomForestRegressor discrepancy in the displayed tree values

有趣的发现。

我仔细研究了一下,发现在导出 graphviz 对象时引导程序打开了 proportion = True 开关。由于同一样本可能不止一次通过决策树,因此以百分比表示。如果 bootstrapping = False,样本只经过一次,因此它可以表示为每个 类.

上的样本计数