Question: Ridge OLS Now, it is time to code ridge regression ( regularization using the _ ( 2 ) norm ) for OLS. See equation given

Ridge OLS
Now, it is time to code ridge regression (regularization using the _(2) norm) for OLS. See equation given below.
w=(X^(TT)X+lambdaI)^(-1)X^(TT)y
TODO 8(15 points): RidgeOrdinaryLeastSquares
fit() TODOs
Follow the below steps to implement the fit() method. Feel free to reuse code from prior homework.
Covert the ridge regression OLS equation into code to compute the optimal weights w. Make sure the bias variable is not regularized, you should only regularize the weights for the features. Store the output into self.w.
A. Hint: Use np. eye to create an identity matrix I with the shape (N,N). Set the first element value to zero to prevent the bias from being regularized as the bias is represented by the first column in the data.
predict() TODOs
Follow the below steps to implement the predict() method. Feel free to reuse code from prior homework.
Compute the predictions by taking the linear combination between the weights and passed data. Return the predictions as a 2D column vector.
class RidgeOrdinaryLeastSquares():
""" Perfroms ordinary least squares regression
Attributes:
lamb (float): Regularization parameter for controlling
L2 regularization.
w: Vector of weights
"""
def __init__(self, lamb: float):
self.lamb = lamb
self.w = None
def fit(self, X: np.ndarray, y: np.ndarray)-> object:
""" Train OLS to learn optimal weights
Args:
X: Training data given as a 2D matrix
y: Training labels given as a 1D vector
Returns:
The class's own object reference.
"""
# TODO 8.1
return self
def predict(self, X: np.ndarray)-> np.ndarray:
""" Make predictions using learned weights
Args:
X: Testing data given as a 2D matrix
Returns:
A 2D column vector of predictions for each data sample in X
"""
# TODO 8.2
return None
Ridge OLS Now, it is time to code ridge

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!