Question: Rewrite the function linear_regression that [does the instructions in the image below], and make sure it passes the assertion. Copyable Code: def linear_regression(X,Y,lam=0): Train

Rewrite the function linear_regression that [does the instructions in the image below], and make sure it passes the assertion.

Rewrite the function linear_regression that [does the instructions in the image below],Copyable Code:

def linear_regression(X,Y,lam=0): """ Train linear regression model

Parameters: X (np.array): A numpy array with the shape (N, d) where N is the number of data points and d is dimension Y (np.array): A numpy array with the shape (N,), where N is the number of data points lam (int): The regularization coefficient where default value is 0 Returns: beta (np.array): A numpy array with the shape (d+1, 1) that represents the linear regression weight vector """ assert X.ndim==2 N = X.shape[0] d = X.shape[1] assert Y.size == N Y_col = Y.reshape(-1,1) # ADD your code here

assert beta.shape == (d+1, 1) return beta

ASSERTION CHECK:

# Performing sanity checks on your implementation some_X = (np.arange(35).reshape(7,5) ** 13) % 20 some_Y = np.sum(some_X, axis=1) some_beta = linear_regression(some_X, some_Y, lam=0) assert np.array_equal(some_beta.round(3), np.array([[ 0.], [ 1.], [ 1.], [ 1.], [ 1.], [ 1.]]))

some_beta_2 = linear_regression(some_X, some_Y, lam=1) assert np.array_equal(some_beta_2.round(3), np.array([[0.032], [0.887], [1.08 ], [1.035], [0.86 ], [1.021]]))

another_X = some_X.T another_Y = np.sum(another_X, axis=1) another_beta = linear_regression(another_X, another_Y, lam=0) assert np.array_equal(another_beta.round(3), np.array([[-0.01 ], [ 0.995], [ 1.096], [ 0.993], [ 0.996], [ 0.995], [ 0.946], [ 0.966]]))

# Checking against the pre-computed test database test_results = test_case_checker(linear_regression, task_id=1) assert test_results['passed'], test_results['message']

and make sure it passes the assertion. Copyable Code: def linear_regression(X,Y,lam=0): """

Write a function linear_regression that fits a linear regression model, and takes the following two arguments as input: 1. x : A numpy array of the shape (N,d) where N is the number of data points, and d is the data dimension. Do not assume anything about N or d other than being a positive integer. 2. Y : A numpy array of the shape (N,)whereN is the number of data points. 3. lam : The regularization coefficient , which is a scalar positive value. See the objective function below. and returns the linear regression weight vector =01d which is a numpy array with a shape of (d+1,1). Your function should: 1. Have an Intercept Weight: In other words, your fitting model should be minimizing the following mean-squared loss L(;X,Y,)2=N1i=1N(y(i)(0+1x1(i)+2x2(i)++dxd(i)))2+T. An easy way to do this is by concatenating a constant 1-column to the data matrix (think about the right numpy function and the proper call given the defined loss and weight vector format). Hint: The textbook has provided you with the solution for the least squares optimization with ridge regression which could be helpful. 2. Never Raise An Error, and Return the Solution with the Smallest Euclidean Norm in case the optimal weight vector is not unique. For instance, when the number of data points is smaller than the dimension, many optimal weight vectors exist. Hint: Reviewing your linear algebra may be helpful in this case. You may want to use the Moore-Penrose matrix inversion. Note: The regularization coefficient will not be used for the first two problems. However, it would be used later, and we expect you to implement it correctly here. def linear_regression (X,Y,lam=0): assert X. ndim ==2 N=X. shape [0] d=X. shape [1] assert Y, size ==N Y col =Y. reshape (1,1) \# your code here assert beta.shape =(d+1,1) return beta

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!