How to perform least squares fitting in NumPy (with example)?

Least squares fitting in NumPy is a method of finding the best fit line or curve for a given set of data points. To do this in NumPy, you use the polyfit() function. This function takes two arrays as parameters, one containing the x-coordinates of the data points and the other containing the y-coordinates. It also requires a degree parameter, which is the degree of the polynomial you are trying to fit. For example, to fit a line to a set of data points, the degree would be 1 and to fit a parabola, the degree would be 2. The polyfit() function then returns an array containing the coefficients of the polynomial that best fits the data points.


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:

x