我一直在尝试安装graphviz并与python连接,以便为决策树绘制一些节点。我已经阅读了很多和我有相同问题的帖子,但我执行了许多解决方案,但我仍然无法执行我的决策树:
我不是程序员,我只是一个简单的经济学家,正在努力学习机器学习模型,所以对我来说,很难阅读其他线程中提供的许多解决方案。
我已经可以在我的命令和conda install -c anaconda pydot的conda install -c anaconda graphviz和安装完成。(我还从GraphViz页面下载了rar包)
然后我尝试导入graphviz,但是python显示了下面的错误No module named 'graphviz'。
然后我尝试用下面的cd C:\Program Files (x86)\Graphviz2.38\bin在我的环境中添加一个新的路径,但是我仍然有同样的问题。
我试图在我的spyder代码中运行以下脚本,但没有成功
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pydot
from IPython.display import Image, display
# import graphviz as gv
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.externals.six import StringIO
from sklearn.tree import DecisionTreeRegressor, DecisionTreeClassifier,
export_graphviz
from sklearn.ensemble import BaggingClassifier, RandomForestClassifier,
BaggingRegressor, RandomForestRegressor, GradientBoostingRegressor
from sklearn.metrics import mean_squared_error,confusion_matrix,
classification_report
# This function creates images of tree models using pydot
def print_tree(estimator, features, class_names=None, filled=True):
tree = estimator
names = features
color = filled
classn = class_names
dot_data = StringIO()
export_graphviz(estimator, out_file=dot_data, feature_names=features,
class_names=classn, filled=filled)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
return(graph)
hitter =
pd.read_csv('C:\\Users\\ldresl\\Documents\\Chapter8\\Hitters.csv',sep=';')
hitter = hitter.dropna()
#Llamo una nueva matriz
X = hitter[['Years','Hits']].as_matrix()
y = np.log(hitter.Salary.as_matrix())
#Se corre todo el codigo junto
fig, (ax1, ax2) = plt.subplots(1,2, figsize=(11,4))
ax1.hist(hitter.Salary.as_matrix())
ax1.set_xlabel('Salary')
ax2.hist(y)
ax2.set_xlabel('Log(Salary)');
# Corro la regresion de la decision tree (NOTAR QUE NO ES RANDOM FOREST!!!)
regr = DecisionTreeRegressor(max_leaf_nodes=3)
regr.fit(X, y)
graph, = print_tree(regr, features=['Years', 'Hits'])
Image(graph.create_png())但是,每当我尝试运行最后两行代码时,都会抛出以下错误[WinError 2] "dot.exe" not found in path.。另外,如果我写的话,import graphviz as gv找不到它。
对不起,我的英语:(我正在学习:)。
发布于 2019-12-17 08:06:12
现在在Anaconda.org上有一个python-graphviz包,它包含了graphviz工具的Python接口。只需使用以下命令进行安装:
conda install python-graphviz有关更多信息,请查看here。
发布于 2020-04-13 22:06:22
下面的命令在windows 10,Anaconda版本4.8.3中对我有效:
conda install -c anaconda python-graphviz发布于 2018-04-30 23:43:14
解决这个问题的最好方法是:
源激活anaconda
pip安装pydot
pip安装pydotplus
pip安装pydot-ng
然后根据您的操作系统类型下载并安装Graphviz:
http://www.graphviz.org/download/
有关更多信息,请查看我之前的回答:
https://stackoverflow.com/questions/50103486
复制相似问题