python3
1import pandas as pd 2import numpy as np 3from sklearn.tree import DecisionTreeClassifier, export_graphviz 4# 下記は決定木可視化のためのツール 5import graphviz 6import pydotplus 7from IPython.display import Image 8from six import StringIO 9 10data = pd.DataFrame({ 11 "buy(y)":[True,True,True,True,True,True,True,False,False,False,False,False,False], 12 "high":[4, 5, 3, 1, 6, 3, 4, 1, 2, 1, 1,1,3], 13 "size":[30, 45, 32, 20, 35, 40, 38, 20, 18, 20, 22,24,25], 14 "autolock":[1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0,1,0] 15 }) 16 17y = data.loc[:,["buy(y)"]] 18X = data.loc[:,["high", "size","autolock"]] 19 20clf = DecisionTreeClassifier() 21clf = clf.fit(X, y) 22 23 24dot_data = StringIO() 25export_graphviz(clf, out_file=dot_data, 26 feature_names=["high", "size","autolock"], 27 class_names=["False","True"], 28 filled=True, rounded=True, 29 special_characters=True) 30graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) 31Image(graph.create_png())
0 コメント