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

2024-04-23 19:09:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在阅读关于决策树和bagging分类器的文章,我试图展示bagging分类器中使用的第一个决策树。我对输出感到困惑。你知道吗

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))

下面是输出的一个片段

enter image description here

我的理解是value应该显示有多少样本被分类为每个类别。在这种情况下,value字段中的数字不应该和samples字段相加吗?为什么这里不是这样?你知道吗


Tags: fromtestimport决策树treemake分类器train
2条回答

有趣的发现。你知道吗

我仔细研究了一下,发现在导出graphviz对象时,引导开关在proporty=True开关上。由于同一样本有可能多次通过决策树,所以它用百分比表示。如果bootstrapping=False,那么样本只经过一次,因此可以表示为每个类上的样本计数。你知道吗

接得好。你知道吗

额外的引导示例似乎包含在value中,但不包含在samples中;逐字重复代码,但更改为bootstrap=False可以消除差异:

enter image description here

相关问题 更多 >