happy_coder
happy_coder

Reputation: 354

How to know the size (Number of nodes) of the tree built using Scikit-learn?

decReg = DecisionTreeRegressor()
clf = decReg.fit(X, Y)

Intuitively anyone would expect either decReg or calf should have a function which will return the number of nodes in the tree grown. But, I am unable to see any such function. Is there anything else to know the tree size?

Upvotes: 2

Views: 4946

Answers (2)

Krishna krish
Krishna krish

Reputation: 67

# Instantiate a decision tree
clf = tree.DecisionTreeClassifier()

# Fit the decision tree
...

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

Upvotes: 5

Artem Sobolev
Artem Sobolev

Reputation: 6069

According to the documentation, there's tree_ attribute, you can traverse that tree to find any properties of interest. In particular, children_right and children_left properties seem to be useful.

Upvotes: 2

Related Questions