Decision tree visualization - graphviz installation

Let's take iris as an example:

from sklearn import tree
from sklearn.datasets import load_iris

iris = load_iris()
clf = tree.DecisionTreeClassifier()

 Generate the following decision tree:

Step 1: Go to the official website to download graphviz

It is recommended to install it into \Lib\site-packages\ under your own anaconda

Download link: Download | Graphviz

Step 2: Change your name

For example, I just installed it into \Lib\site-packages\ under my anaconda, the file name is Graphviz in uppercase by default. For convenience, we changed it to graphviz.

Step 3: Configure environment variables

Add the path to the system and user environment variables: C:\Anaconda01\Lib\site-packages\graphviz\bin

Of course, if you tick the add environment variable during installation, you don't need to worry about it. 

Step 3: Download graphviz in the corresponding python environment

pip install graphviz

At this point, the file C:\Anaconda01\Lib\site-packages\graphviz has changed, and there are more python files in it.

Step 4: Simple Demonstration

Take a set of League of Legends winning and losing data as an example: the link is here

from sklearn import tree
from graphviz import sources

DT = tree.DecisionTreeClassifier(criterion='entropy',max_depth=4,min_samples_split=500)
DT = DT.fit(x_train,y_train)

# export_graphviz 还支持各种美化,包括通过他们的类着色节点(或回归值),
# 如果需要,还能使用显式变量和类名。Jupyter notebook也可以自动内联式渲染这些绘制节点:
dot_data = tree.export_graphviz(DT, out_file=None, feature_names=feature_names, class_names=['lose','win'], filled=True, rounded=True, special_characters=True)  

graph = sources.Source(dot_data)  
graph.render("DT") # 保存成pdf
graph

As you can see, the blue side on the left all loses, the right side wins, and there are losers and winners in the middle.

In addition, graphviz has a variety of uses, you can set up nodes by yourself, and you can check it yourself if you are interested.

Guess you like

Origin blog.csdn.net/suic009/article/details/126397192