【发布时间】:2019-10-07 21:44:19
【问题描述】:
我创建了一个随机森林分类器,我正在尝试生成我的随机森林模型的树深度的直方图。我只是无法提取森林中每棵树的深度。
我的 RF 模型称为“RF_optimized”,我尝试使用下面的代码来迭代我的树并可视化哪个有效。我浏览了estimators_ 和export_graphviz 文档,但似乎没有办法提取树的实际深度。
from sklearn import tree
from sklearn.tree import export_graphviz
from sklearn.externals.six import StringIO
# Create a string buffer to write to (a fake text file)
f = StringIO()
i_tree = 0
for tree_in_forest in RF_optimised.estimators_:
export_graphviz(tree_in_forest,out_file=f,
#feature_names=col,
filled=True,
rounded=True,
proportion=True)
graph = pydotplus.graph_from_dot_data(f.getvalue())
display(Image(graph.create_png()))
我需要一个函数来遍历我的随机森林中的树并将树的深度存储在列表或数据框中,以便稍后生成直方图。有人可以帮忙吗?
【问题讨论】:
标签: python machine-learning scikit-learn tree random-forest