4

I am actively learning how to implement decision trees in python.

When recreating the Iris classification example from scikit-learn, i get a TypeError for parameters that exist in export_graphviz, namely 'class_names' and 'plot_options'.

from IPython.display import Image  
import sklearn
dot_data = StringIO()  
sklearn.tree.export_graphviz(clf, out_file=dot_data,
                     plot_options=['class', 'filled', 'label', 'sample',       'proportion'],
                 target_names=iris['target_names'],
                 feature_names=iris['feature_names'])
graph = pydot.graph_from_dot_data(dot_data.getvalue())  
Image(graph.create_png()) 

The error for the specific code above is:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-33-aba117838252> in <module>()
      5                      plot_options=['class', 'filled', 'label', 'sample', 'proportion'],
      6                      target_names=iris['target_names'],
----> 7                      feature_names=iris['feature_names'])
      8 graph = pydot.graph_from_dot_data(dot_data.getvalue())
      9 Image(graph.create_png())

TypeError: export_graphviz() got an unexpected keyword argument 'plot_options'

On my computer, I have graphviz and pydot2 installed. I receive an error when trying to install pygraphviz:

 If you think your installation is correct you will need to manually

    change the include_dirs and library_dirs variables in setup.py to

    point to the correct locations of your graphviz installation.



    The current setting of library_dirs and include_dirs is:

library_dirs=None

include_dirs=None

error: Error locating graphviz.

Is there a work around/solution to allow me to use the parameters in export_graphviz in order to build the tree visualization I want? Would pursuing a solution to the pygraphviz install error lead to a solution for my tree?

Thank you,

1
  • Where did you see plot_options, target_names, etc in the following example? And what do you want to visualise exactly? Commented Nov 19, 2015 at 5:29

1 Answer 1

1

The signature of export_graphviz is

def export_graphviz(decision_tree, out_file="tree.dot", max_depth=None,
                feature_names=None, class_names=None, label='all',
                filled=False, leaves_parallel=False, impurity=True,
                node_ids=False, proportion=False, rotate=False,
                rounded=False, special_characters=False):

The correct function call is (assuming that your data is in iris object)

sklearn.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) 

In case it throws up the error

TypeError: export_graphviz() got an unexpected keyword argument 'class_names'

it means your sklearn is of older version. If you are using anaconda python distribution you can update to latest version of sklearn using conda update scikit-learn. Use pip command if you are on any other distribution. Make sure your sklearn is 0.17 version.

import sklearn
print sklearn.__version__
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.