Reputation: 1170
I been trying to install graphviz and connect with python to graph some nodes for decision trees. I had read a lot of threads with the same problem as me but i perform much of the solutions but i still cannot perform my decision trees :(
I am not a programmer, i am just an simple economist which is trying to learn machine learning models, so for me is difficult to read much of the solutions provided in other threads.
I already could conda install -c anaconda graphviz
in my cmd and conda install -c anaconda pydot
and installation was completed. (I also download the rar package from GraphViz page)
Then i try to import graphviz, but python shows me the following error No module named 'graphviz'
.
So then i try to add to my environment a new path with the following cd C:\Program Files (x86)\Graphviz2.38\bin
but i continue to having the same problem.
I am trying to run the following script in my spyder codes but without any succed
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())
but whenever i try to run the last two lines throws me the following error [WinError 2] "dot.exe" not found in path.
. Also if i write import graphviz as gv
doesnt found it.
Sorry for my english :( i am learning :).
Upvotes: 8
Views: 24458
Reputation: 627
conda install --channel https://conda.anaconda.org/garylschultz pygraphviz
Upvotes: 0
Reputation: 41
Below command worked for me in windows 10, Anaconda version 4.8.3 :
conda install -c anaconda python-graphviz
Upvotes: 4
Reputation: 111
There exists now a python-graphviz package at Anaconda.org which contains the Python interface for the graphviz tool. Simply install it with:
conda install python-graphviz
Check out here for more info.
Upvotes: 11
Reputation: 5015
The best way to solve the problem is:
source activate anaconda
pip install pydot
pip install pydotplus
pip install pydot-ng
Then you download and install Graphviz according to your OS type:
http://www.graphviz.org/download/
Check my previous answer for more information:
Python RuntimeError: Failed to import pydot
Upvotes: 1