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

To get the least-squares fit of Chebyshev series to data, use the chebyshev.chebfit() function in NumPy. This method returns Chebyshev coefficients ordered from low to high, allowing you to fit polynomial approximations to your data using Chebyshev polynomials.

Syntax

numpy.polynomial.chebyshev.chebfit(x, y, deg, rcond=None, full=False, w=None)

Parameters

The function accepts the following parameters:

  • x ? The x-coordinates of the M sample points (x[i], y[i])
  • y ? The y-coordinates of the sample points. Can be 2-D array for multiple data sets
  • deg ? Degree(s) of the fitting polynomials. If integer, includes all terms up to deg-th term
  • rcond ? Relative condition number of the fit. Default is len(x)*eps (about 2e-16)
  • full ? When False (default), returns only coefficients. When True, returns diagnostic information
  • w ? Optional weights for data points. Use w[i] = 1/sigma(y[i]) for inverse-variance weighting

Example

Let's fit a Chebyshev series to noisy cubic data :

import numpy as np
from numpy.polynomial import chebyshev as C

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

# Create y-coordinates with noise (cubic function)
y = x**3 - x + np.random.randn(len(x)) * 0.1
print("\nY coordinates:")
print(y)

# Fit 3rd degree Chebyshev polynomial
coefficients, stats = C.chebfit(x, y, 3, full=True)

print("\nChebyshev coefficients:")
print(coefficients)
print("\nDiagnostic information:")
print(stats)
X coordinates:
[-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 coordinates:
[ 0.03419312 -0.91816653 -0.77946982 -0.55274981 -0.45519849 -0.39041167
 -0.52721947 -0.18851877 -0.24966675 -0.27534729 -0.36401346 -0.28471247
 -0.37482757 -0.37129476 -0.30473896 -0.25015476 -0.25024146 -0.12892016
 -0.08473899 -0.15644077 -0.03830883 -0.00434473 -0.09742086  0.07071358
  0.00925897 -0.00264845  0.11648076  0.04881842  0.10485881  0.20651769
  0.07647468  0.21967639  0.17962815  0.33456177  0.31816991  0.45628134
  0.41966711  0.50485938  0.59329866  0.59949081  0.72628568  0.72983329
  0.94188779  0.92883796  1.17838951  1.28089766  1.51298854  1.73048086
  1.86949378  2.11473651  2.02471645]

Chebyshev coefficients:
[-0.00506076 -1.00221436 -0.03522471  0.74985134]

Diagnostic information:
[array([0.14782616]), 4, array([7.14142843, 7.07106781, 4.52154136, 4.12310563]), 1.4210854715202004e-14]

Using the Fitted Polynomial

Once you have the coefficients, you can evaluate the fitted Chebyshev polynomial :

import numpy as np
from numpy.polynomial import chebyshev as C

# Generate sample data
x = np.linspace(-1, 1, 11)
y = x**2 + 0.1 * np.random.randn(len(x))

# Fit 2nd degree Chebyshev polynomial
coefficients = C.chebfit(x, y, 2)
print("Coefficients:", coefficients)

# Evaluate the fitted polynomial
x_new = np.array([-0.5, 0, 0.5])
y_fitted = C.chebval(x_new, coefficients)
print("Original x:", x_new)
print("Fitted y:", y_fitted)
Coefficients: [0.50094391 0.01154779 0.49890947]
Original x: [-0.5  0.   0.5]
Fitted y: [0.25094558 0.50094391 0.25094558]

Conclusion

The chebyshev.chebfit() function provides an efficient way to fit Chebyshev polynomial series to data. Use the full=True parameter to get diagnostic information about the fit quality, and chebval() to evaluate the fitted polynomial at new points.

Updated on: 2026-03-26T19:51:00+05:30

686 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements