python interpolate.interp1d,Python interp1d与UnivariateSpline

python interpolate.interp1d,Python interp1d与UnivariateSplineI’mtryingtoportsomeMatLabcodeovertoScipy,andI’vetriedtwodifferentfunctionsfromscipy.interpolate,interp1dandUnivariateSpline.Theinterp1dresultsmatchtheinterp1dMatLabfunctio…

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

python interpolate.interp1d,Python interp1d与UnivariateSpline

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.

94794125e666514af815f651b62fe1b0.png

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.net/131821.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 理解的英文_iaas saas paas daas区别

    理解的英文_iaas saas paas daas区别云服务只是一个统称,可以分成三大类。IaaS:基础设施服务,Infrastructure-as-a-servicePaaS:平台服务,Platform-as-a-serviceSaaS:软件服务

    2022年8月6日
    8
  • 心脏与阴影,求阴影部分

    心脏与阴影,求阴影部分

    2022年1月9日
    45
  • 三极管的导通条件总结[通俗易懂]

    三极管的导通条件总结[通俗易懂]对三极管放大作用的理解,切记一点:能量不会无缘无故的产生,所以,三极管一定不会产生能量。但三极管厉害的地方在于:它可以通过小电流去控制大电流。放大的原理就在于:通过小的交流输入,控制大的静态直流。假设三极管是个大坝,这个大坝奇怪的地方是,有两个阀门,一个大阀门,一个小阀门。小阀门可以用人力打开,大阀门很重,人力是打不开的,只能通过小阀门的水力打开。所以,平常的工作流程便是,每当放水

    2022年6月18日
    75
  • 30分钟看懂经济运行原理_看懂了自然哲学原理

    30分钟看懂经济运行原理_看懂了自然哲学原理这部分内容主要解释一些概念和术语,最好是先理解这部分内容。公钥密码体制分为三个部分,公钥、私钥、加密解密算法,它的加密解密过程如下:加密:通过加密算法和公钥对内容(或者说明文)进行加密,得到密文。

    2022年8月4日
    9
  • qtcpsocket断开_2020-05-06 QT子线程使用QTcpSocket连接服务器

    qtcpsocket断开_2020-05-06 QT子线程使用QTcpSocket连接服务器为什么要是用多线程?多线程的使用主要是为了处理比较耗时的过程。多线程的实现可以通过两种方式实现分别是:1.继承QThread实现多线程2.继承QObject实现多线程(由于继承QObject的多线程实现方法更加灵活,Qt官方推荐使用该方法实现多线程)。这里将采用第二种方式实现多线程多线程实现过程1.创建一个继承于QObject的自定义线程类,用来处理比较耗时的功能。相关函数:voidsetFla…

    2025年8月29日
    11
  • ReLU激活函数:简单之美

    ReLU激活函数:简单之美导语在深度神经网络中,通常使用一种叫修正线性单元(Rectifiedlinearunit,ReLU)作为神经元的激活函数。

    2022年6月15日
    26

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号