2017-06-29 33 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