如果没有对 max_depth、min_samples 等的限制,是否有办法检索 sklearn.tree.DecisionTreeClassifier 生成的最终节点数?
最佳答案
节点数
一旦您拥有了树,您就可以访问其内部的 tree_ 对象,以及该树的各种属性。来源中描述的其中之一是 node_count :
Attributes
- node_count : int
The number of nodes (internal nodes + leaves) in the tree.
所以你可以这样做:
c = DecisionTreeClassifier(…)
c.fit(…)
n_nodes = c.tree_.node_count
节点的其他属性
节点的其他各种属性存储在数组中,这些数组是树对象的属性,并由节点 ID 索引。例如,value 属性是节点分数的数组,n_node_samples 是每个节点的样本数数组。 ericmjl的回答通过有关该表示的引用来更详细地介绍。您可以使用它来获取特定节点的值:
c = DecisionTreeClassifier(…)
c.fit(…)
value_i = c.tree_.value[i]
关于python - 有没有办法检索 sklearn.tree.DecisionTreeClassifier 生成的最终节点数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28160129/