在Python中获得Hermite系列对数据的最小二乘法拟合

在Python中获得Hermite系列对数据的最小二乘法拟合

在这篇文章中,我们将讨论如何在Python和NumPy中找到Hermite系列对数据的最小二乘拟合。

NumPy.polynomials.hermite.hermfit 方法

Hermite数列是一个正交多项式序列,在物理学、波浪理论、数值分析和信号处理中都有其应用。这里,hermfit方法被用来获得Hermite数列与数据的最小二乘拟合,NumPy提供了一个名为Hermite.hermfit()的函数。这个函数返回度数为deg的Hermite数列的系数,该数列是在x点给定的数据值y的最小平方拟合。如果y超过1维,则进行多次拟合,对y的每一列进行一次拟合,得到的系数存储在2维返回的相应列中。

语法: hermite.hermfit(x, y, deg, full)

参数:

  • x, y: 数组
  • deg: 拟合多项式的程度
  • full:返回值的布尔性质
  • w: weights

返回值:

  • coef: 赫米特数列的阵列。

示例 1:

在第一个例子中,让我们取一个大小为30的X坐标数组,范围为(-1,1),让我们使用NumPy随机函数定义Y坐标,如图所示。如图所示,导入必要的包并传递适当的参数。

import numpy as np
from numpy.polynomial import hermite
  
# x - co ordinate
x = np.linspace(-2, 1, 30)
  
# defining the y - co ordinate using numpy random
y = x**3 - x + np.random.randn(len(x))
  
# finding the least squares
c, stats = hermite.hermfit(x, y, 3, full=True)
  
  
print(f'The x coordinate array is \n{x}\n')
print(f'The y coordinate array is \n{y}\n')
print(f'The hermit coefficient are \n{c}\n')
print(f'The resultant array is \n{stats}\n')

输出:

The x coordinate array is 

[-2. -1.89655172 -1.79310345 -1.68965517 -1.5862069 -1.48275862 

-1.37931034 -1.27586207 -1.17241379 -1.06896552 -0.96551724 -0.86206897 

-0.75862069 -0.65517241 -0.55172414 -0.44827586 -0.34482759 -0.24137931 

-0.13793103 -0.03448276 0.06896552 0.17241379 0.27586207 0.37931034 

0.48275862 0.5862069 0.68965517 0.79310345 0.89655172 1. ] 

The y coordinate array is 

[-6.60789595 -5.02995614 -3.82266632 -4.29148936 -3.41689134 -2.73899431 

-2.80203248 0.42527851 1.56309224 -0.15421266 0.01694003 -0.27000318 

1.17370229 0.54996278 1.90737212 1.05195276 1.22928767 -0.24762963 

0.57362607 -0.17531795 0.9954843 -0.33641804 -0.14227936 -0.67820814 

1.36224629 0.31530983 -0.17916042 -0.41791427 -0.26638476 0.4571403 ] 

The hermit coefficient are 

[ 0.32269386 0.23402692 -0.07321117 0.12818421] 

The resultant array is 

[array([17.45846549]), 4, array([1.68066214, 0.84234764, 0.65197165, 0.2018866 ]), 6.661338147750939e-15] 

示例 2:

在第二个例子中,让我们取一个范围为(-10,10)的大小为50的X坐标数组,让我们使用NumPy统一函数定义一个不同的Y坐标,如图所示。如图所示,导入必要的包并传递适当的参数。

import numpy as np
from numpy.polynomial import hermite
  
# x - co ordinate
x = np.linspace(-10, 10, 50)
  
# defining the y - co ordinate using numpy random
y = x**4 - x + np.random.uniform(len(x))
  
# finding the least squares
c, stats = hermite.hermfit(x, y, 3, full=True)
  
print(f'The x coordinate array is \n{x}\n')
print(f'The y coordinate array is \n{y}\n')
print(f'The hermit coefficient are \n{c}\n')
print(f'The resultant array is \n{stats}\n')

输出:

The x coordinate array is 

[-10. -9.59183673 -9.18367347 -8.7755102 -8.36734694 

-7.95918367 -7.55102041 -7.14285714 -6.73469388 -6.32653061 

-5.91836735 -5.51020408 -5.10204082 -4.69387755 -4.28571429 

-3.87755102 -3.46938776 -3.06122449 -2.65306122 -2.24489796 

-1.83673469 -1.42857143 -1.02040816 -0.6122449 -0.20408163 

0.20408163 0.6122449 1.02040816 1.42857143 1.83673469 

2.24489796 2.65306122 3.06122449 3.46938776 3.87755102 

4.28571429 4.69387755 5.10204082 5.51020408 5.91836735 

6.32653061 6.73469388 7.14285714 7.55102041 7.95918367 

8.36734694 8.7755102 9.18367347 9.59183673 10. ] 

The y coordinate array is 

[1.00153754e+04 8.47958029e+03 7.12777075e+03 5.94462618e+03 

4.91549213e+03 4.02638027e+03 3.26396835e+03 2.61560027e+03 

2.06928601e+03 1.61370168e+03 1.23818950e+03 9.32757802e+02 

6.88081016e+02 4.95499697e+02 3.47020507e+02 2.35316221e+02 

1.53725724e+02 9.62540125e+01 5.75721951e+01 3.30174914e+01 

1.85932329e+01 1.09688622e+01 7.47993342e+00 6.12811226e+00 

5.58117577e+00 5.17301251e+00 4.90362246e+00 5.43911710e+00 

8.11171933e+00 1.49197635e+01 2.85276955e+01 5.22660726e+01 

9.01315635e+01 1.46786949e+02 2.27561119e+02 3.38449079e+02 

4.86111942e+02 6.77876934e+02 9.21737393e+02 1.22635277e+03 

1.60104862e+03 2.05581662e+03 2.60131455e+03 3.24886631e+03 

4.01046190e+03 4.89875744e+03 5.92707516e+03 7.10940340e+03 

8.46039661e+03 9.99537536e+03] 

The hermit coefficient are 

[-8.75646882e+02 -5.00000000e-01 2.22734575e+01 -1.47220903e-16] 

The resultant array is 

[array([33708458.779302]), 4, array([1.38316131, 1.31936531, 0.50919072, 0.29472834]), 1.1102230246251565e-14] 

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Numpy 多项式