使用sklearn,如何找到决策树的深度? [英] Using sklearn, how do I find depth of a decision tree?
问题描述
我正在用sklearn训练决策树。当我使用时:
I am training a decision tree with sklearn. When I use:
dt_clf = tree.DecisionTreeClassifier()
max_depth
参数默认为 None
。根据文档,如果 max_depth
为 None
,则节点将被扩展直到所有叶子都是纯净的或直到所有叶子都包含小于 min_samples_split
个样本。
the max_depth
parameter defaults to None
. According to the documentation, if max_depth
is None
, then nodes are expanded until all leaves are pure or until all leaves contain less than min_samples_split
samples.
拟合模型后,如何找出 max_depth
实际上是? get_params()
函数无济于事。拟合后, get_params()
仍显示 None
。
After fitting my model, how do I find out what max_depth
actually is? The get_params()
function doesn't help. After fitting, get_params()
it still says None
.
如何获取 max_depth
的实际数字?
How can I get the actual number for max_depth
?
文档: https://scikit-learn.org/stable/modules/generation/sklearn.tree.DecisionTreeClassifier.html
推荐答案
访问底层 Tree
对象的 max_depth
:
from sklearn import tree
X = [[0, 0], [1, 1]]
Y = [0, 1]
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X, Y)
print(clf.tree_.max_depth)
>>> 1
您可以使用以下方法从基础树对象中获得更多可访问的属性:
You may get more accessible attributes from the underlying tree object using:
help(clf.tree_)
其中包括 max_depth
, node_count
和其他较低级别的参数。
These include max_depth
, node_count
, and other lower-level parameters.
这篇关于使用sklearn,如何找到决策树的深度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!