前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >python interpolate.interp1d,Python interp1d与UnivariateSpline

python interpolate.interp1d,Python interp1d与UnivariateSpline

作者头像
全栈程序员站长
发布2022-07-01 08:56:05
发布2022-07-01 08:56:05
3600
举报

大家好,又见面了,我是你们的朋友全栈君。

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.

解决方案

I just ran into the same issue.

Short answer

f = InterpolatedUnivariateSpline(row1, row2)

return f(interp)

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.

import scipy.interpolate as ip

#Define independent variable

sparse = linspace(0, 2 * pi, num = 20)

dense = linspace(0, 2 * pi, num = 200)

#Define function and calculate dependent variable

f = lambda x: sin(x) + 2

fsparse = f(sparse)

fdense = f(dense)

ax = subplot(2, 1, 1)

#Plot the sparse samples and the true function

plot(sparse, fsparse, label = ‘Sparse samples’, linestyle = ‘None’, marker = ‘o’)

plot(dense, fdense, label = ‘True function’)

#Plot the different interpolation results

interpolate = ip.InterpolatedUnivariateSpline(sparse, fsparse)

plot(dense, interpolate(dense), label = ‘InterpolatedUnivariateSpline’, linewidth = 2)

smoothing = ip.UnivariateSpline(sparse, fsparse)

plot(dense, smoothing(dense), label = ‘UnivariateSpline’, color = ‘k’, linewidth = 2)

ip1d = ip.interp1d(sparse, fsparse, kind = ‘cubic’)

plot(dense, ip1d(dense), label = ‘interp1d’)

ylim(.9, 3.3)

legend(loc = ‘upper right’, frameon = False)

ylabel(‘f(x)’)

#Plot the fractional error

subplot(2, 1, 2, sharex = ax)

plot(dense, smoothing(dense) / fdense – 1, label = ‘UnivariateSpline’)

plot(dense, interpolate(dense) / fdense – 1, label = ‘InterpolatedUnivariateSpline’)

plot(dense, ip1d(dense) / fdense – 1, label = ‘interp1d’)

ylabel(‘Fractional error’)

xlabel(‘x’)

ylim(-.1,.15)

legend(loc = ‘upper left’, frameon = False)

tight_layout()

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/131821.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档