graphviz画决策树图中文乱码问题解决

1、修改graphviz配置文件

C:\Program Files\Graphviz2.38\fonts

C:\WINDOWS\Fonts
更改为
~/.fonts

2、将决策树dot_data文件保存下来

def save_dot_data(model):
    from sklearn import tree
    dot_data = tree.export_graphviz(model, out_file=None, 
                                feature_names=X_train.columns,  
                                class_names=['+','-'],  
                                filled=True, rounded=True)  
    with open('./output/dot_data.txt', 'w') as f:
        f.writelines(dot_data)
save_dot_data(dtc)

3、dot_data文件格式转换

  • 编码转换成utf-8编码,用python或其他工具转换,windows上txt转换会报错
  • 尽量去除label_name上的空格
  • fontname使用"Microsoft YaHei"等支持中文的字体

用python将txt转成utf8形式代码:

import codecs
txt_dir = r'D:\PythonProject\dot_data.txt'
txt_dir_utf8 = r'D:\PythonProject\dot_data_utf8.txt'

with codecs.open(txt_dir, 'r') as f, codecs.open(txt_dir_utf8, 'w', encoding='utf-8') as wf:
    for line in f:
        lines = line.strip().split('\t')
        print(lines)
        if 'label' in lines[0]:
            newline = lines[0].replace('\n', '').replace(' ', '')
        else:
            newline = lines[0].replace('\n','').replace('SimSun-ExtB', 'Microsoft YaHei')
        wf.write(newline + '\t')

4、最后在操作命令窗口用graphviz画决策树图命令如下:

dot -Tpng dot_data_utf8.txt -o example.png

你可能感兴趣的:(graphviz画决策树图中文乱码问题解决)