Python: NameError: global name 'dot_parser' not defined

Python is pretty new to me.

I am trying to run an example of machine learning titanic in the book "Machine Science in Python with Scikit". Classification with decision trees works fine (clf is defined correctly), but if I want to visualize the decision tree (see the code snippet below), I received the following error message (copied from IPython).

--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-34-15b1b4a5d909> in <module>() 3 dot_data = StringIO.StringIO() 4 tree.export_graphviz(clf, out_file = dot_data, feature_names = ['PClass', 'AgeFill', 'Gender']) ----> 5 graph = pydot.graph_from_dot_data(dot_data.getvalue()) 6 graph.write_png('titanic.png') C:\Users\885299\AppData\Local\Continuum\Anaconda32\lib\site-packages\pydot.pyc in graph_from_dot_data(data) 218 """ 219 --> 220 return dot_parser.parse_dot_data(data) 221 222 NameError: global name 'dot_parser' is not defined 

Can someone help me?

A snippet of code that I used (similar to a book):

 import pydot, StringIO dot_data = StringIO.StringIO() tree.export_graphviz(clf, out_file = dot_data, feature_names = ['Class', 'Age', 'Gender']) graph = pydot.graph_from_dot_data(dot_data.getvalue()) graph.write_png('titanic.png') from IPython.core.display import Image Image(filename = 'titanic.png') 
+5
source share
5 answers

If you are using Python 3, using pydotplus instead of pydot is great for me.

Here is the github repository

+1
source

There is something like a related issue on github. The recommendation is to make sure that you "install the pyparser libraries and update them." However, I'm sure they mean the pyparsing library.

You can install pyparsing by running pip install pyparsing

You can upgrade pyparsing by running pip install -U pyparsing

Also, there is a related stack question that recommends removing pyparsing and then reinstalling pyparsing and pydot.

0
source

I also highly recommend using pydotplus instead of pydot (there seems to be a problem with pydot and Python3).

This blogpost helped me: http://www.sas-programming.com/2015/04/sklearn-decisiontree-plot-example-needs.html

0
source

I used the following and got it working with Python3. Pyparser 2.2.0 is compatible with pydot.

 pip install pyparsing==2.2.0 pip install pydot 

Installed pydot 1.2.3.

If you have previous pydot packages installed, uninstall them first with pip uninstall pydot and perform a new installation as described above.

0
source

Great solutions. I had the same issue on Ubuntu 14.04. Just a few words: when I try to remove pyparsing and pydot, I had an error:

Do not remove pydot in / usr / lib / python 2.7 / dist-packages owned by OS

I decided to upgrade pip sudo pip install --upgrade pip

and then the following commands: sudo -H pip uninstall pydot sudo -H pip uninstall pyparsing and reinstall: sudo -H pip install pyparsing sudo -H pip install pydot

0
source

All Articles