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

Ordinary Squares project - image missing

Hello everyone, I'm working on the DS with python course and I am currently a bit confused about the project in the Linear Regression module. In the task description it says " Mathematically, the solution is given by the formula in the image, where the superscript T means the transpose of a matrix, and the superscript -1 means it is an inverse of a matrix. " My problem is not the code or the maths, I simply can't find any image with the formula that is referred to here. It's just not there. Can someone provide the image for me?

2nd Jan 2021, 11:30 AM
Alexander Peeters
Alexander Peeters - avatar
7 Answers
+ 10
I am also struggling to understand the language of these projects. I have no problem understanding the lessons and the exercises and answering the quiz questions, but I don't understand exactly what they want in the projects. When I come over the forums asking to explain the questions, I get someone handing me the code solution, which is not good for learning.
9th Mar 2021, 8:05 AM
Nassim Abed
Nassim Abed - avatar
+ 8
I couldn't find where this text is, by reading the text I imagine the formula it's referring to is the one in the link https://en.m.wikipedia.org/wiki/Ordinary_least_squares in Linear Models under Matrix/vector formulation.
11th Jan 2021, 12:59 PM
Sandra Meneses
Sandra Meneses - avatar
+ 4
You have this equation: y = X*β, (1) where X is a matrix (n, p), β and p are vectors with p and y components, respctivelly. Here '*' denotes multiplications. The basic code gives you X and y. Your goal is to return β. You have to solve Eq. (1) for β, so you know what code to write. First, multiply Eq. (1) by X^T, which is the transpose [1] of X: (X^T)*y = (X^T)*X*β. (2) Now, multiply by the inverse [2] of the (X^T)*X, which is ((X^T)*X)^(-1): (((X^T)*X)^(-1))*(X^T)*y = (((X^T)*X)^(-1))*(X^T)*X*β, (3) leading to β = (((X^T)*X)^(-1))*(X^T)*y. (4) Thus, you need to find: - X^T; You can write code to do it or one can simply use the function np.transpose(X) or np.array(X).T [3]: X^T = np.array(X).T; (5) - ((X^T)*X); When multiplying matrices we cannot simply use the '*' operator as we use in numbers (scalars). Matrices multiply a little differently [4]. If you want you can do this by hand or you could use another wonderful numpy function, which is the np.matmul(x1, x2) [5], where x1 and x2 are the matrices you want to multiply: ((X^T)*X) = np.matmul(X^T, X) (6) ((X^T)*X)^(-1); You already have the matrix (X^T)*X, all you have to do is simply calculate its inverse. Again, matrices are a bit different than scalars, so there is a rule to do that [5]. You can write a code to do it or you could use the numpy function np.linalg.inv() [6]: ((X^T)*X)^(-1) = np.linalg.inv((X^T)*X) (7) After that you just have to multiply everything, i.e., multiply, in order, Eqs. (7) and (6) by the vecor y. Again, remember that when multiplying matrices the order matters, so multiply the factors as they appear in Eq. (4). I hope this helps! :) References [1] https://3c5.com/SDJHH [2] https://3c5.com/VDAIJ [3] https://3c5.com/XISlQ [4] https://3c5.com/BnYCl [5] https://3c5.com/PiWXz [6] https://3c5.com/cNEZy P.s.: I had to use a URL shortener so it could fit everything in one comment.
10th Dec 2021, 6:55 PM
dsvieira - avatar
+ 2
I came here for the same problem.
8th Feb 2021, 6:53 PM
Shannon Negaard-Paper
Shannon Negaard-Paper - avatar
+ 1
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))
14th Sep 2021, 7:39 AM
Brigido bergado jr
Brigido bergado jr - avatar
this is the formula missing from the description β = (((X^T)*X)^(-1))*(X^T)*y
5th Jan 2022, 3:05 PM
Salem - avatar
- 1
Copy my code and like check
14th Sep 2021, 7:40 AM
Brigido bergado jr
Brigido bergado jr - avatar