Ridge regression (L2 regularization) adds a penalty term to the ordinary least squares objective to prevent overfitting. The regularization term shrinks the weights toward zero, which is especially useful when features are correlated or when the number of features is large relative to the number of samples.
Given a feature matrix X, a target vector y, and a regularization parameter lambda, compute the ridge regression weights using the closed-form solution.
Where I is the d x d identity matrix and lambda controls the regularization strength.
Return one float weight for each feature column.
Input: X = [[1, 0], [0, 1]], y = [3, 5], lam = 1
Output: [1.5, 2.5]
Explanation: Adding the identity matrix doubles the diagonal before solving for the weights.
Input: X = [[1, 1], [1, 2], [1, 3]], y = [3, 5, 7], lam = 0
Output: [1.0, 2.0]
Create an identity matrix with the same width as X.
Apply NumPy matrix multiplication in the same order as the closed-form equation.
Sign in to take notes on this problem
Accepts: array
Accepts: array
Accepts: number
Ridge regression (L2 regularization) adds a penalty term to the ordinary least squares objective to prevent overfitting. The regularization term shrinks the weights toward zero, which is especially useful when features are correlated or when the number of features is large relative to the number of samples.
Given a feature matrix X, a target vector y, and a regularization parameter lambda, compute the ridge regression weights using the closed-form solution.
Where I is the d x d identity matrix and lambda controls the regularization strength.
Return one float weight for each feature column.
Input: X = [[1, 0], [0, 1]], y = [3, 5], lam = 1
Output: [1.5, 2.5]
Explanation: Adding the identity matrix doubles the diagonal before solving for the weights.
Input: X = [[1, 1], [1, 2], [1, 3]], y = [3, 5, 7], lam = 0
Output: [1.0, 2.0]
Create an identity matrix with the same width as X.
Apply NumPy matrix multiplication in the same order as the closed-form equation.
Sign in to take notes on this problem
Accepts: array
Accepts: array
Accepts: number