在Python中使用最小二乘拟合Hermite_e系列到数据

在Python中使用最小二乘拟合Hermite_e系列到数据

要将Hermite_e系列最小二乘拟合到数据中,请使用Python numpy中的hermite_e.hermfit()方法。该方法返回按从低到高排序的Hermite_e系数。如果y是2D的,那么y中第k列的数据的系数将在k列中。参数x是M个样本(数据)点的x坐标(x[i],y[i])。

参数y是样本点的y坐标。可以使用一个polyfit调用来拟合多个共享相同x坐标的样本点,只需将y作为包含每列一个数据集的2D数组传递进去。参数deg是拟合多项式的次数。如果deg是一个整数,则将包括所有低于等于deg次的项在拟合中。参数rcond是拟合的相对条件数。相对于最大奇异值,小于rcond的奇异值将被忽略。默认值为len(x)*eps,其中eps是平台Float类型的相对精度,在大多数情况下约为2e-16。

参数full是开关,用于确定返回值的性质。当为False(默认值)时,仅返回系数;当为True时,还返回奇异值分解的诊断信息。参数w是权重。如果不为None,则权重w[i]适用于x[i]处的未平方残差y[i] – y_hat[i]。理想情况下,权重应该选择为所有乘积w[i]*y[i]的误差具有相同的方差。使用逆方差加权时,请使用w[i] = 1/sigma(y[i])。默认值为None。

步骤

首先,导入所需的库−

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

x-坐标 −

x = np.linspace(-1,1,51)

显示x坐标−

print("X Co-ordinate...\n",x)

纵坐标 –

y = x**3 - x + np.random.randn(len(x))
print("\nY Co-ordinate...\n",y)

要将Hermite_e序列的最小二乘拟合到数据中,请使用Python中的hermite_e.hermfit()方法。

c, stats = H.hermefit(x,y,3,full=True)
print("\nResult...\n",c)
print("\nResult...\n",stats)

示例

import numpy as np
from numpy.polynomial import hermite_e 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_e series to data, use the hermite_e.hermfit() method in Python numpy
c, stats = H.hermefit(x,y,3,full=True)

print("\nResult...\n",c)

print("\nResult...\n",stats)

输出

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.54079609 -1.17586687 -0.81506394  0.8047718  -1.21403444 -1.09247646
   -0.88942226 -0.62335081  0.83995142  0.29147171  2.45859847 -0.37545462
    0.90161986 -0.7125131  -0.82978518  0.25422338  0.62073702 -1.43305948
    0.96436296  0.03069738 -1.07349677  0.55233582  1.23286374  0.37330458
    0.27239629  0.46859691 -0.1074476   1.19279741  0.15844038 -0.20424904
   -1.41467693 -0.79396457 -2.38068246 -1.24121297 -0.7877071  -1.09171002
    1.0806185  -0.94389035 -2.16201749  0.21671724 -1.15596405  0.57090598
   -0.52496753 -0.20358065 -3.72121093  1.39868958 -0.02626711 -1.51582035
   -0.12223608 -0.58368042  0.69138128]

Result...
 [-0.54892802 4.71593168 -0.40858959 2.08689429]

Result...
 [array([51.90771673]), 4, array([1.41192215, 1.37967947, 0.31061966, 0.08047256]), 1.1324274851176597e-14]

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程