Question: Please help me define the function. Thanks! In [1]: In this homework, you're asked to write the functions of ordinary least square (OLS) regression with

Please help me define the function. Thanks!

Please help me define the function. Thanks! In [1]: In this homework,you're asked to write the functions of ordinary least square (OLS) regressionwith Numpy. Please fill the code block cells with your code and

In [1]: In this homework, you're asked to write the functions of ordinary least square (OLS) regression with Numpy. Please fill the code block cells with your code and comments, run everything (select cell in the menu, and click Run all), save the notebook, and upload it to canvas. For this homeworkl you should write your code with basic Python or Numpy, and are not allowed to use any other packages/functions in Scikit- Learn, except for loading the data (codes provided below). if import the packages import numpy 39 1\"P import matplotlib.pyplot as plt import seaborn as sns Next, you can define the functions to 1) fit the parameters beta_hats 2) generate the predictions y_hat . For linear algebra operations in Numpy, you can consult here. Please also refer to the discussion file to see how to import and use the functions in linear algebra submodule of Numpy. The most tricky part is about dealing with the intercepts g, while I have already done it for you in the fitting function. In [8]: def OLsfit(data, y): write your document string here data: the data matrix, in Numpy 2D array format. The rows correspond to observations, and the columns correspond to X, y, coefficients = make_regression( ones = np.ones((data.shape[0],1)) 95' column of ones X = np.concatenate((ones, data), axis = 1) if the augmented matrix, \\tilde{x} in our lecture if write your code here return beta_hats In [3]: def OLSpredict (data, beta_hats) : "write your document string here' ' ' # write your code here return y_hat After defining the functions, we can test them with the boston house pricing dataset, loaded from scikit-learn. In [4]: # load the dataset from sklearn import datasets boston = datasets . load_boston( ) data = boston[ ' data' ] # already in Numpy array format y = boston [ ' target' ] In [5]: # call your function here to generate the prediction y_hat beta_hats = OLSfit (data, y) y_hat = OLSpredict (data, beta_hats) In [12 ] : fig, ax = plt. subplots (dpi=150) sns . scatterplot (x=y, y=y_hat) ax. set_xlabel(r'$y$', size = 20) ax. set_ylabel (r'$\\hat{y}$', rotation = 0, size = 20, labelpad = 15) sns . despine ( )\f

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!