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


相关推荐

  • 雅虎优化和排名技术

    雅虎优化和排名技术关于YAHOO优化和排名技术基础.把页面本身优化好,就能在Yahoo中取得很好的排名,而这些页面优化都是自己可以控制的,所以说Yahoo优化比Google优化更容易,所用时间更短,特别是对于热门关键词,两者的区别更明显。从另一个角度来说,能把Google优化好的人,不一定水平有多高,可能只不过是手头有较多的链接资源可以利用;若服务期过了以后,把指向你的链接一撤,你的排名就会掉下来。而若能把Yaho

    2022年7月13日
    14
  • jar find小技巧

    jar find小技巧

    2021年5月12日
    139
  • navigator对象_navigator.geolocation

    navigator对象_navigator.geolocationNavigator对象包含有关浏览器的信息,是BOM对象。一、Navigator对象属性属性 说明 appCodeName 返回浏览器的代码名 appName 返回浏览器的名称 appVersion 返回浏览器的平台和版本信息 cookieEnabled 返回指明浏览器中是否启用cookie的布尔值 platform 返回运行…

    2022年9月11日
    1
  • 人工智能学习路线

    阶段一、人工智能基础- 高等数学必知必会本阶段主要从数据分析、概率论和线性代数及矩阵和凸优化这四大块讲解基础,旨在训练大家逻辑能力,分析能力。拥有良好的数学基础,有利于大家在后续课程的学习中更好的理解机器学习和深度学习的相关算法内容。同时对于AI研究尤为重要,例如人工智能中的智能很大一部分依托“概率论”实现的。一、数据分析1)常数e2)导数3)梯度4)Taylor5)gini系数6)信息熵与…

    2022年4月9日
    1.3K
  • 计算机网络体系结构

    计算机网络体系结构计算机网络体系结构认识一下什么是计算机网络体系结构计算机网络是一个非常复杂、庞大的系统,涉及到很多组成部分。包括主机、路由器、各种链路、应用、协议、硬件、软件……等。那么是否存在一种体系结构可以

    2022年7月4日
    20
  • streamsets数据采集平台介绍

    streamsets数据采集平台介绍Streamsets功能介绍Streamsets功能介绍1数据采集组件简介1.1场景分析1.2组件特点1.3数据接入组件2数据采集管道介绍2.1管道特点2.2数据源,处理器,目标源介绍2.3常用数据源组件2.4常用处理器组件2.5常用目标源组件3任务管道介绍3.1详情的任务管道3.2创建采集任务3.3运行任务详细信息4对数据源组件处理器组件目标源组件统计5数据源详细介…

    2022年6月7日
    51

发表回复

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

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