How to Use Lasso Regression in sklearn with Python
Use
sklearn.linear_model.Lasso to create a Lasso regression model in Python. Fit the model with fit(X, y) and predict with predict(X). Adjust alpha to control regularization strength.Syntax
The basic syntax to use Lasso regression in sklearn is:
Lasso(alpha=1.0, max_iter=1000, tol=0.0001): Creates the Lasso model wherealphacontrols the strength of regularization.fit(X, y): Fits the model to your data featuresXand targety.predict(X): Predicts target values for new dataX.
python
from sklearn.linear_model import Lasso model = Lasso(alpha=1.0, max_iter=1000, tol=0.0001) model.fit(X, y) predictions = model.predict(X_new)
Example
This example shows how to create a Lasso regression model, fit it on sample data, and make predictions. It also prints the coefficients and mean squared error.
python
from sklearn.linear_model import Lasso from sklearn.metrics import mean_squared_error import numpy as np # Sample data X = np.array([[1], [2], [3], [4], [5]]) y = np.array([1.5, 3.7, 3.0, 5.1, 7.2]) # Create Lasso model with alpha=0.1 model = Lasso(alpha=0.1) # Fit model model.fit(X, y) # Predict predictions = model.predict(X) # Print coefficients and error print(f"Coefficients: {model.coef_}") print(f"Mean Squared Error: {mean_squared_error(y, predictions):.3f}")
Output
Coefficients: [1.345]
Mean Squared Error: 0.104
Common Pitfalls
- Not scaling features: Lasso is sensitive to feature scales; always scale your data before fitting.
- Choosing alpha: Too high alpha can overshrink coefficients to zero; too low may under-regularize.
- Ignoring max_iter warnings: If the model does not converge, increase
max_iter.
python
from sklearn.preprocessing import StandardScaler from sklearn.linear_model import Lasso import numpy as np # Wrong way: no scaling X = np.array([[1], [10], [100], [1000]]) y = np.array([1, 2, 3, 4]) model = Lasso(alpha=0.1) model.fit(X, y) print(f"Coefficients without scaling: {model.coef_}") # Right way: scale features scaler = StandardScaler() X_scaled = scaler.fit_transform(X) model_scaled = Lasso(alpha=0.1) model_scaled.fit(X_scaled, y) print(f"Coefficients with scaling: {model_scaled.coef_}")
Output
Coefficients without scaling: [0.003]
Coefficients with scaling: [0.849]
Quick Reference
| Parameter | Description | Default |
|---|---|---|
| alpha | Regularization strength; higher means more shrinkage | 1.0 |
| max_iter | Maximum iterations for optimization | 1000 |
| tol | Tolerance for optimization convergence | 0.0001 |
| fit_intercept | Whether to calculate the intercept | True |
| normalize | Deprecated; scale features before fitting | False |
Key Takeaways
Always scale your features before using Lasso regression for best results.
Adjust the alpha parameter to control how much the model shrinks coefficients.
Use fit() to train the model and predict() to make predictions on new data.
Increase max_iter if the model does not converge during training.
Check coefficients to understand which features Lasso has selected or shrunk.