🎓 A1Learner
🔍
0 of 50 lessons completed

Python ML Regularization: Ridge, Lasso and Elastic Net

Regularization is a penalty on big coefficients. The penalty keeps a linear model from leaning too hard on any one feature.

Ridge keeps every feature

Ridge with alpha=1.0 scores an R2 of 0.419 on the test rows. Every coefficient stays non-zero.

from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
data = load_diabetes()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)
from sklearn.linear_model import Ridge
model = Ridge(alpha=1.0)
model.fit(X_train, y_train)
print(round(model.score(X_test, y_test), 3))
print(int((model.coef_ == 0).sum()))

Lasso drops features to zero

Lasso with alpha=0.1 scores an R2 of 0.472. Three of its ten coefficients are exactly zero.

from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
data = load_diabetes()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)
from sklearn.linear_model import Lasso
model = Lasso(alpha=0.1)
model.fit(X_train, y_train)
print(round(model.score(X_test, y_test), 3))
print(int((model.coef_ == 0).sum()))

Elastic Net mixes the two

Elastic Net with alpha=0.01 and l1_ratio=0.5 scores an R2 of 0.374. It blends the Ridge and Lasso penalties.

from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
data = load_diabetes()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)
from sklearn.linear_model import ElasticNet
model = ElasticNet(alpha=0.01, l1_ratio=0.5, random_state=42)
model.fit(X_train, y_train)
print(round(model.score(X_test, y_test), 3))

Example

from sklearn.datasets import load_diabetes
from sklearn.linear_model import ElasticNet, Lasso, Ridge
from sklearn.model_selection import train_test_split
data = load_diabetes()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)
ridge = Ridge(alpha=1.0)
ridge.fit(X_train, y_train)
lasso = Lasso(alpha=0.1)
lasso.fit(X_train, y_train)
elastic = ElasticNet(alpha=0.01, l1_ratio=0.5, random_state=42)
elastic.fit(X_train, y_train)
print("Ridge test R2:", round(ridge.score(X_test, y_test), 3))
print("Lasso test R2:", round(lasso.score(X_test, y_test), 3))
print("Elastic Net test R2:", round(elastic.score(X_test, y_test), 3))
print("Lasso coefficients:", [round(float(c), 2) + 0.0 for c in lasso.coef_])
print("Lasso zeros:", int((lasso.coef_ == 0).sum()))
print("Ridge coefficients:", [round(float(c), 2) + 0.0 for c in ridge.coef_])

Key points

  • Coefficient: the weight a linear model gives to one feature
  • Regularization: a penalty on big coefficients, added to the training error
  • Alpha: the number that sets how strong the penalty is
  • Ridge: shrinks all coefficients toward zero, and keeps every feature
  • Lasso: shrinks coefficients, and sets some of them to exactly zero
  • Elastic Net: a mix of Ridge and Lasso, set by l1_ratio
  • Use Ridge, Lasso and ElasticNet from sklearn.linear_model

In this example, the data holds diabetes measurements with a disease progress score. The split holds back 20 percent for testing, as before. Ridge with alpha=1.0 scores an R2 of 0.419 on the test rows. Lasso with alpha=0.1 scores an R2 of 0.472, and three of its ten coefficients are exactly zero. Elastic Net with alpha=0.01 and l1_ratio=0.5 scores an R2 of 0.374. Lasso picks features by dropping some to zero, while Ridge keeps every feature with smaller coefficients.

Note

A bigger alpha means a stronger penalty and smaller coefficients.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

Which method can set some coefficients to exactly zero?