Python interp1d 与 UnivariateSpline
我正在尝试将一些 MatLab 代码移植到 Scipy,并且我尝试了 scipy.interpolate 中的两个不同的函数,interp1d 和 UnivariateSpline。 interp1d 结果与 interp1d MatLab 函数匹配,但 UnivariateSpline 数字不同 - 在某些情况下非常不同。
f = interp1d(row1,row2,kind='cubic',bounds_error=False,fill_value=numpy.max(row2))
return f(interp)
f = UnivariateSpline(row1,row2,k=3,s=0)
return f(interp)
有人能提供任何见解吗?我的 x 值间隔不均匀,尽管我不确定为什么这很重要。
I'm trying to port some MatLab code over to Scipy, and I've tried two different functions from scipy.interpolate, interp1d and UnivariateSpline. The interp1d results match the interp1d MatLab function, but the UnivariateSpline numbers come out different - and in some cases very different.
f = interp1d(row1,row2,kind='cubic',bounds_error=False,fill_value=numpy.max(row2))
return f(interp)
f = UnivariateSpline(row1,row2,k=3,s=0)
return f(interp)
Could anyone offer any insight? My x vals aren't equally spaced, although I'm not sure why that would matter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我刚刚遇到了同样的问题。
简短回答
使用 InterpolatedUnivariateSpline< /a> 相反:
长答案
UnivariateSpline 是“适合给定数据点集的一维平滑样条曲线”,而 InterpolatedUnivariateSpline 是“给定数据点集的一维插值样条”。前者平滑数据,而后者是更传统的插值方法,并重现 interp1d。下图说明了其中的差异。
重现该图的代码如下所示。
I just ran into the same issue.
Short answer
Use InterpolatedUnivariateSpline instead:
Long answer
UnivariateSpline is a 'one-dimensional smoothing spline fit to a given set of data points' whereas InterpolatedUnivariateSpline is a 'one-dimensional interpolating spline for a given set of data points'. The former smoothes the data whereas the latter is a more conventional interpolation method and reproduces the results expected from interp1d. The figure below illustrates the difference.
The code to reproduce the figure is shown below.
结果不同(但可能都是正确的)的原因是
UnivariateSpline
和interp1d
使用的插值例程不同。interp1d
使用您提供给它的x
点作为结构建平滑的 B 样条UnivariateSpline
基于 FITPACK,它也构造了平滑的 B 样条线。然而,FITPACK 尝试为样条线选择新结,以更好地拟合数据(可能是为了最小化 chi^2 加上一些曲率惩罚或类似的东西)。您可以通过g.get_knots()
找出它使用了哪些节点。所以得到不同结果的原因是插值算法不同。如果您想要在数据点处带有结的 B 样条线,请使用
interp1d
或splmake
。如果您想要 FITPACK 的功能,请使用UnivariateSpline
。在数据密集的情况下,两种方法都会给出相同的结果,但是当数据稀疏时,可能会得到不同的结果。(我怎么知道这一切:我读过代码:-)
The reason why the results are different (but both likely correct) is that the interpolation routines used by
UnivariateSpline
andinterp1d
are different.interp1d
constructs a smooth B-spline using thex
-points you gave to it as knotsUnivariateSpline
is based on FITPACK, which also constructs a smooth B-spline. However, FITPACK tries to choose new knots for the spline, to fit the data better (probably to minimize chi^2 plus some penalty for curvature, or something similar). You can find out what knot points it used viag.get_knots()
.So the reason why you get different results is that the interpolation algorithm is different. If you want B-splines with knots at data points, use
interp1d
orsplmake
. If you want what FITPACK does, useUnivariateSpline
. In the limit of dense data, both methods give same results, but when data is sparse, you may get different results.(How do I know all this: I read the code :-)
对我有用,
这给了我,
Works for me,
This gives me,
这可以解释略有不同的值吗? (我还体验到 UnivariateSpline 比 interp1d 快得多。)
this might explain the slightly different values? (I also experienced that UnivariateSpline is much faster than interp1d.)