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()

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • history.back(-1)和history.go(-1)的区别

    history.back(-1)和history.go(-1)的区别转载自:http://www.uw3c.com/jsviews/js96.html目录:1这个方法的用途2两个方法的区别3总结1这个方法的用途2两个方法的区别3总结概述:H5页面做多

  • 工作流引擎Activiti系列(一)——初识[通俗易懂]

    工作流引擎Activiti系列(一)——初识[通俗易懂]1、介绍  几乎任何一个公司的软件开发都会涉及到流程,以往我们可能是这么实现的:业务表添加标志位标识流程的节点状态,关联批注表实现审核意见,根据一些业务数据分析处理逻辑,分配任务到用户,节点的调度,审批等…..这其实是很繁琐的,且不说开发起来比较混乱,维护起来更是难上加难:     Activiti刚好就能解决几乎所有的这些问题,当流程开发变得简单有趣。  官网:

    2022年10月27日
  • noip竞赛历年试题_noip2017提高组复赛试题

    noip竞赛历年试题_noip2017提高组复赛试题在线评测:http://codevs.cn/problem/1136/整体思路:失误之处:体会心得:AC代码:

  • 挖矿脚本真的凶残!!

    挖矿脚本真的凶残!!事因:阿里突然发短信说我的阿里云服务器上面有挖矿程序!!!!!,顿时一惊,所以登陆到服务器。1.我用了top命令查看系统目前系统性能结果发现有个叫-bash的进程占用了99%的资源2.接下来我用kill-921252然后等一会又发现了这个脚本继续在占用资源,然后百度了下说这个挖矿可能有定时任务3.然后就采用了crontab-l查看定时任务列表果然有个定时任…

  • navicat连接mysql时出现2003(10060)错误

    navicat连接mysql时出现2003(10060)错误

  • dijkstra算法求最短路_图论的最短路问题

    dijkstra算法求最短路_图论的最短路问题原题链接战争中保持各个城市间的连通性非常重要。本题要求你编写一个报警程序,当失去一个城市导致国家被分裂为多个无法连通的区域时,就发出红色警报。注意:若该国本来就不完全连通,是分裂的k个区域,而失去一个城市并不改变其他城市之间的连通性,则不要发出警报。输入格式:输入在第一行给出两个整数N(0 < N ≤ 500)和M(≤ 5000),分别为城市个数(于是默认城市从0到N-1编号)和连接两城市的通路条数。随后M行,每行给出一条通路所连接的两个城市的编号,其间以1个空格分隔。在城市信息之后给出被攻占的

发表回复

您的电子邮箱地址不会被公开。

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