Python-Graphviz-删除DecisionTreeClassifier节点上的图例 [英] Python - Graphviz - Remove legend on nodes of DecisionTreeClassifier
问题描述
我有一个来自sklearn的决策树分类器,我使用pydotplus进行显示。
但是我真的不喜欢当演示中每个节点上都有很多信息时(熵,样本和值)。
要向人们解释,我会更容易喜欢只保留决定和阶级。
在哪里可以修改代码来完成它?
谢谢。
根据 < img src = https://i.stack.imgur.com/e1LhF.png alt =在此处输入图片描述>
在您的情况下,框中似乎有更多参数,因此您可能需要稍微调整一下代码。
我希望能有所帮助!
I have a decision tree classifier from sklearn and I use pydotplus to show it. However I don't really like when there is a lot of informations on each nodes for my presentation (entropy, samples and value).
To explain it easier to people I would like to only keep the decision and the class on it. Where can I modify the code to do it ?
Thank you.
Accoring to the documentation, it is not possible to abstain from setting the additional information inside boxes. The only thing that you may implicitly omit is the impurity
parameter.
However, I have done it the other explicit way which is somewhat crooked. First, I save the .dot file setting the impurity to False. Then, I open it up and convert it to a string format. I use regex to subtract the redundant labels and resave it.
The code goes like this:
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)
Here are the images before and after modification:
In your case, there seems to be more parameters in boxes, so you may want to tweak the code a little bit.
I hope that helps!
这篇关于Python-Graphviz-删除DecisionTreeClassifier节点上的图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!