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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 且用计算机语言怎么表示,如何学习SCL语言?SCL语言编程入门

    且用计算机语言怎么表示,如何学习SCL语言?SCL语言编程入门原标题:如何学习SCL语言?SCL语言编程入门随着现代工控技术的不断发展,可能很多使用过PLC的技术人员都有这么一个感受:传统的‘梯形图’编程方式在面对越来越复杂的控制要求时,已显得力不从心。其实,现在很多大品牌的中高级PLC都支持国际电工委员会IEC61131标准中规范的五种编程语言的混合编程,即梯形图(LD)、结构化文本(ST)、流程图(SFC)、指令表(IL)和功能块(FB…

    2022年10月7日
    0
  • php 正则表达式怎么匹配标签里面的style?

    php 正则表达式怎么匹配标签里面的style?

    2021年10月15日
    69
  • PDB文件说明

    PDB文件说明蛋白质数据库ProteinDataBank(PDB)是一个包含蛋白质、核酸等生物大分子的结构数据的数据库,网址是http://www.rcsb.org。PDB可以经由网络免费访问,是结构生物学研究中的重要资源。为了确保PDB资料的完备与权威,各个主要的科学杂志、基金组织会要求科学家将自己的研究成果提交给PDB。PDB数据库存储结构数据的文件是PDB文件,每一个蛋白质或核酸都对应着一个编号,即P…

    2022年6月2日
    121
  • pycharm2021激活码【2021免费激活】

    (pycharm2021激活码)JetBrains旗下有多款编译器工具(如:IntelliJ、WebStorm、PyCharm等)在各编程领域几乎都占据了垄断地位。建立在开源IntelliJ平台之上,过去15年以来,JetBrains一直在不断发展和完善这个平台。这个平台可以针对您的开发工作流进行微调并且能够提供…

    2022年3月26日
    87
  • Java ListNode 链表

    JavaListNode链表基本结构基本初始化添加构造方法初始化范型写法创建与遍历链表插入节点替换节点删除节点补充说明基本结构链表是一种数据结构,由数据和指针构成,JavaListNode链表是一种由Java自定义实现的链表结构。基本初始化classListNode{//类名:Java类就是一种自定义的数据结构intval;//数据:节点数据ListNodenext;//对象:引用下一个节点对象。在Jav

    2022年4月8日
    250
  • 黑盒测试用例测试方法

    黑盒测试用例测试方法黑盒测试用例设计方法一、等价类划分法等价类划分法是一种典型的、重要的黑盒测试方法,是指某个输入域的子集合。在该子集合中,所有的输入数据对于揭露软件中的错误都是等效的。等价类划分有效等价类和无效等价类例如:微信红包的例子【0.01-200】按数据范围划分:有效的:0.01-200(1)无效的:小于0.01(2)…

    2022年6月12日
    36

发表回复

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

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