Python - 导出图形界面类名类型错误

4

我正在积极学习如何在Python中实现决策树。

重新创建来自scikit-learn的Iris分类示例时,我会得到一个TypeError错误,错误涉及export_graphviz中存在的'class_names'和'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()) 

以上代码的错误为:

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

在我的电脑上,我已经安装了graphviz和pydot2。当我尝试安装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.

有没有办法可以让我使用export_graphviz参数来构建我想要的树形可视化呢?是否解决pygraphviz安装错误能够解决我的问题呢?

谢谢。


在下面的例子中,你在哪里看到了 plot_optionstarget_names 等等?你想要精确地可视化什么? - Ibraim Ganiev
1个回答

1
"

export_graphviz的签名是:

"
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):

正确的函数调用是(假设你的数据在iris对象中)
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) 

如果出现以下错误:
TypeError: export_graphviz() got an unexpected keyword argument 'class_names'
这意味着您的sklearn版本过旧。如果您使用的是anaconda python发行版,可以使用以下命令更新到最新版本的sklearn:
conda update scikit-learn
如果您使用其他发行版,请使用pip命令。确保您的sklearn版本为0.17。
import sklearn
print sklearn.__version__

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接