计算在numpy数组中长度变化的值的连续出现

时间:2021-07-22 21:46:46

Say I have a bunch of numbers in a numpy array and I test them based on a condition returning a boolean array:

假设我在numpy数组中有一堆数字,我根据返回布尔数组的条件测试它们:

np.random.seed(3456)
a = np.random.rand(8)
condition = a>0.5

And with this boolean array I want to count all of the lengths of consecutive occurences of True. For example if I had [True,True,True,False,False,True,True,False,True] I would want to get back [3,2,1].

有了这个布尔数组,我想计算连续出现的True的所有长度。例如,如果我有[True,True,True,False,False,True,True,False,True],我会想要回来[3,2,1]。

I can do that using this code:

我可以使用此代码执行此操作:

length,count = [],0
for i in range(len(condition)):

    if condition[i]==True:
        count += 1
    elif condition[i]==False and count>0:
        length.append(count)
        count = 0

    if i==len(condition)-1 and count>0:
        length.append(count)

    print length

But is there anything already implemented for this or a python,numpy,scipy, etc. function that counts the length of consecutive occurences in a list or array for a given input?

但是有没有已经实现过这个或python,numpy,scipy等函数来计算给定输入的列表或数组中连续出现的长度?

2 个解决方案

#1


12  

Here's a solution using itertools (it's probably not the fastest solution):

这是使用itertools的解决方案(它可能不是最快的解决方案):

import itertools
condition = [True,True,True,False,False,True,True,False,True]
[ sum( 1 for _ in group ) for key, group in itertools.groupby( condition ) if key ]

Out:
[3, 2, 1]

#2


12  

If you already have a numpy array, this is probably going to be faster:

如果你已经有一个numpy数组,这可能会更快:

>>> condition = np.array([True,True,True,False,False,True,True,False,True])
>>> np.diff(np.where(np.concatenate(([condition[0]],
                                     condition[:-1] != condition[1:],
                                     [True])))[0])[::2]
array([3, 2, 1])

It detects where chunks begin, has some logic for the first and last chunk, and simply computes differences between chunk starts and discards lengths corresponding to False chunks.

它检测块开始的位置,具有第一个和最后一个块的逻辑,并简单地计算块启动之间的差异和丢弃与False块相对应的长度。

#1


12  

Here's a solution using itertools (it's probably not the fastest solution):

这是使用itertools的解决方案(它可能不是最快的解决方案):

import itertools
condition = [True,True,True,False,False,True,True,False,True]
[ sum( 1 for _ in group ) for key, group in itertools.groupby( condition ) if key ]

Out:
[3, 2, 1]

#2


12  

If you already have a numpy array, this is probably going to be faster:

如果你已经有一个numpy数组,这可能会更快:

>>> condition = np.array([True,True,True,False,False,True,True,False,True])
>>> np.diff(np.where(np.concatenate(([condition[0]],
                                     condition[:-1] != condition[1:],
                                     [True])))[0])[::2]
array([3, 2, 1])

It detects where chunks begin, has some logic for the first and last chunk, and simply computes differences between chunk starts and discards lengths corresponding to False chunks.

它检测块开始的位置,具有第一个和最后一个块的逻辑,并简单地计算块启动之间的差异和丢弃与False块相对应的长度。