+ 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

16th Feb 2021, 9:30 AM
Mir Abir Hossain
Mir Abir Hossain - avatar
15 odpowiedzi
+ 10
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))
7th Mar 2021, 7:38 PM
Candelaria Colombres Garmendia
Candelaria Colombres Garmendia - avatar
+ 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
26th Feb 2021, 3:11 PM
Janusz Bujak 🇵🇱 🇺🇦
Janusz Bujak 🇵🇱 🇺🇦 - avatar
+ 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❗❗❗
26th Feb 2021, 1:18 PM
Janusz Bujak 🇵🇱 🇺🇦
Janusz Bujak 🇵🇱 🇺🇦 - avatar
+ 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
27th Feb 2021, 6:55 AM
Brian M.
Brian M. - avatar
+ 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.
16th Feb 2021, 9:59 AM
Mir Abir Hossain
Mir Abir Hossain - avatar
+ 2
Yes! A good proper image would help much!
26th Feb 2021, 2:23 PM
Mir Abir Hossain
Mir Abir Hossain - avatar
+ 2
Janusz Bujak 🇵🇱 oh! Learnt new thing. Thank you ^_^
26th Feb 2021, 3:20 PM
Mir Abir Hossain
Mir Abir Hossain - avatar
+ 2
Is it me or does this solution include functions not covered in this DS course??
9th Mar 2021, 8:08 AM
Nassim Abed
Nassim Abed - avatar
+ 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
9th Mar 2021, 8:54 AM
Mir Abir Hossain
Mir Abir Hossain - avatar
+ 1
Janusz Bujak 🇵🇱 in your comment, what is '@'?
26th Feb 2021, 3:04 PM
Mir Abir Hossain
Mir Abir Hossain - avatar
+ 1
That wouldn't be fair then. Not when it comes to awarding certificates.
9th Mar 2021, 9:25 AM
Nassim Abed
Nassim Abed - avatar
0
import numpy as np 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()] x1 = np.array(X) y1 = np.array(y) X_T = x1.T XTX = np.dot(X_T, X) XTX_INV = np.linalg.inv(XTX) XTXINVXT = np.dot(XTX_INV, X_T) beta = np.dot(XTXINVXT, y) print(beta.round(2))
25th Sep 2022, 10:39 PM
romi kumar
romi kumar - avatar
23rd Nov 2022, 11:53 AM
Alfonso Larraz Geijo
Alfonso Larraz Geijo - avatar
0
import numpy as np 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()] x_arr=np.array(X) y=np.array(y) x_arr_t=x_arr.transpose() a=np.matmul(x_arr_t,x_arr) a_inv=np.linalg.inv(a) c=np.matmul(a_inv,x_arr_t) beta=np.matmul(c,y).round(2) print(beta)
23rd Jan 2023, 8:49 AM
Mustapha ouaniza
0
import numpy as np 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()] x_arr=np.array(X) y=np.array(y) x_arr_t=x_arr.transpose() # to multipy two arrays we can use matmul or dot functions and @ as an operator also a=x_arr_t @ x_arr a_inv=np.linalg.inv(a) c=np.dot(a_inv,x_arr_t) beta=np.matmul(c,y).round(2) print(beta)
23rd Jan 2023, 8:55 AM
Mustapha ouaniza