Table of Contents
Least squares fitting is a statistical method used to find the best fitting line for a set of data points. This method is commonly used in data analysis and machine learning. In NumPy, the “polyfit” function can be used to perform least squares fitting by finding the optimal coefficients for a polynomial equation that best fits the given data. An example of using the “polyfit” function in NumPy is as follows:
import numpy as np
x = np.array([1, 2, 3, 4, 5])
y = np.array([3, 5, 7, 9, 11])
# fit a linear least squares line to the data
coefficients = np.polyfit(x, y, 1)
# print the optimal coefficients
print(coefficients)
# output: [2. 1.]
This output indicates that the best fitting line for the given data is y = 2x + 1. In this way, NumPy provides a simple and efficient method for performing least squares fitting.
Perform Least Squares Fitting in NumPy (With Example)
The method of least squares is a method we can use to find the regression line that best fits a given dataset.
We can use the linalg.lstsq() function in NumPy to perform least squares fitting.
The following step-by-step example shows how to use this function in practice.
Step 1: Enter the Values for X and Y
First, let’s create the following NumPy arrays:
import numpy as np #define x and y arrays x = np.array([6, 7, 7, 8, 12, 14, 15, 16, 16, 19]) y = np.array([14, 15, 15, 17, 18, 18, 19, 24, 25, 29])
Step 2: Perform Least Squares Fitting
We can use the following code to perform least squares fitting and find the line that best “fits” the data:
#perform least squares fitting np.linalg.lstsq(np.vstack([x, np.ones(len(x))]).T, y, rcond=None)[0] array([0.96938776, 7.76734694])
The result is an array that contains the slope and intercept values for the line of best fit.
From the output we can see:
- Slope: 0.969
- Intercept: 7.767
Using these two values, we can write the equation for the line of best fit:
ŷ = 7.767 + 0.969x
Step 3: Interpret the Results
Here’s how to interpret the line of best fit:
- When x is equal to 0, the average value for y is 7.767.
- For each one unit increase in x, y increases by an average of .969.
For example, if x has a value of 10 then we predict that the value of y would be 17.457:
- ŷ = 7.767 + 0.969x
- ŷ = 7.767 + 0.969(10)
- ŷ = 17.457
Bonus: Video Explanation of Least Squares Fitting
Refer to the video below for a simple explanation of least squares fitting:
The following tutorials explain how to perform other common tasks in NumPy:
Cite this article
stats writer (2024). How can I perform least squares fitting in NumPy? Can you provide an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-perform-least-squares-fitting-in-numpy-can-you-provide-an-example/
stats writer. "How can I perform least squares fitting in NumPy? Can you provide an example?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-perform-least-squares-fitting-in-numpy-can-you-provide-an-example/.
stats writer. "How can I perform least squares fitting in NumPy? Can you provide an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-perform-least-squares-fitting-in-numpy-can-you-provide-an-example/.
stats writer (2024) 'How can I perform least squares fitting in NumPy? Can you provide an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-perform-least-squares-fitting-in-numpy-can-you-provide-an-example/.
[1] stats writer, "How can I perform least squares fitting in NumPy? Can you provide an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I perform least squares fitting in NumPy? Can you provide an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
