可视化—解决graphviz保存决策树中文乱码问题

import pandas as pd
from sklearn.tree import DecisionTreeClassifier, export_graphviz
from sklearn.preprocessing import LabelEncoder
import graphviz
import matplotlib as mpl

data = pd.read_excel('data/sales_data.xls', index_col='序号')

data[data == '好'] = 1
data[data == '高'] = 1
data[data == '是'] = 1
data[data != 1] = -1
x = data.iloc[:, :3].values.astype(int)
y = data.iloc[:, 3].values.astype(int)
dtc = DecisionTreeClassifier(criterion='entropy')
dtc.fit(x, y)

dot_data = export_graphviz(dtc, out_file='tree.dot',
                           feature_names=['天气', '是否周末', '是否有促销'],
                           class_names=['高', '低'], filled=True,
                           rounded=True, special_characters=True)

with open('tree.dot', encoding='utf-8') as f:
    dot_grapth = f.read()
dot = graphviz.Source(dot_grapth.replace("helvetica", "MicrosoftYaHei"))  
# 解决中文乱码replace("helvetica", "MicrosoftYaHei")或者 replace("helvetica", "simHei")
dot.view()

在这里插入图片描述


如果对您有帮助,麻烦点赞关注,这真的对我很重要!!!如果需要互关,请评论留言!
在这里插入图片描述


猜你喜欢

转载自blog.csdn.net/weixin_46649052/article/details/115357665
今日推荐