Graphviz installation and error correction

Enter conda install graphviz in Anaconda Prompt  

After the installation is successful, enter pip install graphviz

It will prompt for successful installation.

Start Jupyter Notebook and enter import graphviz test in the file. If no error is reported, the module is installed successfully, but when running the program, it prompts an error. Later, I found a lot of blogs and corrected the error.

Download graphviz-2.38.zip on the official website

https://graphviz.gitlab.io/_pages/Download/Download_windows.html

Unzip the file to the Anaconda3 installation directory, and then configure the environment variable for the file path. Here I made the file name graphviz-2.40.1 by myself. As long as the environment variable is configured, it's ok, and the file name doesn't matter.


Configure under the path of the system variable


import numpy as np
import matplotlib.pyplot as plt

from sklearn import datasets
from sklearn.tree import DecisionTreeClassifier

from IPython.display import Image
from sklearn import tree

import graphviz

# 仍然使用自带的iris数据
iris = datasets.load_iris()
X = iris.data
y = iris.target
# 训练模型,限制树的最大深度4
clf = DecisionTreeClassifier(max_depth=4)
# clf = DecisionTreeClassifier(max_depth=4,criterion='entropy')  #'entropy'
clf = DecisionTreeClassifier(max_depth=4,criterion='gini',splitter='best')  #'random'
#max_feature   'none'     'log2'   'sqrt'   
#拟合模型
clf.fit(X, y)

# dtModel:决策树模型
# out_file:图形数据的输出路径
# class_names:目标属性的名称,一般用于中文化
# feature_names:特征属性的名称,一般用于种文化
# filled= True :是否使用颜色填充
# rounded=True:边框是否采用圆角边框
# special_characters: 是否有特殊字符
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 = graphviz.Source(dot_data)
graph 
Operation result: (the picture is only part)


Guess you like

Origin blog.csdn.net/qq_28409193/article/details/79880886