Estilus Estilus - 10 months ago 78
Python Question

Average Number of Repeating Patterns with Numpy

I have an arbitrary array with only binary values- say:

a = np.array([1,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,1,1,0,])

What would be the most efficient way to count average length of sequences of 1s array? Eg. In this example, it would be (1 + 8 + 2)/3.


I'm not sure easiest, but one alternative is

np.mean([len(list(v)) for k,v in itertools.groupby(a) if k])


Explanation groupby groups adjacent equal values together, we filter only ones (if k), the list comprehension [...] creates the list of the lengths of sub sequences of ones, i.e. [1,8,2], and mean computes the average value.