1

I am trying to follow one of Googles Machine Learning Videos I am specifically stuck at this point (https://youtu.be/tNa99PG8hR8?t=265)

I'm working in Spyder / Python 2.7:

from sklearn.datasets import load_iris
import numpy as np
from sklearn import tree

iris = load_iris()
test_idx = [0,50,100]

#Training Data
train_target = np.delete(iris.target, test_idx)
train_data = np.delete(iris.data, test_idx, axis=0)

#testing data
test_target = iris.target[test_idx]
test_data = iris.data[test_idx]

clf = tree.DecisionTreeClassifier()
clf.fit(train_data, train_target)

print test_target
print clf.predict(test_data)

#Copied code
from sklearn.externals.six import StringIO
import pydot 
dot_data = StringIO()  
tree.export_graphviz(clf, out_file=dot_data,  
                         feature_names=iris.feature_names,  
                         class_names=iris.target_names,  
                         filled=True, rounded=True,  
                         special_characters=True)  
graph = pydot.graph_from_dot_data(dot_data.getvalue())  
graph.write_pdf("iris.pdf")

Ouput is

NameError: global name 'dot_parser' is not defined

Im using PydotPlus and Pyparsing 2 as instructed: NameError: global name 'dot_parser' is not defined

From conda list i get

pyparsing 2.0.3 py27_0 defaults
pydotplus 2.0.2 <pip> defaults
anothermh
  • 9,815
  • 3
  • 33
  • 52
SDBlackwood
  • 51
  • 1
  • 5

2 Answers2

2

I found that changing the code to

import pydotplus

and

graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  

worked fine.

SDBlackwood
  • 51
  • 1
  • 5
1

I had the same issue as yours while doing the google machine learning tutorials. To solve it I did the following: (PS: I am using Linux Mint 17.3 Cinammon.)

Pre-requisites:
In the 1st episode of the tutorial, the lecturer (JoshGordon) suggests that you install scikit-learn from Anaconda.
Tutorial link: https://www.youtube.com/watch?v=cKxRvEZd3Mw
Anaconda Download Link: https://www.continuum.io/downloads
That is EXACTLY what I did, I installed Anaconda for my system. (Linux Mint)

With anaconda installed, I opened my Terminal and threw in the following commands:

conda install graphviz
conda install pydot

And then I run the script again and it worked.

PS: I think that it worked because when you try to run the script, python looks for the pydot and graphviz libraries inside Anaconda, but the thing is, they don't seem to come bundled with it, you have to install them first.

Chagall
  • 282
  • 3
  • 12