Ridge coefficients as a function of the L2 Regularization
where X is the input data, y is the target variable,
β is the vector of coefficients associated with the features, and
α is the regularization strength.
The regularized loss function aims to balance the trade-off between accurately
predicting the training set and to prevent overfitting.
In this regularized loss, the left-hand side (e.g. ∥y−Xβ∥22) measures the squared difference between the actual target
variable, y, and the predicted values. Minimizing this term alone could
lead to overfitting, as the model may become too complex and sensitive to noise
in the training data.
To address overfitting, Ridge regularization adds a constraint, called a penalty
term, (α∥β∥22) to the loss function. This penalty
term is the sum of the squares of the model’s coefficients, multiplied by the
regularization strength α. By introducing this constraint, Ridge
regularization discourages any single coefficient βi from taking
an excessively large value and encourages smaller and more evenly distributed
coefficients. Higher values of α force the coefficients towards
zero. However, an excessively high α can result in an underfit
model that fails to capture important patterns in the data.
Therefore, the regularized loss function combines the prediction accuracy term
and the penalty term. By adjusting the regularization strength, practitioners
can fine-tune the degree of constraint imposed on the weights, training a model
capable of generalizing well to unseen data while avoiding overfitting.
from
sklearn.metrics
import
clf
=
()
alphas
=
(
-
3
,
4
,
200
)
coefs
=
[]
errors_coefs
=
[]
for
a
in
alphas
:
clf
.
set_params
(
alpha
=
a
)
.
fit
(
X
,
y
)
coefs
.
append
(
clf
.
coef_
)
errors_coefs
.
append
(
(
clf
.
coef_
,
w
))
"alpha"
)
coefs
=
(
coefs
,
index
=
alphas
,
columns
=
[
f
"Feature
{
i
}
"
for
i
in
range
(
10
)])
errors
=
(
errors_coefs
,
index
=
alphas
,
name
=
"Mean squared error"
)
fig
,
axs
=
(
1
,
2
,
figsize
=
(
20
,
6
))
coefs
.
plot
(
ax
=
axs
[
0
],
logx
=
True
,
title
=
"Ridge coefficients as a function of the regularization strength"
,
)
axs
[
0
]
.
set_ylabel
(
"Ridge coefficient values"
)
errors
.
plot
(
ax
=
axs
[
1
],
logx
=
True
,
title
=
"Coefficient error as a function of the regularization strength"
,
)
_
=
axs
[
1
]
.
set_ylabel
(
"Mean squared error"
)
alpha
In real-world scenarios where data typically includes noise, selecting an
appropriate alpha value becomes crucial in striking a balance between an
overfitting and an underfitting model.
Here, we saw that Ridge adds a penalty to the
coefficients to fight overfitting. Another problem that occurs is linked to
the presence of outliers in the training dataset. An outlier is a data point
that differs significantly from other observations. Concretely, these outliers
impact the left-hand side term of the loss function that we showed earlier.
Some other linear models are formulated to be robust to outliers such as the
HuberRegressor. You can learn more about it in
the HuberRegressor vs Ridge on dataset with strong outliers example.
Total running time of the script: (0 minutes 0.647 seconds)
Related examples