我正在使用sklearn构建决策树模型。
我认为模型工作得很好,但我不知道为什么它不像matplotlib的show()函数那样自动显示图片。这与设置有关吗?下面是代码:
import pandas as pd
from sklearn import tree
from sklearn.model_selection import train_test_split
from sklearn.tree import export_graphviz
import graphviz
data = pd.read_excel('file location')
target_vars = ['variable1','variable2','variable3']
X = pd.DataFrame()
for i in target_vars:
X[i]=data[i]
y = data['outcome']
X_tn, X_te, y_tn, y_te = train_test_split(X, y, random_state=0)
regr = tree.DecisionTreeClassifier(criterion='entropy', max_depth=5)
regr.fit(X_tn,y_tn)
y_pred = regr.predict(X_te)
accuracy = (y_pred==y_te).mean()
print('Model Accuracy: ', accuracy)
export_graphviz(regr, out_file='tree.dot', class_names=['1','0'],
feature_names=target_vars, impurity = True, filled = True)
with open('tree.dot') as f:
dot_graph = f.read()
graphviz.Source(dot_graph)
字符串
1条答案
按热度按时间dgenwo3n1#
首先,你应该使用命令行在你的设备上安装
Graphviz
,而不仅仅是在PyCharm中。sudo apt-get install graphviz
个然后你可以像这样查看dot文件:
字符串