用python绘制决策树
Posted
技术标签:
【中文标题】用python绘制决策树【英文标题】:plot a decision tree with python 【发布时间】:2016-11-09 13:56:57 【问题描述】:您好,我找到了这段代码,我正在尝试绘制决策树,但最后这个“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】:visualize_tree
的文档字符串指出第一个参数应该是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。
【讨论】:
在这种情况下,错误是: Traceback(最近一次调用最后一次):文件“C:\Anaconda\lib\site-packages\IPython\core\interactiveshell.py”,第 3066 行,在 run_code exec(code_obj,self.user_global_ns,self.user_ns)文件“sys.exit
就行了。内置的 exit
(在 IPython 中)不接受任何参数。
你的意思是:除了:sys.exit("Could not run dot, ie graphviz, to " "produce vision") 因为它也给出错误 File "C:\Program Files (x86) \JetBrains\PyCharm Community Edition 5.0.4\helpers\pydev\pydevconsole.py",第 260 行,在 DoExit os._exit(args[0]) TypeError: an integer is required以上是关于用python绘制决策树的主要内容,如果未能解决你的问题,请参考以下文章