在PyCharm环境中使用graphviz遇到的问题

目录

问题一:请确保Graphviz可执行文件在您的系统路径上

解决办法:

结果展示 :

问题二:Graphviz中出现的中文乱码问题

解决办法:

结果展示 :


问题一:请确保Graphviz可执行文件在您的系统路径上

grawentphviz.backend.execute.ExecutableNotFound: failed to execute 'dot', make sure the Graphviz executables are on your systems' PATH

解决办法:

1.安装graphviz软件(教程)

2.代码中增加以下代码(路径为graphviz软件的位置)

import os
os.environ["PATH"] += os.pathsep + 'D:/Program Files/Graphviz/bin/'

结果展示 :

from graphviz import Digraph
import os
os.environ["PATH"] += os.pathsep + 'D:/Program Files/Graphviz/bin/'

dot = Digraph('测试')
dot.node("1","Hello")
dot.node("2","World")
dot.edge('1','2')

dot.view()

在PyCharm环境中使用graphviz遇到的问题_第1张图片

问题二:Graphviz中出现的中文乱码问题

解决办法:

增加代码:fontname="Microsoft YaHei"

dot_data = tree.export_graphviz(clf
                                , feature_names=feature_name
                                , class_names=["琴酒", "雪莉", "贝尔摩德"]
                                , filled=True
                                , rounded=True
                                , fontname="Microsoft YaHei"  
                                )
graphviz.Source(dot_data).view()

结果展示 :

from sklearn import tree
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split
import graphviz
import os

os.environ["PATH"] += os.pathsep + 'D:/Program Files/Graphviz/bin/'

wine = load_wine()
Xtrain, Xtest, Ytrain, Ytest = train_test_split(wine.data, wine.target, test_size=0.3)
clf = tree.DecisionTreeClassifier(criterion="entropy")  # 构建分类决策树
clf = clf.fit(Xtrain, Ytrain)  # 训练模型
score = clf.score(Xtest, Ytest)  # 测试模型准确度

feature_name = ['酒精', '苹果酸', '灰', '灰的碱性', '镁', '总酚', '类黄酮', '非黄烷类酚类', '花青素', '颜色强度', '色调', 'od280/od315稀释葡萄酒', '脯氨酸']

dot_data = tree.export_graphviz(clf
                                , feature_names=feature_name
                                , class_names=["琴酒", "雪莉", "贝尔摩德"]
                                , filled=True
                                , rounded=True
                                , fontname="Microsoft YaHei"
                                )
graphviz.Source(dot_data).view()

在PyCharm环境中使用graphviz遇到的问题_第2张图片

你可能感兴趣的:(python,python,开发语言)