我正在积极学习如何在python中实现决策树.
当从scikit-learn重新创建Iris分类示例时,我得到了export_graphviz中存在的参数的TypeError,即’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安装错误的解决方案导致我的树的解决方案?
谢谢,
解决方法:
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()得到一个意外的关键字参数’class_names’
这意味着你的sklearn是旧版本.如果您使用的是anaconda python发行版,则可以使用更新到最新版本的sklearn
conda更新scikit-learn.如果您在任何其他发行版上,请使用pip命令.确保您的sklearn是0.17版本.
import sklearn
print sklearn.__version__