[英]How to replace identical consecutive values from a 1D array efficiently
Surprisingly, after a fair bit of research, I did not find any post sparking a good idea to solve this simple problem.令人惊讶的是,经过相当多的研究,我没有发现任何帖子引发了解决这个简单问题的好主意。
I have a 1D numpy
array of shape (n, )
which is mostly zeros with a few other positive values.我有一个形状为
(n, )
的一维numpy
数组,它大部分是零和一些其他正值。
D = np.zeros(10)
D[2] = 1
D[5] = 4
D[7] = 3
Out: array([0., 0., 1., 0., 0., 4., 0., 3., 0., 0.])
However, sometimes, one of the values (which is a trigger at the given sample) might be repeated during the next (or few next samples).但是,有时,其中一个值(它是给定样本的触发器)可能会在下一个(或几个下一个样本)期间重复。
D = np.zeros(10)
D[2] = 1
D[3] = 1
D[5] = 4
D[7] = 3
Out: array([0., 0., 1., 1., 0., 4., 0., 3., 0., 0.])
In this case, I want to process the array to replace the identical consecutive values by 0;在这种情况下,我想处理数组以用 0 替换相同的连续值; in this case, to replace the second
1
by 0
.在这种情况下,将第二个
1
替换为0
。
In: array([0., 0., 1., 1., 0., 4., 0., 3., 0., 0.])
Out: array([0., 0., 1., 0., 0., 4., 0., 3., 0., 0.])
For one additional complexity level, I would like to define a tolerance, and if every element right after a value x>0
is close enough to x
, then they get replaced by 0.对于一个额外的复杂度级别,我想定义一个容差,如果值
x>0
之后的每个元素都足够接近x
,那么它们将被 0 替换。
In: array([0., 0., 1., 0.98, 1.01, 4., 0., 3., 3.1, 0.]), tolerance = 0.05
Out: array([0., 0., 1., 0., 0., 4., 0., 3., 3.1, 0.])
This is fairly easy to do by looping on the array with either a while
or a for
loop.这很容易通过使用
while
或for
循环在数组上循环来实现。 However, as this is for an online application, I was looking for a vectorized solution in numpy.然而,由于这是一个在线应用程序,我一直在 numpy 中寻找矢量化解决方案。 If anyone has any suggestion, any function I could look at to do this as efficiently as possible, please comment!
如果有人有任何建议,任何 function 我可以考虑尽可能高效地执行此操作,请发表评论!
Try using np.diff()
like so:尝试像这样使用
np.diff()
:
D[np.where(np.diff(D)==0)] = 0
Edit: Including OP's solution to include tolerance and prepend:编辑:包括OP的解决方案,包括公差和前置:
D[np.where(np.diff(D, prepend=[0])<=tolerance)] = 0
Edit 2: @obchardon's excellent suggestion to remove the unnecessary np.where
:编辑 2:@obchardon 删除不必要的
np.where
的极好建议:
D[abs(np.diff(D, prepend=[0]))<=tolerance] = 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.