如何在scikit-learn中提取决策树的节点

时间:2015-09-21 10:01:58

标签: scikit-learn

我想提取决策树的详细信息,如门槛,基尼...... 当我使用谷歌搜索时,我可以找到一些有关显示的信息,但几乎没有关于节点信息的信息。 同性恋者你知道吗?

1 个答案:

答案 0 :(得分:1)

您可以以dot格式导出决策树,并随意执行任何操作,没有人强迫您将其可视化: Link with source code from official documentation

from sklearn.datasets import load_iris
from sklearn import tree
iris = load_iris()
clf = tree.DecisionTreeClassifier()
clf = clf.fit(iris.data, iris.target)

with open("iris.dot", 'w') as f:
    f = tree.export_graphviz(clf, out_file=f)

iris.dot文件现在包含:

digraph Tree {
node [shape=box] ;
0 [label="X[3] <= 0.8\ngini = 0.6667\nsamples = 150\nvalue = [50, 50, 50]"] ;
1 [label="gini = 0.0\nsamples = 50\nvalue = [50, 0, 0]"] ;
0 -> 1 [labeldistance=2.5, labelangle=45, headlabel="True"] ;
2 [label="X[3] <= 1.75\ngini = 0.5\nsamples = 100\nvalue = [0, 50, 50]"] ;
0 -> 2 [labeldistance=2.5, labelangle=-45, headlabel="False"] ;
3 [label="X[2] <= 4.95\ngini = 0.168\nsamples = 54\nvalue = [0, 49, 5]"] ;
2 -> 3 ;
....
}