放置在树根中的变量的信息增益是多少?

问题描述 投票:0回答:2

我正在尝试从 Stepic 解决这个问题:

下载包含三个变量的数据集:sex、exang、num。想象一下 我们想使用决策树来分类患者是否 患有心脏病(可变数字)基于两个标准:性别和 存在/不存在心绞痛 (exang)。训练决策树 在此数据上,使用熵作为标准。指定信息内容 增益值将用于放置在根中的变量 那个树。答案必须是精确到小数点后 3 位的数字。

这就是我所做的:

clf = tree.DecisionTreeClassifier()
clf.fit(X, y)
tree.plot_tree(clf, filled=True)

l_node = clf.tree_.children_left[0]
r_node = clf.tree_.children_left[1]
n1 = clf.tree_.n_node_samples[l_node]
n2 = clf.tree_.n_node_samples[r_node]
e1 = clf.tree_.impurity[l_node]
e2 = clf.tree_.impurity[r_node]
n = n1 + n2

ig = 0.996 - (n1 * e1 + n2 * e2) / n

信息增益为0.607。但当我输入信息增益时,答案不正确。 我做错了什么?

python pandas scikit-learn
2个回答
0
投票

您没有使用所需的标准创建决策树:

entropy
。如果您没有指定任何内容,算法将使用
gini
标准作为默认值(正如您在图中看到的那样)

代码应该是:

clf = tree.DecisionTreeClassifier(criterion="entropy")
clf.fit(X, y)

有时,改变标准可以改变你的树(在这种情况下我怀疑),但你将能够看到每个分割中的熵,这对你的任务非常有帮助。


0
投票

右分支的子节点数量可能有错误。 尝试弄清楚 r_node = clf.tree_.children_left[1] r_node = clf.tree_.children_right[0] 和 e0 = Decision_tree.tree_.impurity[0]

© www.soinside.com 2019 - 2024. All rights reserved.