Question: As we explored above, the exact solution for linear regression can be computed using an analytic solution. The solution for l 2 regularized linear regression

As we explored above, the exact solution for linear regression can be computed using an analytic solution. The solution for l2 regularized linear regression can be given as $
w=(XXT+\lambda I)1XY
$ implement this exact solution for the parameters w.
[2]
0s
def analytic_solution(X, y, lam):
"""Compute the analytic solution for regularized regression using the equation above.
Return the value of the updated parameters.
ARGS:
X -(n x m ) matrix of features
y -(n x 1) column vector of targets
lam - real number denoting the regularization term
"""
m,n = X.shape
return np.linalg.inv(X.T @ X + lam*np.eye(n))@X.T@y
Compute the analytic solution for the problem of sensory scores.
[3]
0s
start = time.time()
print("Computing analytic soln for sensory score")
w = analytic_solution(X_train, y_train, DEFAULT_LAMBDA)
duration = time.time()- start
training_loss = loss(X_train, y_train, w, L2_LOSS)
test_loss = loss(X_test, y_test, w, L2_LOSS)
print("Training loss achieved {loss}".format(loss=training_loss))
print("Test loss achieved {loss}".format(loss=test_loss))
print("Duration {time}".format(time=duration))

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 Databases Questions!