在numpy数组中找到相同值的序列的长度(游程长度编码)

在pylab程序中(也可能是matlab程序),我有一个代表距离的数字的numpy数组:d[t]是时间上的距离t(我的数据的时间跨度是len(d)时间单位)。


我感兴趣的事件是距离小于某个阈值时,并且我想计算这些事件的持续时间。使用轻松获得布尔数组很容易b = d<threshold,问题归结为计算中的True-only单词的长度顺序b。但是我不知道如何有效地做到这一点(即使用numpy原语),我求助于遍历数组并进行手动更改检测(即,当值从False变为True时初始化计数器,只要value为True便增加计数器,并在值返回False时将计数器输出到序列。但这非常慢。


如何有效地检测numpy数组中的那种序列?


以下是一些说明我的问题的python代码:第四个点需要很长时间才能显示(如果没有,请增加数组的大小)


from pylab import *


threshold = 7


print '.'

d = 10*rand(10000000)


print '.'


b = d<threshold


print '.'


durations=[]

for i in xrange(len(b)):

    if b[i] and (i==0 or not b[i-1]):

        counter=1

    if  i>0 and b[i-1] and b[i]:

        counter+=1

    if (b[i-1] and not b[i]) or i==len(b)-1:

        durations.append(counter)


print '.'


慕勒3428872
浏览 761回答 3
3回答

临摹微笑

尽管不是numpy原始itertools函数,但函数通常非常快,因此请尝试一下(当然,还要测量包括该函数在内的各种解决方案的时间):def runs_of_ones(bits):&nbsp; for bit, group in itertools.groupby(bits):&nbsp; &nbsp; if bit: yield sum(group)如果确实需要列表中的值,那么当然可以使用list(runs_of_ones(bits));但也许列表理解仍然会稍微快一些:def runs_of_ones_list(bits):&nbsp; return [sum(g) for b, g in itertools.groupby(bits) if b]转向“ numpy-native”的可能性,那么:def runs_of_ones_array(bits):&nbsp; # make sure all runs of ones are well-bounded&nbsp; bounded = numpy.hstack(([0], bits, [0]))&nbsp; # get 1 at run starts and -1 at run ends&nbsp; difs = numpy.diff(bounded)&nbsp; run_starts, = numpy.where(difs > 0)&nbsp; run_ends, = numpy.where(difs < 0)&nbsp; return run_ends - run_starts再说一遍:请确保在为您量身定制的示例中相互比较基准解决方案!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python