# Get the Least-squares fit of a polynomial to data in Python

PythonNumpyServer Side ProgrammingProgramming

To get the least-squares fit of a polynomial to data, use the polynomial.polyfit() in Python Numpy. The method returns the Polynomial coefficients ordered from low to high. If y was 2-D, the coefficients in column k of coef represent the polynomial fit to the data in y’s k-th column. The parameter, x are the x-coordinates of the M sample (data) points (x[i], y[i]).

The parameter, y are the y-coordinates of the sample points. Several sets of sample points sharing the same x-coordinates can be (independently) fit with one call to polyfit by passing in for y a 2-D array that contains one data set per column. The parameter, deg is the Degree(s) of the fitting polynomials. If deg is a single integer all terms up to and including the deg’th term are included in the fit

The parameter, rcond is the relative condition number of the fit. Singular values smaller than rcond, relative to the largest singular value, will be ignored. The default value is len(x)*eps, where eps is the relative precision of the platform’s float type, about 2e-16 in most cases. The parameter, full is the switch determining the nature of the return value. When False (the default) just the coefficients are returned; when True, diagnostic information from the singular value decomposition is also returned.

The parameter, w are the weights. If not None, the weight w[i] applies to the unsquared residual y[i] - y_hat[i] at x[i]. Ideally the weights are chosen so that the errors of the products w[i]*y[i] all have the same variance. When using inverse-variance weighting, use w[i] = 1/sigma(y[i]). The default value is None.

## Steps

At first, import the required libraries −

import numpy as np
from numpy.polynomial import polynomial as P

The x-coordinate −

x = np.linspace(-1,1,51)


Display the x-coordinate −

print("X Co-ordinate...\n",x)

The y-coordinate −

y = x**3 - x + np.random.randn(len(x))
print("\nY Co-ordinate...\n",y)

To get the least-squares fit of a polynomial to data, use the polynomial.polyfit() in Python Numpy −

c, stats = P.polyfit(x,y,3,full=True)
print("\nResult...\n",c)
print("\nResult...\n",stats)

## Example

import numpy as np
from numpy.polynomial import polynomial as P

# The x-coordinate
x = np.linspace(-1,1,51)

# Display the x-coordinate
print("X Co-ordinate...\n",x)

# The y-coordinate
y = x**3 - x + np.random.randn(len(x))
print("\nY Co-ordinate...\n",y)

# To get the least-squares fit of a polynomial to data, use the polynomial.polyfit() in Python Numpy

# The method returns the Polynomial coefficients ordered from low to high. If y was 2-D, the coefficients in column k of coef represent the polynomial fit to the data in y’s k-th column.
c, stats = P.polyfit(x,y,3,full=True)
print("\nResult...\n",c)
print("\nResult...\n",stats)

## Output

X Co-ordinate...
[-1. -0.96 -0.92 -0.88 -0.84 -0.8 -0.76 -0.72 -0.68 -0.64 -0.6 -0.56
-0.52 -0.48 -0.44 -0.4 -0.36 -0.32 -0.28 -0.24 -0.2 -0.16 -0.12 -0.08
-0.04 0. 0.04 0.08 0.12 0.16 0.2 0.24 0.28 0.32 0.36 0.4
0.44 0.48 0.52 0.56 0.6 0.64 0.68 0.72 0.76 0.8 0.84 0.88
0.92 0.96 1. ]

Y Co-ordinate...
[ 0.24282445 -0.48320773 -1.18068109 -0.48486683 0.80514762 0.48976259
0.31363813 -0.03382685 -0.92334611 0.86835062 0.24661201 0.9790978
0.03782101 0.12213365 -1.37248029 1.99891304 -0.09825977 1.74524931
0.70560858 0.15516069 0.69169705 0.76957712 -1.21919676 1.50064825
1.32101339 -2.51479992 -0.28998783 -1.24293076 0.45927699 -0.53484746
0.50455341 -0.06351788 -2.69585303 -0.46833578 1.4924168 -2.42374146
-1.91934499 -1.36311466 -1.23946547 -1.56822005 -0.79648036 1.58269324
-0.53682862 -0.90861958 -0.28174461 -0.10775622 0.58535687 1.06373501
-2.28991738 2.01597286 -0.75841069]

Result...
[-0.17198829 -1.84107674 0.09439374 2.39030912]

Result...
[array([60.43653521]), 4, array([1.38446749, 1.32119158, 0.50443316, 0.28853036]), 1.1324274851176597e-14]
Updated on 28-Feb-2022 11:09:41