【发布时间】:2021-05-19 03:51:01
【问题描述】:
我正在尝试通过 W3School 的 Python 学习机器学习。我正在尝试获取 mydecisiontree。使用 PyDotPlus 的 PNG
我正在使用 pip PyCharm 专业版 2020.3 代码如下:
import numpy as np
import matplotlib.pyplot as plt
import pandas
from sklearn import tree
import pydotplus
from sklearn.tree import DecisionTreeClassifier
import matplotlib.image as pltimg
df = pandas.read_csv("shows.csv")
d = {'UK' : 0,'USA' : 1, 'N' : 2}
df['Nationality'] = df['Nationality'].map(d)
d = {'YES' : 1, "NO" : 0}
df['Go'] = df['Go'].map(d)
features = ['Age', 'Experience', 'Rank', 'Nationality']
X = df[features]
y = df['Go']
dtree = DecisionTreeClassifier()
dtree = dtree.fit(X, y)
data = tree.export_graphviz(dtree, out_file=None, feature_names=features)
graph = pydotplus.graph_from_dot_data(data)
graph.write_png('mydecisiontree.png')
img=pltimg.imread('mydecisiontree.png')
img_plot = plt.imshow(img)
plt.show()
虽然 PyCharm 没有显示错误,但是当我运行代码时,它无法生成 PNG 文件并在行上给出错误:
graph.write_png('mydecisiontree.png')
它显示以下错误:
File "direc.....\venv\lib\site-packages\pydotplus\graphviz.py", line 1960, in create
'GraphViz\'s executables not found')
pydotplus.graphviz.InvocationException: GraphViz's executables not found
我看不到问题所在。如何解决?
【问题讨论】:
-
GraphViz是用 C/C++ 创建的外部程序,您必须单独安装 graphviz.org/download -
谢谢。它奏效了
标签: python pycharm png pygraphviz