0

我不能完全理解如何从点未排序的数据集中进行推断,即对于“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 进行外推。我知道如何使用简单的线性回归从数据中进行插值,但我错过了如何从中推断出更复杂的样条(?)。目的是让外推函数遵循数据点的曲率。(至少在一端)

干杯,谢谢!

4

2 回答 2

1

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

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

拟合 x(t) 和 y(t) 并保留得到的拟合函数。它们将使用 from t=0to的数据生成,t=1但没有什么*会阻止您在该范围之外评估它们。

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

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

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

*几乎没有

于 2014-07-21T14:31:13.470 回答
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)
于 2014-08-18T17:45:12.670 回答