Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: NameError: global name 'dot_parser' is not defined

Tags:

python

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')
like image 468
Gerard Schouten Avatar asked Nov 09 '22 13:11

Gerard Schouten


1 Answers

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

Here is the github repo

like image 116
Kraviz Avatar answered Nov 14 '22 21:11

Kraviz