1

Hi I've found this code and I'm trying to plot a decision tree, but at the very end this "visualize_tree(test,columns)" give me an error: this is the code

from __future__ import print_function

import os
import subprocess

import pandas as pd
import numpy as np
from sklearn.tree import DecisionTreeClassifier, export_graphviz



y = test["churn"]
X = test[columns]
dt = DecisionTreeClassifier(min_samples_split=20, random_state=99)
dt.fit(X, y)

def visualize_tree(tree, feature_names):
    """Create tree png using graphviz.

    Args
    ----
    tree -- scikit-learn DecsisionTree.
    feature_names -- list of feature names.
    """
    with open("dt.dot", 'w') as f:
        export_graphviz(tree, out_file=f,
                        feature_names=feature_names)

    command = ["dot", "-Tpng", "dt.dot", "-o", "dt.png"]
    try:
        subprocess.check_call(command)
    except:
        exit("Could not run dot, ie graphviz, to "
             "produce visualization")



visualize_tree(test,columns)

As I said only the very last line give me an error:

In[471]: visualize_tree(test,columns)

Traceback (most recent call last):
  File "C:\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3066, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-471-ccc62a7b61d9>", line 1, in <module>
    visualize_tree(test,columns)
  File "<ipython-input-470-be9bd10e9f84>", line 81, in visualize_tree
    feature_names=feature_names)
  File "C:\Anaconda\lib\site-packages\sklearn\tree\export.py", line 403, in export_graphviz
    recurse(decision_tree.tree_, 0, criterion=decision_tree.criterion)
  File "C:\Anaconda\lib\site-packages\pandas\core\generic.py", line 2360, in __getattr__
    (type(self).__name__, name))
AttributeError: 'DataFrame' object has no attribute 'tree_'
2
  • Could you add the stacktrace? Commented Jul 7, 2016 at 10:05
  • done in the original message, thanks Commented Jul 7, 2016 at 10:15

1 Answer 1

1

The docstring of visualize_tree states that the first argument should be an instance of DecisionTreeClassifier. So the correct way to call it is

visualize_tree(dt, columns)

and not

visualize_tree(test, columns)

because test (according to the stacktrace) is a DataFrame.


An update regarding the use of exit: I think the code intended to use sys.exit, which allows using an str argument. However, this fails as well if you're running IPython inside PyCharm (see this issue). Whay you could do instead is this:

sys.stderr.write("Could not run dot, ie graphviz, to produce visualization")
sys.exit(1)

The more important part is that the visualize_tree could only reach this statement if subprocess.check_call terminated with an exception. So make sure you have Graphviz installed.

Sign up to request clarification or add additional context in comments.

3 Comments

in this case the error is: Traceback (most recent call last): File "C:\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3066, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-473-7bc69dfb3e19>", line 1, in <module> visualize_tree(dt, columns) File "<ipython-input-470-be9bd10e9f84>", line 86, in visualize_tree exit("Could not run dot, ie graphviz, to " TypeError: __call__() takes exactly 1 argument (2 given)
Right, just change it to sys.exit. The builtin exit (in IPython) does not accept any arguments.
do you mean: except: sys.exit("Could not run dot, ie graphviz, to " "produce visualization") because it give error as well File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 5.0.4\helpers\pydev\pydevconsole.py", line 260, in DoExit os._exit(args[0]) TypeError: an integer is required

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.