如何知道使用 Scikit-learn 构建的树的大小(节点数)?

问题描述 投票:0回答:2
decReg = DecisionTreeRegressor()
clf = decReg.fit(X, Y)

直观上,任何人都会期望 decReg 或 calf 应该有一个函数,该函数将返回树中生长的节点数。但是,我看不到任何这样的功能。还有什么可以知道树的大小吗?

python machine-learning scikit-learn regression decision-tree
2个回答
4
投票
# Instantiate a decision tree
clf = tree.DecisionTreeClassifier()

# Fit the decision tree
...

# Print node count
print(clf.tree_.node_count)

2
投票

根据 documentation,有

tree_
属性,您可以遍历该树以查找任何感兴趣的属性。特别是,
children_right
children_left
属性似乎很有用。

© www.soinside.com 2019 - 2024. All rights reserved.