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

PythonNumpyServer Side ProgrammingProgramming

To get the Least squares fit of Laguerre series to data, use the laguerre.lagfit() method in Python numpy. The method returns the Laguerre 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 laguerre 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 Laguerre series to data, use the laguerre.lagfit() method in Python numpy. The method returns the Laguerre 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.lagfit(x,y,3,full=True)
print("\nResult...\n",c)
print("\nResult...\n",stats)

Example

import numpy as np
from numpy.polynomial import laguerre 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 Laguerre series to data, use the laguerre.lagfit() method in Python numpy
c, stats = L.lagfit(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...
   [ 2.60011413  0.59715605  1.38401537 -1.76702116 -1.48948207  0.19627462
     0.6350364   0.41990937 -0.72067571  0.07617042  0.33693761  1.08876378
     0.71283482  1.36064396  0.55285081  1.94847732  1.14871192 -0.26605826
    -1.18954961  1.15875553  0.30059389 -0.91705656  1.27988081 -0.42751846
     0.44466317 -1.41118489  0.31492152  0.70787202 -0.85295102 -0.45038585
    -2.05583591 -0.0799937  -1.13000262  0.09813804 -0.33068455  0.03329552
    -0.7666786  -0.9596926  -0.72177629 -0.62779169 -0.75490363 -0.7826376
    -2.26888118  1.1356559  -0.39593627  0.02709962 -0.95303898 -0.01582218
     0.65609447  1.43566953  1.10442549]

Result...
   [ 11.2805293 -36.35804353 36.47911284 -11.65554029]

Result...
   [array([43.46828156]), 4, array([1.88377481, 0.66402594, 0.10220349, 0.00405509]), 1.1324274851176597e-14]
raja
Updated on 04-Mar-2022 06:09:59

Advertisements