Win 10+py36 安装 graphviz-2.38 和 pygraphviz-1.6
程序员文章站
2022-04-26 22:25:10
...
1 下载 graphviz-2.38.msi 和 pygraphviz-1.6(python其他版本)
2 安装 graphviz-2.38.msi
可用其默认安装地址。我是 C:\Program Files (x86)\Graphviz2.38
3.添加环境变量
将C:\Program Files (x86)\Graphviz2.38\bin 添加到系统变量path中
4 查看graphviz-2.38是否安装成功
终端,输入dot -version 显示如下则配置成功
5 安装pygraphviz-1.6
6.测试效果
6.1 实验代码mytree.py
from sklearn.datasets import fetch_20newsgroups
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import classification_report
from sklearn.feature_extraction import DictVectorizer
from sklearn.tree import DecisionTreeClassifier, export_graphviz
import pandas as pd
def decision():
"""
决策树对泰坦尼克号进行生死预测
:return:None
"""
titan = pd.read_csv("http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic.txt")
# 处理数据
x = titan[['pclass', 'age', 'sex']]
y = titan['survived']
# print(x)
# 缺失值处理
x['age'].fillna(x['age'].mean(), inplace=True)
# 先分割数据集到训练集和测试集
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25)
# 进行处理one_hot编码
dict = DictVectorizer(sparse=False)
x_train = dict.fit_transform(x_train.to_dict(orient='records'))
print(dict.get_feature_names())
x_test = dict.transform(x_test.to_dict(orient='records'))
# print(x_test)
# 决策树预测
dec = DecisionTreeClassifier()
dec.fit(x_train, y_train)
# 预测准确率
print("预测准确率:", dec.score(x_test, y_test))
# 导出决策树结构
export_graphviz(dec, out_file="./tree.dot",
feature_names=['年龄', 'pclass=1st', 'pclass=2nd',
'pclass=3rd', '女性', '男性'] )
if __name__ == '__main__':
decision()
6.2 运行如下:
6.3 在D:\Jeff路径下生成 文件 tree.dot
6.4 终端上切换到文件路径,输入dot -Tpng tree.dot -o tree.png
路径D:\Jeff 下生成 tree.png
参考:https://blog.csdn.net/weixin_45170058/article/details/102575418
上一篇: Windows开发基础知识
下一篇: python graphviz安装
推荐阅读