How to find the max depth in a random forest classifier?

Question:

I’ve a random forest classifier in Python with default parameters. After the classifier is built is it possible to find the max depth in random forest classifier ?

For decision tree classifier we have tree.max_depth().
Example : this

I know we can set max_depth in Random forest classifier but given a classifier with default values can we get this max_depth ? Please help.

Asked By: rockstar

||

Answers:

To find the maximum depth of a random forest classifier in Python, you can use the estimators_ attribute of the classifier object. This attribute contains a list of all of the individual decision trees that make up the random forest classifier, and each decision tree has a max_depth attribute that specifies its maximum depth.

Here is an example of how you can find the maximum depth of a random forest classifier in Python:


# Create a random forest classifier with default parameters
clf = RandomForestClassifier()

# Fit the classifier to some data
clf.fit(X, y)

# Find the maximum depth of the individual decision trees in the classifier
max_depths = [tree.tree_.max_depth for tree in clf.estimators_]

# Print the maximum depth of the classifier
print(max(max_depths))