随机深林 使用graphviz进行可视化

from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(n_estimators=2, max_depth=2, random_state=1)
rf.fit(x_train, y_train)

# 随机森林画图
i=0
for per_rf in rf.estimators_:
    dot_data = tree.export_graphviz(per_rf, out_file=None, 
                         feature_names=x_train.columns,  
                         class_names=['0','1'],  
                         filled=True, rounded=True,  
                         special_characters=True)  
    graph = pydotplus.graph_from_dot_data(dot_data)  
    i = i+1
    graph.write_pdf(str(i)+"DTtree.pdf") 

  

posted @ 2022-11-23 10:02  wangssd  阅读(161)  评论(0编辑  收藏  举报