高效的 numpy 零阶保持
有没有一种有效的方法使用零阶保持对 numpy 数组进行重新采样?理想情况下,签名类似于 numpy.interp ?
我知道 scipy .interpolate.interp1d,但我确信矢量化替代方案可用于处理此类情况。
Is there an efficient way to resample a numpy array using zero-order hold? Ideally something with a signature like that of numpy.interp?
I'm aware of the scipy.interpolate.interp1d, but I'm sure that a vectorised alternative would be available for dealing with cases like this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
numpy < 1.12
由于您不会插入任何新值,因此最有效的方法是保持原始数组不变并用浮点数对其进行索引。这实际上是零阶保持。
Numpy >= 1.12
numpy v1.11 和 中已弃用浮点数索引在 v1.12 中删除(2017 年 1 月)。上面显示的代码在当前 numpy 版本中引发
IndexError
异常。您可以通过使用包装器访问数组来重现旧 numpy 版本的浮点索引行为,将浮点索引动态转换为整数。当考虑内存效率时,这将避免使用
numpy.interp
或scipy.interpolate.interp1d
。Numpy < 1.12
Since you won't be interpolating any new values, the most efficient way would be to leave the original array as is and index it with floats. This is effectively a zero-order hold.
Numpy >= 1.12
Indexing with floats was deprecated in numpy v1.11 and removed in v1.12 (Jan 2017). The code shown above raises
IndexError
exception in current numpy versions.You can reproduce the float indexing behavior of older numpy versions by using a wrapper to access the array, converting float indices to integers on the fly. When memory efficiency is a concern, this would avoid the need to pre-emptively interpolate intermediate values using
numpy.interp
orscipy.interpolate.interp1d
.Numpy 数组不再允许非整数索引,因此接受的解决方案不再有效。 scipy.interpolate.interp1d 很强大,但在许多情况下不是最快的。这是一个强大的代码解决方案,可以时使用 np.searchsorted,不能时则恢复到 scipy 版本。
这通过了一系列测试用例:
前两个测试的速度测试表明,numpy 解决方案对于高速率数据的二次采样大约快 100 倍,对于低速率数据的超采样大约快 3 倍。
Numpy arrays no longer allow non-integer indexing, so the accepted solution is no longer valid. scipy.interpolate.interp1d is robust, but not the fastest in many cases. Here's a robust code solution that uses np.searchsorted when it can, and reverts to the scipy version when it can't.
This passes a bunch of test cases:
Speed testing on the first two tests show that the numpy solution is about 100X faster for subsampling high rate data, and about 3X faster for supersampling low rate data.
聚会有点晚了,但这是我的想法:
A bit late to the party, but here's what I came up with:
这是 numpy 免费版本,具有相同的签名。数据需要按递增顺序排列 - b/c 当您通过“巧妙”使用列表作为嵌套函数默认值时,它们会被转储(加速 100 倍):
Here's a numpy free version, with the same signature. Data need to be in increasing order- b/c they get dumped as you go via "clever" usage of a list as the nested function default (a factor of 100 speed-up):