Ordinary squares problem from end of the module project in Data science course [Solved] | Sololearn: Learn to code for FREE!

+5

# Ordinary squares problem from end of the module project in Data science course [Solved]

First of all, in the problem description they said the formula is given in the image, but I can't see any image there. Is there actually an image? Or is it bug or they forget to put image. Nevertheless the formula is on google. I used the formula and try to solve the problem. 4 test cases are correct but 1 is incorrect. I have no clue what to modify. Can anyone please revise my code or give a solution code? https://code.sololearn.com/cu5GsMXgguJB/?ref=app

2/16/2021 9:30:57 AM

Mir Abir Hossain

+9

n, p = [int(x) for x in input().split()] X = [] for i in range(n): X.append([float(x) for x in input().split()]) y = [float(x) for x in input().split()] import numpy as np X_array=np.array(X).reshape(n,p) y_array=np.array(y) Beta=np.linalg.inv(X_array.T @ X_array) @ X_array.T @ y_array print(Beta.round(2))

+8

Mir Abir Hossain @ == .dot() A = np.array( [[1,1], [0,1]] ) B = np.array( [[2,0], [3,4]] ) A * B # elementwise product # array([[2, 0], [0, 4]]) A @ B # matrix product # array([[5, 4], [3, 4]]) A.dot(B) # another matrix product #array([[5, 4], [3, 4]]) #https://numpy.org/doc/stable/user/quickstart.html

+7

Mir Abir Hossain I slightly modified your solution: 👍 # Xβ = y β = np.matmul(np.matmul(X.T, y), np.linalg.inv(X.T @ X)) β = X.T @ y @ np.linalg.inv(X.T @ X) β = np.linalg.pinv(X) @ y.T print(β.round(2)) I have reported the lack of an image to SoloLearn❗❗❗

+3

If you would like to dive into the mathematics... I found the equation (12) at: https://web.stanford.edu/~mrosenfe/soc_meth_proj3/matrix_OLS_NYU_notes.pdf

+2

Turned out the if clause was unnecessary. The portion inside the 'else' code is the solution. The reason I added 'if' clause was we can directly compute inverse matrix of a square matrix using numpy.linalg.inv(). So I thought it would be good. For non square matrix we need to compute the transpose matrix and then apply the formula in the code.

+2

Yes! A good proper image would help much!

+2

Janusz Bujak 🇵🇱 oh! Learnt new thing. Thank you ^_^

+2

Is it me or does this solution include functions not covered in this DS course??

+2

Nassim Abed yes I think some functions that required to solve this problem is not discussed in SL. Probably to check how much self study we are doing by ourselves :D

+1

Janusz Bujak 🇵🇱 in your comment, what is '@'?

+1

That wouldn't be fair then. Not when it comes to awarding certificates.