Question: using Python Data Science - Ordinary Squares Ordinary least squares for linear regression. Ordinary least squares (OLS) is a method to estimate the parameters? in
using Python
Data Science - Ordinary Squares
Ordinary least squares for linear regression.
Ordinary least squares (OLS) is a method to estimate the parameters? in a simple linear regression, X? = y, where X is the featurematrix and y is the dependent variable (or target), by minimizingthe sum of the squares of the differences between the observeddependent variable in the given dataset and those predicted by thelinear function. Mathematically, the solution is given by theformula in the image, where the superscript T means the transposeof a matrix, and the superscript -1 means it is an inverse of amatrix.
Task
Given a 2D array feature matrix X and a vector y, return thecoefficient vector; see the formula.
Input Format
First line: two integers separated by spaces, the first indicatesthe rows of the feature matrix X (n) and the second indicates thecolumns of X (p)
Next n lines: values of the row in the feature matrix
Last line: p values of target y
Output Format
An numpy 1d array of values rounded to the second decimal.
Sample Input
2 2
1 0
0 2
2 3
Sample Output
[2. , 1.5]
Explanation
The feature matrix X has n = 2 rows and p = 2 features. Followingthe OLS solution, substitute X by np.array([[1. 0.], [0. 2.]] and yby np.array([2. 3.]), respectively, performing the matrixmultiplication using tools in numpy results in np.array([2.1.5]).
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
