需要提前安装的:
brew : graphviz (for mac)
pip : pydotplus
# visualization model
from sklearn import tree
dot_data = \
tree.export_graphviz(
dtc, # 此处按照需要修改model name
out_file = None,
feature_names = list(X.columns), # DataFrame.columns获得数据框特征名称,此处需要list格式
filled = True,
impurity = False,
rounded = True
)
import pydotplus
graph = pydotplus.graph_from_dot_data(dot_data)
graph.get_nodes()[7].set_fillcolor("#FFF2DD")
from IPython.display import Image
Image(graph.create_png())
# graph.write_png("dtc_white_background.png") # 此处按照需要填写要保存的file_path