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

PythonNumpyServer Side ProgrammingProgramming

To get the Least squares fit of Hermite series to data, use the hermite.hermfit() method in Python Numpy. The method returns the Hermite 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 hermite as H

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 Hermite series to data, use the hermite.hermfit() method in Python numpy. The method returns the Hermite 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 = H.hermfit(x,y,3,full=True)
print("\nResult...\n",c)
print("\nResult...\n",stats)

Example

import numpy as np
from numpy.polynomial import hermite as H

# 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 Hermite series to data, use the hermite.hermfit() method in Python numpy

c, stats = H.hermfit(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...
   [-1.54632387  1.51958929  1.97346067  1.17759858  0.18851406 -0.43906085
    -0.18878755 -0.25952276 -0.10422342  0.17851603  0.12145051  1.42408375
     0.87115462 -1.03677161  1.01691995  0.45143153 -2.11382606  0.92466707
    -0.04160743  0.9302213   1.19532222  1.69238045  1.63260027 -0.38037316
     1.57013958  0.50920773 -0.19218013 -1.104298    0.10788693  0.68370213
     0.7219109   1.28598447 -0.92218973 -0.11028072 -0.49917013 -1.44008132
    -1.51616162 -0.80578712  1.47099231 -0.79775329 -1.0606385  -0.59517496
    -0.32977967  1.04847432 -2.1621314  -0.40009103 -0.84519     0.06397194
    -2.03655702 -0.28429534  0.47013787]

Result...
   [-0.03198532 -0.0005095 -0.11666602 0.08302362]

Result...
   [array([49.28934723]), 4, array([1.39825832, 1.20144978, 0.74600162, 0.21183404]), 1.1324274851176597e-14]
raja
Updated on 02-Mar-2022 05:30:13

Advertisements