簡體   English   中英

不使用 graphviz/web 可視化決策樹

[英]Visualizing decision tree not using graphviz/web

由於某些限制,我無法使用 graphviz 、 webgraphviz.com 來可視化決策樹(工作網絡與另一個世界是封閉的)。

問題:是否有一些替代實用程序或一些 Python 代碼用於至少非常簡單的可視化可能只是決策樹的 ASCII 可視化(python/sklearn)?

我的意思是,我可以特別使用 sklearn:tree.export_graphviz(),它生成具有樹結構的文本文件,從中可以讀取一棵樹,但是通過“眼睛”進行操作並不愉快......

PS請注意

graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  
Image(graph.create_png())

將不起作用,因為 create_png 隱式使用 graphviz

這是一個既不使用 graphviz 也不使用在線轉換器的答案。 從 scikit-learn 21.0 版(大約 2019 年 5 月)開始,現在可以使用 scikit-learn 的tree.plot_tree使用 matplotlib 繪制決策樹,而無需依賴於 graphviz。

import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree

X, y = load_iris(return_X_y=True)

# Make an instance of the Model
clf = DecisionTreeClassifier(max_depth = 5)

# Train the model on the data
clf.fit(X, y)

fn=['sepal length (cm)','sepal width (cm)','petal length (cm)','petal width (cm)']
cn=['setosa', 'versicolor', 'virginica']

# Setting dpi = 300 to make image clearer than default
fig, axes = plt.subplots(nrows = 1,ncols = 1,figsize = (4,4), dpi=300)

tree.plot_tree(clf,
           feature_names = fn, 
           class_names=cn,
           filled = True);

fig.savefig('imagename.png')

下圖是保存的內容。 在此處輸入圖片說明

代碼改編自這篇文章

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM