2016-09-23 42 views
0

我试图将这些值:Values to fitnumpy的polyfit产量废话

这是我的代码:

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]喂它?我在

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] 
+0

你能提供输入数据吗? – Jeon

+0

我们不知道'area','stDev','i0','j0' – Jeon

回答

1

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

  1. 看来,在您的数据,对于的X给定值也有Ÿ多个值。给定(X ÿ)数据,polyfit返回表示多项式函数一个元组,但没有功能可以的X单个值映射到的ý多个值。作为第一步,考虑使用例如平均值,中值或模式将每组数值折叠为单个代表值。或者,也许在你的领域,有一种更自然的方式来做到这一点。

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

    a。鉴于你的测量y_data,在时间或地点采取x_data,绘制它们并猜测合适的顺序。那是不是看起来像一条线?像抛物线一样?假设您相信您的数据是抛物线的,并且您将使用二阶多项式拟合。

    b。确保你的数组按照递增的顺序排列。有很多方法可以做到这一点,但np.argsort是一个简单的方法。

    c。运行polyfitp = polyfit(x_data,y_data,2),它返回一个包含p(c2,c1,c0)中的第2,第1和第0阶系数的元组。 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() 

希望有帮助。

+0

好,所以我的问题是我有多个值y到x。取平均值是一个很好的工作,但我不知道这是否真的等于我在这里的映射的最小平方(我不是一个数学家,但如果我正确地记得一个函数是准确的,但映射可以有多个值所以我想我实际上是试图将一个多项式拟合到一个映射上,自从我被引入到函数,映射和它们的理论后,它又一次很长时间:() – Sebastiano1991

+0

术语'映射'和'函数'或多或少是可以互换的,具有一些与学科相关的约束条件,不管你称之为什么,你所绘制的关系都是不确定的:给定的x值给出f(x)的很多值,没有多项式可以描述它。可能有一种方法可以使x值抖动并避免使用适当的低阶多项式进行过拟合,但LSE将有效地对f(x)值取平均值。 – rjonnal