在没有numpy polyfit的情况下在python中拟合二次函数

Ahm*_*ssa 6 python regression numpy curve-fitting

我正在尝试将二次函数拟合到某些数据,并且尝试不使用numpy的polyfit函数来执行此操作。

从数学上讲,我试图遵循此网站https://neutrium.net/mathematics/least-squares-fitting-of-a-polynomial/,但我不知为何我做对了。如果有人可以帮助我,那就太好了;或者,如果您可以建议另一种方法,那也很棒。

到目前为止,我已经尝试过:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

ones = np.ones(3)
A = np.array( ((0,1),(1,1),(2,1)))
xfeature = A.T[0]
squaredfeature = A.T[0] ** 2
b = np.array( (1,2,0), ndmin=2 ).T
b = b.reshape(3)

features = np.concatenate((np.vstack(ones), np.vstack(xfeature), np.vstack(squaredfeature)), axis = 1)
featuresc = features.copy()
print(features)
m_det = np.linalg.det(features)
print(m_det)
determinants = []
for i in range(3):
    featuresc.T[i] = b
    print(featuresc)
    det = np.linalg.det(featuresc)
    determinants.append(det)
    print(det)
    featuresc = features.copy()

determinants = determinants / m_det
print(determinants)
plt.scatter(A.T[0],b)
u = np.linspace(0,3,100)
plt.plot(u, u**2*determinants[2] + u*determinants[1] + determinants[0] )
p2 = np.polyfit(A.T[0],b,2)
plt.plot(u, np.polyval(p2,u), 'b--')
plt.show()
Run Code Online (Sandbox Code Playgroud)

如您所见,我的曲线与nnumpy的polyfit曲线相比效果不佳。 情节


更新:我遍历了代码并删除了所有愚蠢的错误,现在,当我尝试使其超过3分时,它可以工作,但是我不知道如何超过3分。 更新

这是新代码:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

ones = np.ones(3)
A = np.array( ((0,1),(1,1),(2,1)))
xfeature = A.T[0]
squaredfeature = A.T[0] ** 2
b = np.array( (1,2,0), ndmin=2 ).T
b = b.reshape(3)

features = np.concatenate((np.vstack(ones), np.vstack(xfeature), np.vstack(squaredfeature)), axis = 1)
featuresc = features.copy()
print(features)
m_det = np.linalg.det(features)
print(m_det)
determinants = []
for i in range(3):
    featuresc.T[i] = b
    print(featuresc)
    det = np.linalg.det(featuresc)
    determinants.append(det)
    print(det)
    featuresc = features.copy()

determinants = determinants / m_det
print(determinants)
plt.scatter(A.T[0],b)
u = np.linspace(0,3,100)
plt.plot(u, u**2*determinants[2] + u*determinants[1] + determinants[0] )
p2 = np.polyfit(A.T[0],b,2)
plt.plot(u, np.polyval(p2,u), 'r--')
plt.show()
Run Code Online (Sandbox Code Playgroud)

ray*_*ica 6

代替使用克莱默法则,实际上使用最小二乘法求解系统。请记住,只有当您拥有的点总数等于多项式的期望阶数加1时,克莱默法则才有效。如果您不具备此点,那么克莱默法则将不起作用,因为您正试图找到精确的解。问题。如果您有更多点,则此方法不合适,因为我们将创建一个超定方程组。

为了numpy.linalg.lstsq使它适应更多点,将是更好的拟合方法,因为它通过使用矩阵A计算使欧几里得范数最小的向量x来解决Ax = b的解决方案。因此,从特征矩阵的最后一列中删除值并求解系数,然后使用来求解系数:ynumpy.linalg.lstsq

import numpy as np
import matplotlib.pyplot as plt


ones = np.ones(4)
xfeature = np.asarray([0,1,2,3])
squaredfeature = xfeature ** 2
b = np.asarray([1,2,0,3])

features = np.concatenate((np.vstack(ones),np.vstack(xfeature),np.vstack(squaredfeature)), axis = 1) # Change - remove the y values

determinants = np.linalg.lstsq(features, b)[0] # Change - use least squares
plt.scatter(xfeature,b)
u = np.linspace(0,3,100)
plt.plot(u, u**2*determinants[2] + u*determinants[1] + determinants[0] )
plt.show()
Run Code Online (Sandbox Code Playgroud)

我现在得到了这个图,它与图中的虚线曲线匹配,也与numpy.polyfit给你的图形匹配:

最小二乘拟合

  • 哦,哇...我回到我的代码,看了看我的矩阵,对我自己说,如果我精确地使用3分,那这可能行得通。我回到这里阅读您的答案,它说的正是我刚刚发现的。谢谢哈哈 (2认同)
  • @AhmadMoussa啊哈!我的荣幸。我认为如果您要添加更多点,您将会遇到麻烦。最小二乘是解决这个问题的方法。很高兴我能帮上忙! (2认同)
  • 如果要理解和使用我发现的最小二乘拟合,最好的描述是在Gene F. Franklin(作者),J。David所著的《动态系统的数字控制》(第三版)第三版中。鲍威尔(作者),迈克尔·沃克曼(作者) (2认同)