有没有办法从随机森林模型中提取树的深度?
Is there a way to extract Tree depths from a Random Forest model?
我已经创建了一个随机森林分类器,并且我正在 尝试生成我的随机森林模型的树木深度的直方图 。我只是无法提取森林中每棵树的深度。
我的 RF 模型称为 'RF_optimised',我已经尝试使用下面的代码迭代我的树并可视化哪个有效。我已经阅读了 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()))
我需要一个函数来遍历随机森林中的树并将树的深度存储在列表或数据框中,以便稍后生成直方图。有人可以帮忙吗?
解释器中的一些探索显示每个 Tree
实例都有一个 max_depth
参数,这似乎是我正在寻找的 - 同样,它没有记录。
[estimator.tree_.max_depth for estimator in RF_optimised.estimators_]
对我有用:)
我已经创建了一个随机森林分类器,并且我正在 尝试生成我的随机森林模型的树木深度的直方图 。我只是无法提取森林中每棵树的深度。
我的 RF 模型称为 'RF_optimised',我已经尝试使用下面的代码迭代我的树并可视化哪个有效。我已经阅读了 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()))
我需要一个函数来遍历随机森林中的树并将树的深度存储在列表或数据框中,以便稍后生成直方图。有人可以帮忙吗?
解释器中的一些探索显示每个 Tree
实例都有一个 max_depth
参数,这似乎是我正在寻找的 - 同样,它没有记录。
[estimator.tree_.max_depth for estimator in RF_optimised.estimators_]
对我有用:)