- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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() in Python Numpy. The method returns the Chebyshev 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 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. 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 chebyshev as C

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 Chebyshev series to data, use the chebyshev.chebfit() in Python Numpy. The method returns the Chebyshev 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 = C.chebfit(x,y,3,full=True) print("\nResult...\n",c) print("\nResult...\n",stats)

## Example

import numpy as np from numpy.polynomial import chebyshev as C # 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 Chebyshev series to data, use the chebyshev.chebfit() in Python Numpy c, stats = C.chebfit(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... [ 0.04578661 -0.41009751 -0.59839355 -0.86942574 1.19418042 -0.53671972 -0.71247683 0.7118818 -0.09274183 1.46114141 -0.40189463 -0.84017206 -1.00618725 -0.7191427 -0.48005631 -0.28661328 0.58161734 2.62382626 -0.56256678 0.92925678 1.68074305 0.97381262 1.22568804 1.71884192 1.03080843 0.55990935 0.29117168 -0.63718482 0.49396313 -0.32920431 1.16682261 0.90746863 -1.0058597 0.54972961 -1.06040041 -0.11828954 -0.51446299 -1.97932024 -0.91902371 -0.31859977 -1.16124938 0.31809796 0.54940462 -1.11008331 1.04918751 -2.60742632 -1.07242746 0.54313779 -0.3440979 -0.28234564 0.46429998] Result... [-0.12730537 -0.08699379 -0.4211565 0.32959334] Result... [array([43.34485511]), 4, array([1.20144978, 1.19227163, 0.76058422, 0.74600162]), 1.1324274851176597e-14]

- Related Questions & Answers
- Get the Least squares fit of Hermite series to data in Python
- Get the Least squares fit of Laguerre series to data in Python
- Get the Least squares fit of Legendre series to data in Python
- Get the Least squares fit of Hermite_e series to data in Python
- Get the Least-squares fit of a polynomial to data in Python
- Compute the roots of a Chebyshev series in Python
- Integrate a Chebyshev series in Python
- Differentiate a Chebyshev series in Python
- Add one Chebyshev series to another in Python
- Multiply one Chebyshev series to another in Python
- Convert a Chebyshev series to a polynomial in Python
- Convert a polynomial to a Chebyshev series in Python
- Raise a Chebyshev series to a power in Python
- Integrate a Chebyshev series and set the order of integration in Python
- Differentiate a Chebyshev series and set the derivatives in Python