SKLearner Home | About | Contact | Examples

Configure RandomForestRegressor "ccp_alpha" Parameter

The ccp_alpha parameter in scikit-learn’s RandomForestRegressor controls the complexity of the decision trees in the ensemble using cost complexity pruning.

Cost complexity pruning is a post-pruning technique that removes branches from a decision tree to reduce overfitting. The ccp_alpha parameter determines the complexity penalty threshold for pruning.

The default value for ccp_alpha is 0.0, which means no pruning is performed.

In practice, values between 0.0 and 0.1 are commonly used depending on the desired trade-off between bias and variance.

from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error

# Generate synthetic dataset
X, y = make_regression(n_samples=1000, n_features=10, n_informative=5,
                       n_targets=1, noise=0.1, 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 ccp_alpha values
ccp_alpha_values = [0.0, 0.01, 0.05, 0.1]
mse_scores = []

for ccp_alpha in ccp_alpha_values:
    rf = RandomForestRegressor(n_estimators=100, ccp_alpha=ccp_alpha, random_state=42)
    rf.fit(X_train, y_train)
    y_pred = rf.predict(X_test)
    mse = mean_squared_error(y_test, y_pred)
    mse_scores.append(mse)
    print(f"ccp_alpha={ccp_alpha}, MSE: {mse:.3f}")

Running the example gives an output like:

ccp_alpha=0.0, MSE: 208.093
ccp_alpha=0.01, MSE: 207.990
ccp_alpha=0.05, MSE: 209.144
ccp_alpha=0.1, MSE: 209.852

The key steps in this example are:

  1. Generate a synthetic regression dataset with informative and noise features
  2. Split the data into train and test sets
  3. Train RandomForestRegressor models with different ccp_alpha values
  4. Evaluate the mean squared error (MSE) of each model on the test set

Some tips and heuristics for setting ccp_alpha:

Issues to consider:



See Also