解决sklearn中,Graphviz画决策树中文乱码的问题

from sklearn  import tree 
from  sklearn.datasets import load_wine
from  sklearn.model_selection import train_test_split
import pydotplus
from IPython.display import Image

wine = load_wine()
Xtrain,Xtest,Ytrain,Ytest = train_test_split(wine.data,wine.target,test_size=0.3)
clf = tree.DecisionTreeClassifier(criterion='entropy')
clf = clf.fit(Xtrain,Ytrain)
score = clf.score(Xtest,Ytest)
print(score)


feature_name = ['酒精','苹果酸','灰','灰的碱性','镁','总酚','类黄酮','非黄烷类酚类','花青素','颜色强度','琴酒','雪莉','贝尔摩德']
dot_data = tree.export_graphviz(clf
                                ,out_file=None
                                ,feature_names= feature_name
                                ,class_names=['琴酒','雪莉','贝尔摩德']
                                ,filled = True
                                ,rounded = True
                                ,special_characters=True)       

graph = pydotplus.graph_from_dot_data(dot_data)
Image(graph.create_png())
graph.write_png("wine0.png")

F:\python387\Lib\site-packages\sklearn\tree\_export.py中的“helvetica”改为“simsun”‘Micorsoft YaHei’ 即可。在程序运行时需要在 filled = True 和 special_characters=True 之间加上rounded = True,即可。

你可能感兴趣的:(python)