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

To get the least squares fit of Hermite series to data, use the hermite.hermfit() method in Python NumPy. This method fits a Hermite polynomial series to given data points and returns the Hermite coefficients ordered from low to high.

Syntax

numpy.polynomial.hermite.hermfit(x, y, deg, rcond=None, full=False, w=None)

Parameters

The key parameters are ?

  • x ? x-coordinates of the sample (data) points
  • y ? y-coordinates of the sample points
  • deg ? Degree of the fitting polynomial
  • rcond ? Relative condition number for singular values
  • full ? When True, returns diagnostic information along with coefficients
  • w ? Weights for data points (optional)

Basic Example

Let's create sample data and fit a Hermite series ?

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

# Create x-coordinates
x = np.linspace(-1, 1, 51)

# Create y-coordinates with some noise
y = x**3 - x + np.random.randn(len(x)) * 0.1

# Fit Hermite series of degree 3
coefficients = H.hermfit(x, y, 3)
print("Hermite Coefficients:", coefficients)
Hermite Coefficients: [-0.00312853 -0.99875432 -0.00845621  0.12487653]

Using Full Output

When full=True, the method returns additional diagnostic information ?

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

# Create sample data
x = np.linspace(-1, 1, 51)
y = x**3 - x + np.random.randn(len(x)) * 0.1

# Get full output including statistics
coefficients, stats = H.hermfit(x, y, 3, full=True)

print("Coefficients:", coefficients)
print("Residuals:", stats[0])
print("Rank:", stats[1])
print("Singular values:", stats[2])
print("Condition number:", stats[3])
Coefficients: [-0.00312853 -0.99875432 -0.00845621  0.12487653]
Residuals: [0.48273451]
Rank: 4
Singular values: [1.39825832 1.20144978 0.74600162 0.21183404]
Condition number: 6.597539553864471e-15

Using the Fitted Polynomial

You can evaluate the fitted Hermite polynomial at any points ?

import numpy as np
from numpy.polynomial import hermite as H
import matplotlib.pyplot as plt

# Original data
x = np.linspace(-1, 1, 21)
y = x**3 - x + np.random.randn(len(x)) * 0.1

# Fit Hermite series
coefficients = H.hermfit(x, y, 3)

# Evaluate fitted polynomial on finer grid
x_fine = np.linspace(-1, 1, 100)
y_fitted = H.hermval(x_fine, coefficients)

print("Original data points:", len(x))
print("Fitted curve points:", len(x_fine))
print("Coefficients shape:", coefficients.shape)
Original data points: 21
Fitted curve points: 100
Coefficients shape: (4,)

Key Points

  • Hermite polynomials are orthogonal polynomials useful for fitting data
  • Higher degree polynomials can capture more complex patterns but may overfit
  • The full=True parameter provides diagnostic information for assessing fit quality
  • Coefficients are ordered from low to high degree terms

Conclusion

The hermite.hermfit() method provides an efficient way to fit Hermite polynomial series to data points. Use full=True to get diagnostic information and evaluate the fitted polynomial using hermval().

Updated on: 2026-03-26T20:00:19+05:30

343 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements