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 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.

Syntax

numpy.polynomial.legendre.legfit(x, y, deg, rcond=None, full=False, w=None)

Parameters

x ? The x-coordinates of the M sample (data) points (x[i], y[i]).

y ? 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.

deg ? 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.

rcond ? 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.

full ? Switch determining the nature of the return value. When False (default) just the coefficients are returned; when True, diagnostic information is also returned.

w ? Weights. If not None, the weight w[i] applies to the unsquared residual y[i] - y_hat[i] at x[i]. When using inverse-variance weighting, use w[i] = 1/sigma(y[i]).

Example

Let's create sample data and fit a Legendre polynomial to it ?

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

# Create x-coordinates
x = np.linspace(-1, 1, 51)
print("X Co-ordinate...")
print(x)

# Create y-coordinates with some noise
y = x**3 - x + np.random.randn(len(x))
print("\nY Co-ordinate...")
print(y)

# Fit Legendre polynomial of degree 3
c, stats = L.legfit(x, y, 3, full=True)
print("\nCoefficients...")
print(c)
print("\nDiagnostic information...")
print(stats)
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.37454012 -1.38706515 -1.19034044 -0.6748472   0.28615649  2.08159077
  0.22524325 -0.56536842  0.70622851 -1.03395302 -0.22338056 -0.45805334
 -1.94290313  0.61346067  0.16334851 -1.66043086 -0.35529757 -0.06870081
 -0.15140628  0.50958089  1.61970319 -0.84582301  0.63167239 -0.49893432
  1.06633853  0.93589539 -0.48336772 -0.34074081  0.25892104 -0.83159577
  0.15615877  2.48097842  2.16468863 -0.12893951  0.01866316  0.86667641
 -0.74654119  1.51398859  1.19876948 -0.95659026 -0.96583598 -0.29695008
  0.17473996 -0.57725829  1.13609527 -0.49853633 -0.1825309   0.14127717
  1.08871108 -1.2030244   0.37454012]

Coefficients...
[ 0.1289584  -0.04235284 -0.20089677  0.20107306]

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

Understanding the Results

The coefficients array contains the Legendre polynomial coefficients. The diagnostic information includes residuals, rank, singular values, and rcond value when full=True is used.

Simple Example Without Noise

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

# Simple data points
x = np.array([-1, 0, 1])
y = np.array([0, 1, 0])

# Fit degree 2 polynomial
coefficients = L.legfit(x, y, 2)
print("Coefficients:", coefficients)

# Verify the fit
fitted_y = L.legval(x, coefficients)
print("Original y:", y)
print("Fitted y:", fitted_y)
Coefficients: [ 0.5  0.  -0.5]
Original y: [0 1 0]
Fitted y: [2.77555756e-17 1.00000000e+00 0.00000000e+00]

Conclusion

The legendre.legfit() method provides an efficient way to fit Legendre polynomials to data using least squares. Use full=True to get diagnostic information about the fit quality.

Updated on: 2026-03-26T21:14:33+05:30

767 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements