我有一个简单的Python scikit-learn脚本,用于展示使用决策树算法进行性别分类。
https://github.com/Sarbjyotsingh/Gender-Classification-with-Python
from sklearn import treeclf = 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
回答:
from sklearn import treeclf = 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 graphvizdot_data = tree.export_graphviz(clf, out_file=None)graph = graphviz.Source(dot_data)graph.render("gender")
最后一行将会生成一个PDF文件gender.pdf
,显示决策树。