Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
  • Last Updated : 03 Jun, 2022

In this article, we will cover how to get the Least-squares fit of the Chebyshev series to data in Python.

chebyshev.chebfit method

The NumPy library provides us numpy.polynomial.chebyshev.chebfit() method to get the Least-squares fit of the Chebyshev series to data in python. The method returns the coefficients of a degree Chebyshev series that is the best fit (least square fit) to the data values y at positions x. If y is one-dimensional, the coefficients returned will be one-dimensional as well. If y is 2-D, multiple fits are performed, per each column of y, and the coefficients are placed in the corresponding columns of the 2D return.

The polynomial(s) that have been fitted are of the form:

p(x) = C0+ C1.T1(x)+........+Cn.Tn(x)

Syntax: polynomial.chebyshev.chebfit(x, y, deg, full=False)

Parameters:

  • x: x-coordinates points
  • y: y-coordinates points
  • deg: Degree(s) of the fitting polynomials.
  • full: bool, (optional) Switch determining nature of return value. When it is False (the default) just the coefficients are returned.

Returns: coefficient matrix in the least-squares fit.

[residuals, rank, singular_values, rcond]: 

If full == True, these values are returned.

  • residuals – the total of the least squares fit’s squared residuals.
  • rank — the scaled Vandermonde matrix’s numerical rank.
  • singular values – singular values of the scaled Vandermonde matrix.
  • rcond — rcond’s value.

Example 1:

Here, we will create a NumPy array using np.linspace() for the x-coordinate and y-coordinate functions. After that numpy.polynomial.chebyshev.chebfit() method is used to find the least-squares fit of the Chebyshev series. This method returns coefficients and statistics only when the full parameter is set to true. statistics contain residuals, rank, singular_values, and rcond.

Python3




# import packages
import numpy as np
from numpy.polynomial import chebyshev as C
  
# X- coordinate
x = np.linspace(0, 1, 25)
print(x)
  
# y - coordinate computed from x-coordinate
y = x**3 - x**2 + np.random.randn(len(x))
print(y)
  
# least square fit of chebyshev series
c, stats = C.chebfit(x, y, 2, full=True)
  
print('coefficients are :'+str(c))
print('residuals '+str(stats[0]))
print('rank :'+str(stats[1]))
print('singular_values :'+str(stats[2]))
print('rcond: '+str(stats[3]))

Output:

[0.         0.04166667 0.08333333 0.125      0.16666667 0.20833333

 0.25       0.29166667 0.33333333 0.375      0.41666667 0.45833333

 0.5        0.54166667 0.58333333 0.625      0.66666667 0.70833333

 0.75       0.79166667 0.83333333 0.875      0.91666667 0.95833333

 1.        ]

[ 0.37409044 -0.08380424 -0.86646175  0.03529103 -0.51223158 -0.82765727

 -1.30289843  0.06483149  0.00468366 -0.94344187  0.79565795  0.4189812

  1.11198273 -1.82178502 -1.34997261 -1.4128537  -0.07464673  0.15374686

  0.48787202 -0.46500345 -0.8149045   1.49261006 -1.94050386 -0.41054362

  0.20983018]

coefficients are :[ 0.17746311 -0.73510349  0.36625023]

residuals [18.52977625]

rank :3

singular_values :[1.39668318 1.02095653 0.08320976]

rcond: 5.551115123125783e-15

Example 2:

In this example, parameter full is set to false. statistics are not returned when it is false, only coefficients are returned.

Python3




# import packages
import numpy as np
from numpy.polynomial import chebyshev as C
  
# X- coordinate
x = np.linspace(0, 1, 25)
print(x)
  
# y - coordinate computed from x-coordinate
y = x**3 - x**2 + np.random.randn(len(x))
print(y)
  
# least square fit of chebyshev series
c = C.chebfit(x, y, 2, full=False)
  
print('coefficients are :'+str(c))

Output:

[0.         0.04166667 0.08333333 0.125      0.16666667 0.20833333

 0.25       0.29166667 0.33333333 0.375      0.41666667 0.45833333

 0.5        0.54166667 0.58333333 0.625      0.66666667 0.70833333

 0.75       0.79166667 0.83333333 0.875      0.91666667 0.95833333

 1.        ]

[ 1.43124921  0.704068    0.87329216 -1.89762515 -1.00132009 -0.07043263

 -0.52919039 -0.35211855  0.16805591  0.21070363 -0.54878338 -0.05096546

 -1.86555805 -0.35063789 -2.46754197 -0.7162462   0.21864938 -0.25926418

 -1.39237896  1.49328312  0.04526993 -0.76624966 -1.36429022 -0.16418669

 -1.05438407]

coefficients are :[ 1.94042826 -3.72505731  1.45929506]


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!