Ridge Regularization (L2 Regularization) › Example
Machine Learning from ScratchAll chapters Ridge Regularization (L2 Regularization) › ExampleOne section · Sections in this chapter5 / 9 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Let's work through a simple mathematical example to understand how Ridge regression works. We remind ourselves of the ridge estimator:
We'll use a small dataset with 3 observations and 2 features (excluding scaling to make the example easier to follow). Given following data:
- observations
- features
- (regularization parameter)
Let's walk through the Ridge regression calculation step by step.
Step 1: Calculate
First, we compute the matrix multiplication of the transpose of and . This gives us the sum of squares and cross-products matrix of the features, which is used in both OLS and Ridge regression.
To verify: The element is . The element is .
Note: This matrix is symmetric, meaning for all . This is always true for .
Step 2: Add regularization term
Next, we add the regularization term to the matrix. This penalizes large coefficients and helps prevent overfitting by making the matrix more stable. The resulting matrix is better conditioned, a term from numerical linear algebra that means small changes in input tend to produce small changes in output.
Step 3: Calculate
We then compute the product of the transpose of and the target vector . This operation measures how each feature in is correlated with the target variable . It forms the right-hand side of the normal equations used in both OLS and Ridge regression.
To verify: The first element is . The second element is .
Step 4: Find the inverse and solve
Now, we invert the regularized matrix from Step 2 to solve the linear system and find the coefficients.
Note: In mathematics, the inverse of a matrix is given by , provided . For larger matrices, the process involves more advanced linear algebra techniques such as Gaussian elimination or using determinants and adjugates.
In practice, when working with code, you can invert matrices using libraries like NumPy. For example, in Python, you can use
np.linalg.inv(matrix)to compute the inverse of a matrix. However, for solving systems like Ridge regression, it's often more numerically stable to usenp.linalg.solve()or specialized solvers rather than explicitly computing the inverse.
np.linalg.inv(A)explicitly computes the inverse of a matrix and then multiplies it by , which is slower and can amplify numerical errors. In contrast,np.linalg.solve(A, b)directly solves the system using efficient factorizations without forming the inverse, making it both faster and more numerically stable.
Step 5: Calculate Ridge coefficients
Finally, we multiply the inverse matrix by to obtain the Ridge regression coefficients. These coefficients are shrunk compared to the OLS solution due to the regularization.
To verify the matrix multiplication:
- First element:
- Second element:
Then dividing by 104: and .
If we compare it with OLS which has no regularization (), the OLS solution would be:
Let's calculate this step by step:
First, compute the determinant:
Then multiply by :
where and .
Comparison:
- Ridge coefficients:
- OLS coefficients:
Notice how Ridge shrunk both coefficients toward zero compared to OLS. The first coefficient decreased from 1.413 to 1.394, and the second from 1.747 to 1.644.