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

The Laguerre series is a set of orthogonal polynomials useful for data fitting. NumPy's laguerre.lagfit() method performs least squares fitting of Laguerre series to data points.

Syntax

laguerre.lagfit(x, y, deg, rcond=None, full=False, w=None)

Parameters

  • x ? x-coordinates of the sample points
  • y ? y-coordinates of the sample points
  • deg ? degree of the fitting polynomial
  • rcond ? relative condition number (optional)
  • full ? if True, returns diagnostic information (default: False)
  • w ? weights for each data point (optional)

Return Value

Returns Laguerre coefficients ordered from low to high. When full=True, also returns diagnostic statistics including residuals, rank, and singular values.

Example

Let's fit a Laguerre series to noisy cubic data ?

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

# Generate x-coordinates
x = np.linspace(-1, 1, 51)
print("X coordinates:")
print(x[:10])  # Show first 10 values

# Generate y-coordinates with noise
np.random.seed(42)  # For reproducible results
y = x**3 - x + np.random.randn(len(x)) * 0.1
print("\nY coordinates:")
print(y[:10])  # Show first 10 values
X coordinates:
[-1.   -0.96 -0.92 -0.88 -0.84 -0.8  -0.76 -0.72 -0.68 -0.64]

Y coordinates:
[ 0.04967142  0.90648896  0.64345327  0.32804837 -0.01532579  0.22336206
  0.54839625  0.59476765  0.47946752  0.31748958]

Basic Fitting

Fit a 3rd degree Laguerre series to the data ?

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

# Generate data
x = np.linspace(-1, 1, 51)
np.random.seed(42)
y = x**3 - x + np.random.randn(len(x)) * 0.1

# Fit Laguerre series (basic)
coefficients = L.lagfit(x, y, 3)
print("Laguerre coefficients:")
print(coefficients)
Laguerre coefficients:
[ 0.07334608 -1.0127893   1.04242468 -0.34214675]

Full Diagnostic Information

Use full=True to get detailed fitting statistics ?

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

# Generate data
x = np.linspace(-1, 1, 51)
np.random.seed(42)
y = x**3 - x + np.random.randn(len(x)) * 0.1

# Fit with full diagnostic information
coefficients, stats = L.lagfit(x, y, 3, full=True)

print("Coefficients:", coefficients)
print("\nDiagnostic information:")
print("Residuals:", stats[0])
print("Rank:", stats[1])
print("Singular values:", stats[2])
print("Condition number:", stats[3])
Coefficients: [ 0.07334608 -1.0127893   1.04242468 -0.34214675]

Diagnostic information:
Residuals: [0.46542832]
Rank: 4
Singular values: [1.60263347 0.56518678 0.08721395 0.00345394]
Condition number: 4.639431067018027e-15

Evaluating the Fit

Use the coefficients to evaluate the fitted polynomial ?

import numpy as np
from numpy.polynomial import laguerre as L
import matplotlib.pyplot as plt

# Generate and fit data
x = np.linspace(-1, 1, 51)
np.random.seed(42)
y = x**3 - x + np.random.randn(len(x)) * 0.1
coefficients = L.lagfit(x, y, 3)

# Evaluate the fitted polynomial
x_eval = np.linspace(-1, 1, 100)
y_fitted = L.lagval(x_eval, coefficients)

print("Original data points (first 5):")
for i in range(5):
    print(f"x={x[i]:.2f}, y={y[i]:.3f}")
    
print(f"\nFitted values at x=0: {L.lagval(0, coefficients):.3f}")
print(f"Fitted values at x=0.5: {L.lagval(0.5, coefficients):.3f}")
Original data points (first 5):
x=-1.00, y=0.050
x=-0.96, y=0.906
x=-0.92, y=0.643
x=-0.88, y=0.328
x=-0.84, y=-0.015

Fitted values at x=0: -0.049
Fitted values at x=0.5: -0.377

Key Points

  • Laguerre polynomials are orthogonal over [0, ?) with weight function e^(-x)
  • Higher degree fitting can capture more complex patterns but may overfit
  • The full=True parameter provides useful diagnostic information
  • Use laguerre.lagval() to evaluate the fitted polynomial

Conclusion

The laguerre.lagfit() method provides an effective way to fit Laguerre series to data using least squares. Use the full=True parameter to access diagnostic information for evaluating fit quality.

Updated on: 2026-03-26T20:27:20+05:30

357 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements