2017-06-29 70 views
1

我有一个来自sklearn的决策树分类器,我使用pydotplus来显示它。 但是,当我的演示文稿(熵,样本和值)在每个节点上有很多信息时,我并不真正喜欢。Python - Graphviz - 删除DecisionTreeClassifier节点上的图例

enter image description here

解释它更容易的人,我想只保留决策和它的类。 我在哪里可以修改代码来做到这一点?

谢谢。

回答

1

根据documentation,不可以放弃在框内设置附加信息。唯一可能隐含忽略的是impurity参数。

但是,我已经完成了另一个有点歪曲的显式方式。首先,我保存.dot文件设置杂质为False。然后,我打开它并将其转换为字符串格式。我使用正则表达式来减去冗余标签并重新保存。

的代码是这样的:

import pydotplus # pydot library: install it via pip install pydot 
from sklearn.tree import DecisionTreeClassifier 
from sklearn.tree import export_graphviz 
from sklearn.datasets import load_iris 

data = load_iris() 
clf = DecisionTreeClassifier() 
clf.fit(data.data, data.target) 

export_graphviz(clf, out_file='tree.dot', impurity=False, class_names=True) 

PATH = '/path/to/dotfile/tree.dot' 
f = pydot.graph_from_dot_file(PATH).to_string() 
f = re.sub('(\\\\nsamples = [0-9]+)(\\\\nvalue = \[[0-9]+, [0-9]+, [0-9]+\])', '', f) 
f = re.sub('(samples = [0-9]+)(\\\\nvalue = \[[0-9]+, [0-9]+, [0-9]+\])\\\\n', '', f) 

with open('tree_modified.dot', 'w') as file: 
    file.write(f) 

以下是图像修改前和修改后:

enter image description hereenter image description here

在你的情况,似乎是在框的详细参数,所以你可能想稍微调整一下代码。

我希望有帮助!

+1

谢谢你的帮助!你救我 :) – DionysoSong