NumPy Least Squares
One of the methods of Python lstsq() is used to find the regression line of the known linear equation ax=b that best fits with this equation. This means you have to determine the line which properly shows the relationship between the x and y points if your data indicates that there is one. The line between both points is known as a regression line when it is used to find the least square through this equation, ax=b.
Let’s start learning the implementing style of the linalg.lstsq() function. First, we write the library name that we use in Python which is “numpy”. Then, we concatenate the linalg() function and concatenate the lstsq() function. The linalg() function means linear algebra. It is always used with the lstsq() function because it is a linear algebraic expression. After this, we pass the arguments in the function brackets.
Let’s understand the parameters of the linalg.lstsq() function:
point1: It is the coefficient matrix.
point2: This matrix or array contains dependent variables.
rcond: The datatype of it is float. The rcond ratio serves as a cut-off for smaller singular values of point_1. If a singular value is less than the rcond times the biggest singular element of point_1, it is considered zero when determining the rank.
In return, we get the least square of known variable x in the equation ax=b.
Let’s start implementing our first example of a least square method of the Python library, NumPy. First, we need a Python compiler so that we can code in it. Open the compiler. You also need to install the NumPy library because we are using one of the functions of NumPy which is the lstsq() function. Then, you have to import the NumPy package in it. First, write the keyword “import” which tells the compiler that we are going to import the package. Then, we have to write the package name which we use in the function which is “numpy”. And then, we also write the alternate name of the NumPy “np” because many programmers use this approach. This is a good programming approach, and it saves time.
After importing the package, we start writing the actual line of code which we want to do. We print the messages first so that the user can easily understand what we are doing in the example using the print() statement. We create the one-dimensional array “A” using the array() function and then print it by calling the print() statement. Then, we create another one-dimensional array “B” using the array() function and print it using the print() function.
After the creation of both points A and B, we implement the lstsq() function. But first, we use the vstack() function to stack the elements of “A”, sequence-wise. Then, we take the transpose of array “A”. Then, we pass the vstack() function as the first argument of the lstsq() function. The second argument is the “B” array and the third argument is “rcond” in which we set the value of rcond as “none”. Then, we store the whole function in another array named “x” which shows that it is the known variable linear equation, ax=b. After this, we display the results so we use the print() statement for this and pass the “x” array in it.
Now, let’s start implementing another example of NumPy least squares. We always import the library first which we use in the program which is NumPy. First, we write the keyword “import” to get the package in the program. We also write the package name which is “numpy” and then his alias, “np”. Then, we call the print() method so that we can display the retable message of the least squares for a better understanding of the user.
Then, we create the array name “x_axis” and store the array in it using the arange() function. Then, we print it using the print() method. Then, we create another array name “y_axis” and store the array in it which we created in the following illustration.
After creating both arrays, we implement the ones() method on the x_axis array and store it in another array named “array_a”. And then, we also print this array. We create another array named “arg_reg_line” and implement a linalg.lstsq() function on it. Then, we pass the parameters to this function so that we can get the least squares between two arrays or points. The first parameter is that we take the transpose of array_a. The second parameter is the second point which is the y_axis. Then, we have “rcond” which contains the “none” value. We then display the array using the print() method.
Here is the output of the previoulsy-implemented example:
We import another package of NumPy which is the “matplotlib” package which is used to plot the graph. Then, we plot the x_axis values and y_axis_values. Next, we set the title and labels of the graph. Lastly, we display the graph using the show() method.
Here is the desired graph of the given example:
In this article, we learned what is the least square and how we get the linalg.lstsq() of the unknown variable x using the linear equation ax=b. We used multiple functions of NumPy to find the least squares and implemented some examples with detailed explanations for a better understanding of the user.