+ 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))
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))
+ 20
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.
+ 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
+ 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)
+ 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.
+ 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))
+ 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
+ 2
Bat-Ochir Artur Robson Marini could you find out? I am also stack btw
+ 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))
+ 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
+ 1
help please
+ 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.
+ 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.
+ 1
Robson Marini thanks bro
+ 1
Robson Marini could you solve binary disorder?
+ 1
Didn't get there. But I ll try that one soon.
+ 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
+ 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)
+ 1
Robson Marini thanks bro plethora of useful tips
+ 1
Robson Marini also i am stack in Split to Achieve Gain in ML