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:
- Generate a synthetic regression dataset with informative and noise features
- Split the data into train and test sets
- Train
RandomForestRegressor
models with differentccp_alpha
values - Evaluate the mean squared error (MSE) of each model on the test set
Some tips and heuristics for setting ccp_alpha
:
- Start with the default value of 0.0 and incrementally increase it
- Higher
ccp_alpha
values prune more aggressively, leading to simpler trees - There is a trade-off between bias and variance - some pruning can reduce overfitting
Issues to consider:
- Pruning too aggressively (
ccp_alpha
too high) can lead to underfitting - The optimal
ccp_alpha
value depends on the specific dataset and problem - It’s important to experiment and tune
ccp_alpha
using a validation set or cross-validation