| by Arround The Web | No comments

## NumPy Least Squares

Today, we are going to learn about the least squares in linear equations and how to implement the least square method for the best fit for the regression line in the provided datasets. But before that, let’s get the basic knowledge of NumPy. NumPy is one of the best mathematical packages of Python that provides services for multi-dimensional arrays and matrices along with a wide range of complex numerical operations that may be performed on these matrices/arrays.

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.

## Syntax:

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.

## Parameters:

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.

## Return Value:

In return, we get the least square of known variable x in the equation ax=b.

### Example 1:

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.

import numpy as np

print("Implementation of Least Square Method in NumPy: ")

A = np.array([1,2,1,1,1,2,2,1,1])

print("\nThe Array A is: ", A)

B = np.array([4,3,5,4,2,3,6,3,2])

print("\nThe Array B is: ", B)

X = np.linalg.lstsq(np.vstack([A, np.ones(len(A))]).T, B, rcond=None)[0]

print("\nThe Least Square is: ", X)

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.

### Example 2:

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.

import numpy as np

print("Implementation of linalg.lstsq() function: ")

x_axis = np.arange(0, 10)

print("\nThe Value of x axis are: ", x_axis)

y_axis = [10.3, 10.5, 11, 11.5, 13.2, 13.9, 14, 15.5, 16.6, 17]

print("\nThe Value of y axis are: ", y_axis)

array_a = np.array([x_axis, np.ones(10)])

print("\nThe Array is: \n", array_a)

arg_reg_line = np.linalg.lstsq(array_a.T, y_axis, rcond=None)[0]

print("\nThe Parameters of Regrssion Line are: ", arg_reg_line)

reg_line = arg_reg_line[0] * x_axis + arg_reg_line[1]

import matplotlib.pyplot as plt

plt.plot(x_axis, reg_line, 'r-')

plt.plot(x_axis, y_axis, 'o')

plt.title("Linear Regression Line")

plt.xlabel("X-Axis")

plt.ylabel("Y-Axis")

plt.show()

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:

## Conclusion

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.

Source: linuxhint.com