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
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%)
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, ...]
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With