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

标签 python numpy regression 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()

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


更新: 我检查了我的代码并删除了所有愚蠢的错误,现在它可以工作了,当我尝试将它拟合超过 3 个点时,但我不知道如何拟合超过三个点。 updated

这是新代码:

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()

最佳答案

而不是使用 Cramer 规则,实际使用最小二乘法求解系统。请记住,只有当您拥有的总点数等于所需的多项式阶数加 1 时,克莱默规则才会起作用。 如果你没有这个,那么当你试图找到问题的精确解决方案时,Cramer 的规则将不起作用。如果点数较多,则该方法不适用,因为我们将创建一个超定方程组。

为了适应更多点,numpy.linalg.lstsq会更合适,因为它通过计算使用矩阵 A 最小化欧几里得范数的向量 x 来求解 Ax = b 的解.因此,从特征矩阵的最后一列中删除 y 值并求解系数,并使用 numpy.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()

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

least squares fit

关于python - 在没有numpy polyfit的情况下在python中拟合二次函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56181712/

相关文章:

r - 使用 R 提取每个回归系数的 p 值列表(1104 个线性回归)

关于使用 lm 在 R 中的线性回归建模中的 I( ) 项

python - 如何在点击 QTreeWidget 的空白区域时执行函数

c# - 将 C#(前端)连接到 apache/php/python(后端)

python - 更改mapltolib图中的 Axis

pandas - 迭代 Pandas 数据框和字典项

python - 从头到尾的 Numpy 切片

python - 在 python/pandas 中连接文本和数字

python - 如何按列分组并将其他列的值作为 pandas 中的列表返回?

scala - 如何在 Spark MLlib 中设置自定义损失函数