Get the Least squares fit of Legendre series to data in Python


To get the Least squares fit of Legendre series to data, use the legendre.legfit() method in Python numpy. The method returns the Legendre coefficients ordered from low to high. If y was 2-D, the coefficients for the data in column k of y are in column k.

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 xcoordinates 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 library −

import numpy as np
from numpy.polynomial import legendre as L

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 Legendre series to data, use the legendre.legfit() method in Python numpy. The method returns the Legendre coefficients ordered from low to high. If y was 2-D, the coefficients for the data in column k of y are in column k −

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

Example

import numpy as np
from numpy.polynomial import legendre as L

# 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 Legendre series to data, use the legendre.legfit() method in Python numpy
c, stats = L.legfit(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...
  [-5.28795520e-02 -7.61252904e-03  7.35194215e-02 -1.33072588e-01
   -1.21785636e+00  7.75679385e-02  6.55168668e-01  1.42872448e+00
    8.42326214e-01  2.49667989e+00  9.58942508e-01 -2.67332869e-01
   -7.85575928e-01  1.93333045e+00  7.32492468e-01  5.23576961e-01
   -1.91529521e+00 -1.41434385e+00  4.44787373e-01  3.81831261e-01
    3.74128321e-01  1.20562789e+00  1.44870029e+00  1.01091575e-03
    8.94334713e-01  1.22342199e+00  9.52055370e-01 -7.29520012e-01
   -2.42648820e-01 -9.78434555e-02  1.27468237e-01  9.39489448e-01
    1.08795136e+00  2.31230197e+00  1.93107556e-02 -6.13335407e-01
    1.93170835e-01 -8.77958854e-01 -3.59868085e-01  4.31331759e-01
    7.24929856e-01 -2.22736540e-01 -1.29623093e+00  4.13226024e-01
    7.82155644e-01 -1.56618537e-01  1.25043737e+00  6.32386988e-01
   -2.75716271e-01  8.80669895e-02 -3.20225560e-01]

Result...
 [ 0.29249467 -0.10521942 -0.24847572 0.2010877 ]

Result...
 [array([39.35467561]), 4, array([1.0425003 , 1.02126704, 0.97827074, 0.95561139]), 1.1324274851176597e-14]

Updated on: 10-Mar-2022

359 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements