5

Python is rather new to me.

I am trying to run the titanic machine learning example of the "Machine Learning in Python with Scikit" book. The classification with decision trees works fine (clf is defined properly) but if I want to visualize the decision tree (see code snippet below) I got 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 somebody help me?

Code snippet that I used (similar to the book) was:

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')
  • 1
    Welcome to Stack Exchange! I want to say thanks for putting together a well formed question. I would suggest that you leave out "Can somebody help me?" from future posts as it can be a bit of a red flag to many members of the SO community. Otherwise, this looks like a solid question. – amccormack May 08 '15 at 15:16
  • What package is ``dot_parser`` in? Have you imported it (like you've imported ``pydot`` and ``StringIO``)? – Mike P May 08 '15 at 15:17

5 Answers5

1

In case you're using Python 3, using pydotplus instead of pydot worked fine for me.

Here is the github repo

Kraviz
  • 511
  • 4
  • 5
  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – NathanOliver Mar 18 '16 at 12:12
0

There is what appears to be a related issue on github. The recommendation is to ensure you "have installed the pyparser libraries and to update them." However, I'm pretty sure they mean the pyparsing library.

You can install pyparsing by running pip install pyparsing

You can update pyparsing by running pip install -U pyparsing

Additionally, a related stackoverflow question that recommends uninstalling pyparsing and then reinstalling pyparsing and pydot.

Community
  • 1
  • 1
amccormack
  • 13,207
  • 10
  • 38
  • 61
  • Just a small question: On a Windows PC, should I just type pip install pyparsing on the command line? If so, in which directory? – Gerard Schouten May 08 '15 at 16:28
  • `pip` doesn't come installed with python prior to python version 3.4. Here is a great [SO Post](http://stackoverflow.com/a/12476379/228489) on how to install `pip` on windows. – amccormack May 08 '15 at 17:02
0

I also strongly recommend using pydotplus instead of pydot (seems there is a problem with pydot and Python3).

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

0

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

This installed pydot 1.2.3.

If you have any prior installed pydot packages, first uninstall them with pip uninstall pydot and do a fresh installation as in above steps.

maheeka
  • 1,983
  • 1
  • 17
  • 25
0

Great solutions thanks. I had the same issue on Ubuntu 14.04. Just few words: when i try to uninstall pyparsing and pydot i had a error:

Not uninstalling pydot at /usr/lib/python2.7/dist-packages, owned by OS

i solved upgrading pip with sudo pip install --upgrade pip

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