Decision tree results visualization solutions to problems Chinese garbled

Problem Description:

    Because the decision tree can explain the strong, decision tree classification issues to deal with, when you export a result, there is a problem Chinese can not be displayed (displayed as a Chinese box). This situation is due to the font of causes, the solution is as follows:

1. Modify the configuration file Graphviz

    Fonts.conf font configuration file path: C: \ Program Files (x86) \ Graphviz2.38 \ fonts

    The <dir> #FONTDIR # </ dir> <dir> ~ / .fonts </ dir> Change

        <Dir> C: / Windows / fonts </ dir> <dir> ~ / .fonts </ select>

2. Save the file down the decision tree dot

    dot_data.dot View saved locally can be found, the default font fontname = helvetica, font simply modified to support Chinese characters can be, achieved by replacing regular expressions.

# dot_data.dot文件内容
digraph Tree {
node [shape=box, style="filled, rounded", color="black", fontname=helvetica] ;
edge [fontname=helvetica] ;
dot_data = export_graphviz(decision_tree=clf,
                           out_file=None,
                           feature_names=feature_names,
                           class_names=class_names,
                           filled=True,
                           rounded=True,
                           special_characters=True)
with open("./dot_data.dot", 'w', encoding="utf-8") as f:  # 注意编码方式
    f.writelines(dot_data)
# 打开 dot_data.dot,修改 fontname="支持的中文字体"
f = open("./dot_data.dot", "r+", encoding="utf-8")
open('./dot_test.dot', 'w').write(re.sub(r'fontname=helvetica', 'fontname="Microsoft YaHei"', f.read()))

3. The results were derived at operation command window graphviz

    The results can be exported to PDF or PNG image, the following results can display Chinese.

# dot -Tpng dot_test.dot -o test.png  # 将结果保存为图片
# dot -Tpdf dot_test.dot -o test-2.pdf  # 将结果保存为 PDF

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_41713230/article/details/87860133