SKLearner Home | About | Contact | Examples

Configure GradientBoostingRegressor "n_iter_no_change" Parameter

The n_iter_no_change parameter in scikit-learn’s GradientBoostingRegressor controls the early stopping mechanism based on the validation score.

Gradient Boosting is an ensemble technique that builds models sequentially to correct errors made by previous models. It is widely used for regression and classification tasks. The n_iter_no_change parameter sets the number of iterations with no improvement on the validation set to trigger early stopping.

The default value for n_iter_no_change is None, meaning no early stopping. In practice, values between 5 and 10 are commonly used, depending on the dataset and computational resources.

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

# Generate synthetic dataset
X, y = make_regression(n_samples=1000, n_features=20, 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 n_iter_no_change values
n_iter_no_change_values = [None, 5, 10, 20]
mse_scores = []

for n in n_iter_no_change_values:
    gbr = GradientBoostingRegressor(n_iter_no_change=n, random_state=42)
    gbr.fit(X_train, y_train)
    y_pred = gbr.predict(X_test)
    mse = mean_squared_error(y_test, y_pred)
    mse_scores.append(mse)
    print(f"n_iter_no_change={n}, MSE: {mse:.3f}")

Running the example gives an output like:

n_iter_no_change=None, MSE: 3052.375
n_iter_no_change=5, MSE: 3378.267
n_iter_no_change=10, MSE: 3378.267
n_iter_no_change=20, MSE: 3378.267

The key steps in this example are:

  1. Generate a synthetic regression dataset with noise to simulate real-world conditions.
  2. Split the data into train and test sets.
  3. Train GradientBoostingRegressor models with different n_iter_no_change values.
  4. Evaluate the mean squared error (MSE) of each model on the test set.

Some tips and heuristics for setting n_iter_no_change:

Issues to consider:



See Also