【问题标题】:numpy polyfit yields nonsensenumpy polyfit 产生废话
【发布时间】:2017-02-01 07:23:09
【问题描述】:

我正在尝试适应这些值:

这是我的代码:

  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. 

这会产生:

显然这是完全错误的? 我想我想念理解 polyfit 的概念?从文档中,要求是我用两个形状为 x[i] y[i] 的数组来喂它?我的价值观在

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

我是:

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

【问题讨论】:

  • 能否提供输入数据?
  • 我们不知道areastDevi0j0

标签: python numpy data-fitting


【解决方案1】:

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

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

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

    一个。给定您在时间或位置 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()

希望对您有所帮助。

【讨论】:

  • 好的,所以我的问题是我有多个值 y 到 x。取平均值是一个很好的解决方法,但我不确定这是否真的等于我在这里拥有的映射的最小二乘(我不是数学家,但如果我没记错的话,一个函数是精确的,但是映射可以有多个值x->y(x)。所以我想我实际上是在尝试将多项式拟合到映射中,同样,自从我被介绍给函数、映射及其理论以来已经有很长时间了:()
  • 术语“地图”和“功能”或多或少可以互换,但有一些特定学科的限制。你怎么称呼它并不重要。您绘制的关系是不确定的:给定的 x 值会为 f(x) 产生许多值,并且没有多项式可以描述它。可能有一种方法可以通过使用适当的低阶多项式来抖动 x 值并避免过度拟合,但 LSE 会有效地平均您的 f(x) 值。
猜你喜欢
  • 1970-01-01
  • 2021-07-24
  • 1970-01-01
  • 1970-01-01
  • 2012-05-20
  • 2015-03-30
  • 2012-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多