使用python绘制决策树

时间:2016-07-07 09:41:41

标签: python plot scikit-learn decision-tree

您好我发现了这段代码,我试图绘制决策树,但最后这个" visualize_tree(test,columns)"给我一个错误:这是代码

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)

正如我所说,只有最后一行给我一个错误:

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_'

1 个答案:

答案 0 :(得分:1)

visualize_tree的docstring声明第一个参数应该是DecisionTreeClassifier的一个实例。所以调用它的正确方法是

visualize_tree(dt, columns)

而不是

visualize_tree(test, columns)

因为test(根据堆栈跟踪)是DataFrame

有关使用exit的更新:我认为代码旨在使用sys.exit,它允许使用str参数。但是,如果您在PyCharm中运行IPython,这也会失败(请参阅此issue)。你可以做的就是:

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

更重要的一点是visualize_tree只有subprocess.check_call以异常终止才能达到此声明。因此,请确保您拥有Graphviz installed