Implement the Ridge Regression loss function. Ridge regression adds an L2 penalty term to the ordinary least squares loss. Given predictions, true values, weights, and a regularization parameter lambda, compute the total loss.
import numpy as np
def ridge_loss(y_true, y_pred, weights, alpha):
y_true = np.array(y_true, dtype=np.float64)
y_pred = np.array(y_pred, dtype=np.float64)
weights = np.array(weights, dtype=np.float64)
n = len(y_true)
mse = np.sum((y_true - y_pred) ** 2) / n
penalty = alpha * np.sum(weights ** 2)
return mse + penaltyalpha * sum(weights^2).