SKLearner Home | About | Contact | Examples

Configure ExtraTreesClassifier "max_depth" Parameter

The max_depth parameter in scikit-learn’s ExtraTreesClassifier controls the maximum depth of the trees in the ensemble.

Extra Trees Classifier is an ensemble method that builds a forest of unpruned decision trees. It differs from Random Forests in how it constructs decision trees, using random thresholds for each feature rather than searching for the best possible thresholds.

The max_depth parameter limits how deep each tree can grow. Deeper trees can capture more complex patterns but may lead to overfitting, while shallower trees might underfit but generalize better.

By default, max_depth is set to None, allowing trees to grow until all leaves are pure or contain less than min_samples_split samples.

Common values for max_depth range from 3 to 20, depending on the complexity of the dataset and the desired trade-off between bias and variance.

from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.metrics import accuracy_score

# Generate synthetic dataset
X, y = make_classification(n_samples=1000, n_features=20, n_informative=10,
                           n_redundant=5, n_classes=3, random_state=42)

# Split into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train with different max_depth values
max_depth_values = [None, 3, 5, 10, 20]
accuracies = []

for depth in max_depth_values:
    etc = ExtraTreesClassifier(n_estimators=100, max_depth=depth, random_state=42)
    etc.fit(X_train, y_train)
    y_pred = etc.predict(X_test)
    accuracy = accuracy_score(y_test, y_pred)
    accuracies.append(accuracy)
    print(f"max_depth={depth}, Accuracy: {accuracy:.3f}")

Running the example gives an output like:

max_depth=None, Accuracy: 0.845
max_depth=3, Accuracy: 0.735
max_depth=5, Accuracy: 0.805
max_depth=10, Accuracy: 0.830
max_depth=20, Accuracy: 0.835

The key steps in this example are:

  1. Create a synthetic multi-class classification dataset
  2. Split the data into training and test sets
  3. Train ExtraTreesClassifier models with different max_depth values
  4. Evaluate the accuracy of each model on the test set

Some tips and heuristics for setting max_depth:

Issues to consider:



See Also