numpy 为什么我的多边形拟合结果不真实?

vptzau2j  于 2023-01-17  发布在  其他
关注(0)|答案(1)|浏览(102)

我使用以下代码将2次多项式拟合到我的2D数据:

deg = 1
fig, ax = plt.subplots(1,facecolor="white")
fig.set_size_inches(5,4)
x = [0.843291,0.873922,0.903581,0.93344,0.961478,0.990968,
     1.019434,1.047562,1.075373,1.102883,1.130106,1.157059,
     1.183751,1.210195,1.236399,1.262376,1.288134,1.313679,
     1.339022,1.364167,1.389123,1.413896,1.438492,1.462916,
     1.505685,1.57998,1.690303,1.730423,1.761613,1.791332,
     1.63034,1.652749,1.675848,1.698821,1.721672,1.744402]
y = [291000000,291000000,292000000,291000000,290000000,290000000,
     290000000,290000000,289000000,289000000,288000000,288000000,
     288000000,287000000,287000000,287000000,287000000,286000000,
     286000000,286000000,285000000,285000000,284000000,284000000,
     284000000,283000000,283000000,282000000,282000000,281000000,
     280000000,280000000,279000000,278000000,277000000,277000000]
ax.scatter(x, y,label = "data", c="black", alpha = 0.8)
poly =np.poly1d(np.polyfit(x, y, deg = deg))
ax.plot(np.polyval(poly, x), label='fit', c="black", alpha = 0.5)
ax.legend()

这给了我下面的情节,这似乎并不现实:

我实现的代码中是否有任何部分是错误的?

41zrol4v

41zrol4v1#

你的拟合是(几乎)正确的,你的图不是:

# first sort the values by x
order = np.argsort(x)
x = np.array(x)[order]
y = np.array(y)[order]

# the plot and compute the fit
deg = 1
fig, ax = plt.subplots(1,facecolor="white")
fig.set_size_inches(5,4)
ax.scatter(x, y,label = "data", c="black", alpha = 0.8)
poly =np.poly1d(np.polyfit(x, y, deg = deg))

# you need to pass the x values here
ax.plot(x, np.polyval(poly, x), label='fit', c="black", alpha = 0.5)
ax.legend()
  • 如果省略x值,则使用range。*

输出:

相关问题