python - numpy polyfit 产生无意义的结果

标签 python numpy data-fitting

我正在尝试适应这些值:Values to fit

这是我的代码:

  for i in range(-area,area):
    stDev1= []
    for j in range(-area,area):
        stDev0 = stDev[i+i0][j+j0]
        stDev1.append(stDev0)
    slices[i] = stDev1
fitV = []
xV = []

for l in range(-area,area):
    y = np.asarray(slices[l])
    x =  np.arange(0,2*area,1)

for m in range(-area,area):
        fitV.append(slices[m][l])
        xV.append(l)


fit = np.polyfit(xV,fitV,4)
yfit = function(fit,area)

x100 = np.arange(0,100,1)

plt.plot(xV,fitV,'.')
plt.savefig("fits1.png")

def function(fit,area):
   yfit = []
   for x in range(-area,area):
       yfit.append(fit[0]+fit[1]*x+fit[2]*x**2+fit[3]*x**3+fit[4]*x**4)
   return(yfit)

i0 = 400
j0 = 400
area = 50 
stdev = 2d np.array([1300][800]) #just an image of "noise" feel free to add any image // 2d np array you like. 

这会产生: Wrong fit, in green are the same values I plotted in image 1

显然这是完全错误的? 我想我想念 Polyfit 的概念?从文档中,要求我向它提供两个形状为 x[i] y[i] 的数组?我的值(value)观

  xV = [ x_1_-50,x_1_-49,...,x_1_49,x_2_-50,...,x_49_49] 

我的ys是:

  fitV = [y_1_-50,y_1_-49,...,y_1_49,...y_2_-50,...,y_2_49]

最佳答案

我不完全理解你的程序。将来,如果您将问题提炼为 MCVE 将会很有帮助。 。但这里有一些想法:

  1. 在您的数据中,对于给定的 x 值,似乎有多个 y 值。给定 (x,y) 数据,polyfit返回一个表示多项式函数的元组,但没有函数可以将x的单个值映射到y的多个值。第一步,考虑使用平均值、中位数或众数等将每组 y 值折叠为单个代表值。或者,在您的领域中,可能有一种更自然的方法来做到这一点。

  2. 其次,有一种惯用的方式来使用这对函数 np.polyfitnp.polyval ,并且您没有以标准方式使用它们。当然,存在许多与此模式不同的有用的方式,但首先请确保您了解这两个函数的基本模式。

    a.给定您在时间或地点 x_data 进行的测量值 y_data,绘制它们并猜测拟合顺序。也就是说,它看起来像一条线吗?像抛物线一样?假设您认为您的数据是抛物线型的,并且您将使用二阶多项式拟合。

    b.确保您的数组按升序排序 x 。有很多方法可以做到这一点,但是 np.argsort这是一个简单的问题。

    c.运行polyfit :p = polyfit(x_data,y_data,2) ,它返回一个包含 p 中的二阶、一阶和零阶系数的元组, (c2,c1,c0) .

    d.惯用用法是 polyfitpolyval ,接下来您将生成适合的: polyval(p,x_data) 。或者您可能希望对拟合进行更粗略或更精细的采样,在这种情况下,您可能会采用 x_data 的子集。或在 x_data 中插入更多值.

下面是一个完整的示例。

import numpy as np
from matplotlib import pyplot as plt

# these are your measurements, unsorted
x_data = np.array([18, 6, 9, 12 , 3, 0, 15])
y_data = np.array([583.26347805, 63.16059915, 100.94286909, 183.72581827, 62.24497418,
                   134.99558191, 368.78421529])

# first, sort both vectors in increasing-x order:
sorted_indices = np.argsort(x_data)
x_data = x_data[sorted_indices]
y_data = y_data[sorted_indices]

# now, plot and observe the parabolic shape:
plt.plot(x_data,y_data,'ks')
plt.show()

# generate the 2nd order fitting polynomial:
p = np.polyfit(x_data,y_data,2)

# make a more finely sampled x_fit vector with, for example
# 1024 equally spaced points between the first and last
# values of x_data
x_fit = np.linspace(x_data[0],x_data[-1],1024)

# now, compute the fit using your polynomial:
y_fit = np.polyval(p,x_fit)

# and plot them together:
plt.plot(x_data,y_data,'ks')
plt.plot(x_fit,y_fit,'b--')
plt.show()

希望有帮助。

关于python - numpy polyfit 产生无意义的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39660663/

相关文章:

python - Haystack/Whoosh 索引生成错误

python - 删除模型继承并将 id 保留在新创建的自动字段中

python - Scipy 稀疏矩阵中的行划分

python - 如何在python中将包含单引号或双引号的数据插入数据库?

python - 从字典中的对象访问方法

python - 大数组之间的 numpy bool 比较返回 False 而不是 bool 数组

python - Numpy 数组 View 和垃圾收集

python - 如何在 scipy.stats.gamma.fit 中获得拟合参数的误差估计?

matlab - Matlab 函数 "fit"是否有等效的 GNU Octave?

3D 中的 MATLAB 曲线拟合,具有附加边界