python - 如何在我的决策树中获取所有 Gini 指数?

标签 python machine-learning scikit-learn decision-tree

我在这里使用 sklearn 制作了一个决策树,在 SciKit learn DL 包下,即。 sklearn.tree.DecisionTreeClassifier().fit(x,y)

如何在每个步骤中获取所有可能节点的基尼系数? graphviz 只给我 gini 指数最低的节点的 gini 指数,即用于拆分的节点。

例如,下图(来自 graphviz)告诉我 Pclass_lowVMid 右索引的基尼系数为 0.408,但不是 Pclass_lower 或 Sex_male 在该步骤的基尼系数。我只知道 Pclass_lower 和 Sex_male 的基尼系数必须大于 (0.408*0.7 + 0) 但仅此而已。

decision tree

最佳答案

使用 export_graphviz显示所有节点的不纯度,至少在 0.20.1 版本中是这样。

from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier, export_graphviz
from graphviz import Source

data = load_iris()
X, y = data.data, data.target

clf = DecisionTreeClassifier(max_depth=2, random_state=42)
clf.fit(X, y)

graph = Source(export_graphviz(clf, out_file=None, feature_names=data.feature_names))
graph.format = 'png'
graph.render('dt', view=True);

enter image description here

所有节点的杂质值也可以在impurity属性中访问。

clf.tree_.impurity
array([0.66666667, 0.        , 0.5       , 0.16803841, 0.04253308])

关于python - 如何在我的决策树中获取所有 Gini 指数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53699121/

相关文章:

python - Django 给出 "GET/static/css/style.css HTTP/1.1"304 0

python - 使用 Python/OpenCV 对非浅色/彩色数字进行数字识别

python - 如何忽略 pandas.to_numeric() 中的错误并将 str 更改为 int

python - 在 python 中使用 scikit 包在 SVM 中获取负 alpha 值

python - 如何预测不超过目标示例?

python - 只有一个数字特征的逻辑回归

python - 易于实现内存高效元组列表的结构

numpy - 神经网络不良结果

python - 优达学城 : Assignment 3: ValueError: bad input shape (1000, 10)

python - python中的保留方法