【问题标题】:How to display the graphical decision tree for this scikit-learn decision tree script?如何显示此 scikit-learn 决策树脚本的图形决策树?
【发布时间】:2019-12-26 08:35:10
【问题描述】:

我有这个简单的 python scikit-learn 脚本来演示使用决策树算法进行性别分类。

https://github.com/Sarbjyotsingh/Gender-Classification-with-Python

from sklearn import tree

clf = tree.DecisionTreeClassifier()

# [height, weight, shoe_size]
X = [[181, 80, 44], [177, 70, 43], [160, 60, 38], [154, 54, 37], [166, 65, 40],
     [190, 90, 47], [175, 64, 39],
     [177, 70, 40], [159, 55, 37], [171, 75, 42], [181, 85, 43]]

Y = ['male', 'male', 'female', 'female', 'male', 'male', 'female', 'female',
     'female', 'male', 'male']

clf = clf.fit(X, Y)
prediction = clf.predict([[160, 60, 22]])
print(prediction)

脚本运行良好。如何修改它以显示图形树,显示决策树如何解释输入数据以预测输出?

我使用的是 python 3.7,scikit-learn 0.21.3

【问题讨论】:

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


【解决方案1】:
from sklearn import tree

clf = tree.DecisionTreeClassifier()

# [height, weight, shoe_size]
X = [[181, 80, 44], [177, 70, 43], [160, 60, 38], [154, 54, 37], [166, 65, 40],
     [190, 90, 47], [175, 64, 39],
     [177, 70, 40], [159, 55, 37], [171, 75, 42], [181, 85, 43]]

Y = ['male', 'male', 'female', 'female', 'male', 'male', 'female', 'female',
     'female', 'male', 'male']

clf = clf.fit(X, Y)
prediction = clf.predict([[160, 60, 22]])
print(prediction)

import graphviz
dot_data = tree.export_graphviz(clf, out_file=None)
graph = graphviz.Source(dot_data)
graph.render("gender")

最后一行将生成一个 pdf gender.pdf 显示决策树。

【讨论】:

    猜你喜欢
    • 2017-02-23
    • 2020-04-05
    • 2017-03-26
    • 2013-12-12
    相关资源
    最近更新 更多