不使用graphviz / web可视化决策树 [英] Visualizing decision tree not using graphviz/web

查看:556
本文介绍了不使用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的



代码是根据帖子


Due to some restriction I cannot use graphviz , webgraphviz.com to visualize decision tree (work network is closed from the other world).

Question: Is there some alternative utilite or some Python code for at least very simple visualization may be just ASCII visualization of decision tree (python/sklearn) ?

I mean, I can use sklearn in particular: tree.export_graphviz( ) which produces text file with tree structure, from which one can read a tree, but doing it by "eyes" is not pleasant ...

PS Pay attention that

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

will NOT work, since create_png uses implicitly graphviz

解决方案

Here is an answer that doesn't use either graphviz or an online converter. As of scikit-learn version 21.0 (roughly May 2019), Decision Trees can now be plotted with matplotlib using scikit-learn’s tree.plot_tree without relying on 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')

The image below is what is saved.

The code was adapted from this post.

这篇关于不使用graphviz / web可视化决策树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆