SKLearner Home | About | Contact | Examples

Configure Lasso "max_iter" Parameter

The max_iter parameter in scikit-learn’s Lasso class controls the maximum number of iterations for the coordinate descent solver.

Lasso (Least Absolute Shrinkage and Selection Operator) is a linear regression technique that performs both feature selection and regularization. It adds an L1 penalty term to the ordinary least squares objective, encouraging sparse solutions.

The max_iter parameter determines the maximum number of iterations the solver will run before stopping, even if it has not converged. Increasing max_iter can improve the chances of convergence but also increases computation time.

The default value for max_iter is 1000.

In practice, values between 1000 and 10000 are commonly used depending on the size and complexity of the dataset.

from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.linear_model import Lasso
from sklearn.metrics import r2_score

# Generate synthetic dataset
X, y = make_regression(n_samples=1000, n_features=10, n_informative=5,
                       n_targets=1, noise=0.5, 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_iter values
max_iter_values = [100, 1000, 5000, 10000]
r2_scores = []

for max_iter in max_iter_values:
    lasso = Lasso(max_iter=max_iter, random_state=42)
    lasso.fit(X_train, y_train)
    y_pred = lasso.predict(X_test)
    r2 = r2_score(y_test, y_pred)
    r2_scores.append(r2)
    print(f"max_iter={max_iter}, R-squared: {r2:.3f}")

Running the example gives an output like:

max_iter=100, R-squared: 0.999
max_iter=1000, R-squared: 0.999
max_iter=5000, R-squared: 0.999
max_iter=10000, R-squared: 0.999

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 Lasso models with different max_iter values
  4. Evaluate the R-squared of each model on the test set

Some tips and heuristics for setting max_iter:

Issues to consider:



See Also