Ordinary Squares project | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 11

Ordinary Squares project

I’m not understanding this project? It’s in DS with Python 4th module. It’s saying it should multiply by matrix multiplication but it shows me an error when I submit the code? 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 print(np.dot(y, X))

19th Dec 2020, 1:21 PM
BatOchir Artur
BatOchir Artur - avatar
30 Answers
+ 29
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=np.array(X).reshape(n,p) y=np.array(y) b=np.linalg.pinv(X) @ y.transpose() print(np.around(b,decimals=2))
5th Mar 2021, 7:46 PM
Amir Mehrabi Jorshari
+ 19
I dont see how this project is relevant to the module, it’s well beyond the information covered. Does’t make sense at all. I had a couple of challenging ones in the past but this I would have never solved wihtout peeking in the comments.
23rd Jul 2021, 10:56 AM
Vasyl
+ 12
Robson Marini Instead of doing the calculations explicitly, there's a command to calculate Penrose Inverse directly in numpy. Just use pinv instead of inv. beta=np.linalg.pinv(X) @ y.T will give answer directly. Here's mine: https://code.sololearn.com/cN5LXGeLIYWg/?ref=app
26th Dec 2020, 7:26 AM
Pranjal Tambe
Pranjal Tambe - avatar
+ 4
Zhenis Otarbay Robson Marini binary disorder: y_true = [int(x) for x in input().split()] y_pred = [int(x) for x in input().split()] from sklearn.metrics import confusion_matrix import numpy as np s =confusion_matrix(y_true, y_pred) a=np.flip(s) s[0][0], s[-1][-1]=s[-1][-1], s[0][0] s=s.astype(float) print(s)
22nd Dec 2020, 3:04 PM
BatOchir Artur
BatOchir Artur - avatar
+ 4
The exercise is horribly explained, refers to an image that is not shown, a formula that is not shown and requires instruments not mentioned in the course.
4th Feb 2022, 9:15 AM
Paolo
+ 3
import numpy as np n, p = [int(x) for x in input().split()] X = np.empty([n, p]) for i in range(n): X[i,] = input().split() y = [float(x) for x in input().split()] Xarr = np.array(X) yarr = np.array([y]).T # beta = inv(Xarr.T * Xarr) * Xarr.T * yarr # beta = temp1 * Xarr.T * yarr # beta = temp2 * yarr # temp1 = inv(Xarr.T * Xarr) # temp2 = temp1 * Xarr.T temp1 = np.linalg.inv(np.matmul(Xarr.T, Xarr)) temp2 = np.matmul(temp1, Xarr.T) beta = np.matmul(temp2, yarr) print(beta.flatten().round(2))
22nd Dec 2020, 2:57 PM
Robson Marini
Robson Marini - avatar
+ 3
Bat-Ochir Artur Robson Marini As I had seen this thread earlier, and just completed Binary disorder, I would like to add something. In Binary disorder the default labeling starts from 0 in confusion matrix, but as 1 is True and 0 is False in python, we could just use them as labels to get the output directly, and we don't have to rotate or flip the matrix. import numpy as np from sklearn.metrics import confusion_matrix as cm y_true = [int(x) for x in input().split()] y_pred = [int(x) for x in input().split()] yt=np.array(y_true) yp=np.array(y_pred) print(cm(yp,yt,labels=[True,False]).astype(float)) This solves the problem https://code.sololearn.com/c6f3Gd2zUmaJ/?ref=app
26th Dec 2020, 2:54 PM
Pranjal Tambe
Pranjal Tambe - avatar
+ 2
Bat-Ochir Artur Robson Marini could you find out? I am also stack btw
22nd Dec 2020, 2:50 PM
Zhenis Otarbay
Zhenis Otarbay - avatar
+ 2
import numpy as np n, p = [int(x) for x in input().split()] X = np.empty([n, p]) for i in range(n): X[i,] = input().split() y = [float(x) for x in input().split()] X_arr = np.array(X) y_arr = np.array([y]).T temp_1 = np.linalg.inv(np.matmul(X_arr.T, X_arr)) temp_2 = np.matmul(temp_1, X_arr.T) Abdullah = np.matmul(temp_2, y_arr) print(Abdullah.flatten().round(2))
5th Mar 2021, 5:39 PM
Abdullah Abdelhakeem
Abdullah Abdelhakeem - avatar
+ 2
You only need to add this line to the code : print((np.linalg.pinv(X) @ y).round(2)) ############################### Formula: XB = y ----> B = X^-1 . y where: B: beta ^-1: inverse . : matrix multiplication
17th Mar 2022, 6:13 PM
Behrooz Ostadaghaee
Behrooz Ostadaghaee - avatar
+ 1
help please
19th Dec 2020, 1:42 PM
BatOchir Artur
BatOchir Artur - avatar
+ 1
Dude. I am trying to solve this one too. My code passes the first test but it doesn't pass the other tests. The equation that solves beta is: beta = (X^T . X)^(-1) . X^T . y where: ^T = transpose ^(-1) = inverse https://en.wikipedia.org/wiki/Ordinary_least_squares However, there is something really weird on the second test. "y" has 3 elements, but X is a 2 by 2 matrix. So "y" should have only 2 elements as well. I don't know if there is any hidden trick here, or if it is a sololearn error. Because "y" should have only 2 elements and not 3.
21st Dec 2020, 10:08 PM
Robson Marini
Robson Marini - avatar
+ 1
After my complaint, I think Sololearn solved the "Test 2" input error. X was 2x2 and y was 3x1. A clear matching mistake. Now it is correct. And here is my code that passed all tests. I hope it helps.
22nd Dec 2020, 2:56 PM
Robson Marini
Robson Marini - avatar
+ 1
Robson Marini thanks bro
22nd Dec 2020, 2:58 PM
Zhenis Otarbay
Zhenis Otarbay - avatar
+ 1
Robson Marini could you solve binary disorder?
22nd Dec 2020, 2:58 PM
Zhenis Otarbay
Zhenis Otarbay - avatar
+ 1
Didn't get there. But I ll try that one soon.
22nd Dec 2020, 2:59 PM
Robson Marini
Robson Marini - avatar
+ 1
Bat-Ochir Artur thanks bro, now I understand it, did you understand welcome to the matrix in machine learning? I can't pass the last testing case
22nd Dec 2020, 6:10 PM
Zhenis Otarbay
Zhenis Otarbay - avatar
+ 1
Didn't do "welcome to the matrix" yet. Bit I just did "Binary Disorder". import numpy as np y_true = [int(x) for x in input().split()] y_pred = [int(x) for x in input().split()] TP, FP, FN, TN = 0, 0, 0, 0 for i in range(len(y_true)): if y_true[i] == 1 and y_pred[i] == 1: TP += 1.0 elif y_true[i] == 1 and y_pred[i] == 0: FN += 1.0 elif y_true[i] == 0 and y_pred[i] == 0: TN += 1.0 elif y_true[i] == 0 and y_pred[i] == 1: FP += 1.0 cm = np.array([[TP, FP],[FN, TN]]) print(cm)
22nd Dec 2020, 6:13 PM
Robson Marini
Robson Marini - avatar
+ 1
Robson Marini thanks bro plethora of useful tips
22nd Dec 2020, 6:15 PM
Zhenis Otarbay
Zhenis Otarbay - avatar
+ 1
Robson Marini also i am stack in Split to Achieve Gain in ML
22nd Dec 2020, 6:15 PM
Zhenis Otarbay
Zhenis Otarbay - avatar