Ridge Regression
Ridge regression takes the normal loss function used in OLS regression and adds an additional term that is proportional to the norm of the weight vector.
{% L(\vec{w}) = \frac{1}{N} \sum_{i=1}^N (y_i - (w_0 + \vec{w}^T \vec{x}))^2 + \lambda || \vec{w} || ^2 %}
Here {% \lambda || \vec{w} || ^2 %} is the additional term. This term penalizes the regression for having
large weights, thereby pushing the weights toward zero. The term inlcudes the hyper-parameter {% \lambda %}
which dictates how strongly the term affects the regression.
The optimal weights of the ridge regression are given by
{% \vec{w}_{opt} = (\lambda I_D + X^TX)^{-1} X^T \vec{y} %}
Choosing Lambda
In general, there are no a-priori reasons to choose one value of lambda over another. Typically, lambda is labeled as a hyper-parameter and trained by using a validation set. See Data partitioning.Sample Code
import numpy as np
from sklearn.linear_model import Ridge
inputs = [[1.0,1.0],[1.2,2.0],[0.9,0.6]]
outputs = [[1],[2],[0.9]]
X = np.array(inputs)
y = np.array(outputs)
reg = Ridge(alpha=0.1, solver="cholesky")
reg.fit(X,y)
print(reg.intercept_)
print(reg.coef_)
SGDRegressor
import numpy as np
from sklearn.linear_model import SGDRegressor
inputs = [[1.0,1.0],[1.2,2.0],[0.9,0.6]]
outputs = [[1],[2],[0.9]]
X = np.array(inputs)
y = np.array(outputs)
reg = SGDRegressor(alpha=0.1, penalty="l2")
reg.fit(X,y)
print(reg.intercept_)
print(reg.coef_)