PIXELBANKv8.2.1
Menu

Ridge Regression

Implement Ridge Regression (L2 regularization) using the regularized normal equation.

Given feature matrix XX (without bias column), target vector yy, and regularization parameter λ\lambda, compute: w=(XaTXa+λI)1XaTyw = (X_a^T X_a + \lambda I')^{-1} X_a^T y

where XaX_a has a column of ones prepended, and II' is the identity matrix with the top-left element set to 0 (we don't regularize the bias).

Return the weight vector as a list, rounded to 4 decimal places.

Example:

Input:
X = [[1], [2], [3]]
y = [2, 4, 6]
lambda_reg = 0.0
Output:
[0.0, 2.0]
Reasoning:
  • First, we prepend a column of ones to the feature matrix XX to get XaX_a: Xa=[[1,1],[1,2],[1,3]]X_a = [[1, 1], [1, 2], [1, 3]]
  • Then, we calculate XaTXaX_a^T X_a and XaTyX_a^T y: XaTXa=[[3,6],[6,14]]X_a^T X_a = [[3, 6], [6, 14]] and XaTy=[[2+4+6],[2+8+12]]=[[12],[22]]X_a^T y = [[2 + 4 + 6], [2 + 8 + 12]] = [[12], [22]]
  • Next, we compute the regularized matrix XaTXa+λIX_a^T X_a + \lambda I', where λ=0.0\lambda = 0.0 and II' is the identity matrix with the top-left element set to 0: XaTXa+λI=[[0,6],[6,14]]X_a^T X_a + \lambda I' = [[0, 6], [6, 14]]
  • Finally, we calculate the weight vector w=(XaTXa+λI)1XaTyw = (X_a^T X_a + \lambda I')^{-1} X_a^T y: w=[[0.0],[2.0]]w = [[0.0], [2.0]], which when rounded to 4 decimal places gives the output [0.0,2.0][0.0, 2.0]

Constraints:

  • X is a 2D list (n x m), y is a 1D list of n targets
  • lambda_reg is a positive float
  • Return list of (m+1) weights rounded to 4 decimal places
  • First weight is the intercept (not regularized)
  • Implement without numpy
Editor

Test Results

0/0
Run code to see test results.