从弯曲的数据点推断

时间:2014-07-21 12:28:52

标签: python image-processing scipy extrapolation

我无法完全理解如何从没有排序点的数据集中推断,即对于'x'减少。像这样:

http://www.pic-host.org/images/2014/07/21/0b5ad6a11266f549.png

我知道我需要单独为x和y值创建一个图。所以代码让我这样:(点数是有序的)

x = bananax
y = bananay

t = np.arange(x.shape[0], dtype=float) 
t /= t[-1]
nt = np.linspace(0, 1, 100) 

x1 = scipy.interpolate.spline(t, x, nt) 
y1 = scipy.interpolate.spline(t, y, nt)

plt.plot(nt, x1, label='data x')
plt.plot(nt, y1, label='data y')

现在我得到了插值样条。我想我必须分别对f(nt)= x1和f(nt)= y1进行推断。我通过简单的线性回归得到了如何用数据进行插值,但是我不知道如何从中推断出更复杂的样条(?)。 目的是让外推函数遵循数据点的曲率。 (至少在一端)

干杯,谢谢!

2 个答案:

答案 0 :(得分:1)

我相信您已经走上正轨,因为您正在创建参数曲线(创建x(t)和y(t)),因为这些点是有序的。部分问题似乎是spline函数返回离散值而不是样条曲线的形式和参数。 scipy.optimize有一些很好的工具可以帮助你找到函数而不是计算点

如果您对生成数据的基础流程有任何了解,我建议您使用它来帮助选择适合的功能表单。这些更自由形式的方法将为您提供一定程度的灵活性。

调整x(t)和y(t)并保持最终的拟合函数。它们将使用t=0t=1的数据生成,但没有*会阻止您在该范围之外对它们进行评估。

我可以推荐以下链接,以获得有关曲线拟合程序的指导:

简短:http://glowingpython.blogspot.com/2011/05/curve-fitting-using-fmin.html

长:http://nbviewer.ipython.org/gist/keflavich/4042018

*几乎没有

答案 1 :(得分:0)

谢谢这让我走上正轨。对我有用的是:

x = bananax
y = bananay

#------ fit a spline to the coordinates, x and y axis are interpolated towards t
t = np.arange(x.shape[0], dtype=float) #t is # of values
t /= t[-1] #t is now devided from 0 to 1
nt = np.linspace(0, 1, 100) #nt is array with values from 0 to 1 with 100 intermediate values

x1 = scipy.interpolate.spline(t, x, nt) #The x values where spline should estimate the y values
y1 = scipy.interpolate.spline(t, y, nt)

#------ create a new linear space for nnt in which an extrapolation from the interpolated spline will be made 
nnt = np.linspace(-1, 1, 100) #values <0 are extrapolated (interpolation started at the tip(=0)

x1fit = np.polyfit(nt,x1,3) #fits a polynomial function of the nth order with the spline as input, output are the function parameters
y1fit = np.polyfit(nt,y1,3)

xpoly = np.poly1d(x1fit) #genereates the function based on the parameters obtained by polyfit
ypoly = np.poly1d(y1fit)