Graphviz是AT&T Labs Research開發(fā)的結(jié)構(gòu)化圖形繪制工具,支持多種格式輸出。它的輸入是一個用dot語言編寫的繪圖腳本,通過對輸入腳本的解析,分析出其中的點,邊以及子圖,然后根據(jù)屬性進行繪制。因此可利用Graphviz可以將Sklearn生成dot格式的決策樹可視化。
一定要注意安裝順序
第一步、安裝pydotplus
可以在 cmd 窗口中,直接使用下面pip或conda命令安裝:
pip3 install pydotplus
conda install pydotplus
第二步、安裝Graphviz
Graphviz 包不能使用pip或conda進行安裝,否則安裝完成后使用時報下面這種錯誤:
pydot.InvocationException: GraphViz’s executables not found:
手動安裝可以解決這個問題,下載地址:http://www.graphviz.org/download/
選擇下載stable版本

image.png

image.png
打開點擊next默認安裝,然后把 bin 默認安裝路徑C:\Program Files (x86)\Graphviz2.38\bin添加到系統(tǒng)環(huán)境變量PATH就可以使用了。
Python3 測試代碼
from sklearn.datasets import load_iris
from sklearn import tree
import pydotplus
iris = load_iris()
clf = tree.DecisionTreeClassifier()
clf = clf.fit(iris.data, iris.target)
dot_data = tree.export_graphviz(clf, out_file=None,
feature_names=iris.feature_names,
class_names=iris.target_names,
filled=True, rounded=True,
special_characters=True)
graph = pydotplus.graph_from_dot_data(dot_data)
graph.write_pdf('iris.pdf')

image.png

image.png