Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using sklearn, how do I find depth of a decision tree?

I am training a decision tree with sklearn. When I use:

dt_clf = tree.DecisionTreeClassifier()

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.

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.

How can I get the actual number for max_depth?

Docs: https://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html

like image 248
Mel Avatar asked Feb 03 '19 01:02

Mel


People also ask

How do you determine the depth of a decision tree?

There is no theoretical calculation of the best depth of a decision tree to the best of my knowledge. So here is what you do: Choose a number of tree depths to start a for loop (try to cover whole area so try small ones and very big ones as well) Inside a for loop divide your dataset to train/validation (e.g. 70%/30%)

How do I get depth in random forest Python?

n_estimators = len(forest. estimators_) for good measure. This answer is incorrect, this tells you the the maximum allowed depth of each tree in the forest, not the actual depth. So for example a random forest trained with max_depth=10 will return: [10, 10, 10, ...]

What is Max depth in decision tree classifier?

max_depth: int or None, optional (default=None) The theoretical maximum depth a decision tree can achieve is one less than the number of training samples, but no algorithm will let you reach this point for obvious reasons, one big reason being overfitting.


2 Answers

Access the max_depth for the underlying Tree object:

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_)

These include max_depth, node_count, and other lower-level parameters.

like image 156
Primusa Avatar answered Sep 21 '22 18:09

Primusa


The answer according to the docs https://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html#sklearn.tree.DecisionTreeClassifier.get_depth is to use the tree.get_depth() function.

like image 23
hcbh96 Avatar answered Sep 21 '22 18:09

hcbh96