# -------- 中文乱码问题解决 -------- ''' # 先保存xxx.dot文件,然后打开保存的xxx.dot文件修改字体,修改为(注意两个fontname的位置): digraph Tree { node [shape=box, style="filled, rounded", color="black", fontname="Microsoft YaHei"] ; edge [fontname="Microsoft YaHei"] ; ''' # 其余相关代码 ------------------------------------- # 需导入模块 from sklearn import tree import re import pydotplus
# 1.直接生成图片(中文会乱码) dot_data = tree.export_graphviz(rf_model, out_file=None, feature_names=cols_name, class_names=label_name, filled=True, rounded=True, special_characters=True) graph = pydotplus.graph_from_dot_data(dot_data) with open('1.png', 'wb') as f: f.write(graph.create_png()) #2. 生成tree.dot文件 with open("tree.dot",