Ana*_*gha 5 python decision-tree python-3.x pygraphviz
我无法可视化或编写决策树。我该怎么办呢?
Python版本3.5,Anaconda 3,我什至设置了环境变量
from sklearn import tree
model = tree.DecisionTreeClassifier(criterion='gini')
model=tree.DecisionTreeClassifier()
model.fit(trainData,trainLabel)
model.score(trainData,trainLabel)
predicted= model.predict(testData)
from sklearn.externals.six import StringIO
import pydot
import pydotplus
dot_data = StringIO()
tree.export_graphviz(model, out_file=dot_data)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
print(graph)
graph.write_pdf("C:\\Users\\anagha\\Desktop\\SynehackData\\DATA\\DATA\\graph.pdf")
Run Code Online (Sandbox Code Playgroud)
错误 :
InvocationException: GraphViz's executables not found
Run Code Online (Sandbox Code Playgroud)
小智 -4
您可以利用此代码获得帮助!
import pydotplus
from sklearn.datasets import load_iris
from sklearn import tree
import collections
# Data Collection
X = [ [180, 15,0],
[177, 42,0],
[136, 35,1],
[174, 65,0],
[141, 28,1]]
Y = ['man', 'woman', 'woman', 'man', 'woman']
data_feature_names = [ 'height', 'hair length', 'voice pitch' ]
# Training
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X,Y)
# Visualize data
dot_data = tree.export_graphviz(clf,
feature_names=data_feature_names,
out_file=None,
filled=True,
rounded=True)
graph = pydotplus.graph_from_dot_data(dot_data)
colors = ('turquoise', 'orange')
edges = collections.defaultdict(list)
for edge in graph.get_edge_list():
edges[edge.get_source()].append(int(edge.get_destination()))
for edge in edges:
edges[edge].sort()
for i in range(2):
dest = graph.get_node(str(edges[edge][i]))[0]
dest.set_fillcolor(colors[i])
graph.write_png('tree.png')
Run Code Online (Sandbox Code Playgroud)