Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the groups of consecutive elements in a NumPy array

Tags:

python

numpy

I have to cluster the consecutive elements from a NumPy array. Considering the following example

a = [ 0, 47, 48, 49, 50, 97, 98, 99] 

The output should be a list of tuples as follows

[(0), (47, 48, 49, 50), (97, 98, 99)] 

Here the difference is just one between the elements. It will be great if the difference can also be specified as a limit or a hardcoded number.

like image 239
Shan Avatar asked Sep 08 '11 18:09

Shan


People also ask

How do you find consecutive elements in an array?

Method 1 (Use Sorting) 1) Sort all the elements. 2) Do a linear scan of the sorted array. If the difference between the current element and the next element is anything other than 1, then return false. If all differences are 1, then return true.


1 Answers

def consecutive(data, stepsize=1):     return np.split(data, np.where(np.diff(data) != stepsize)[0]+1)  a = np.array([0, 47, 48, 49, 50, 97, 98, 99]) consecutive(a) 

yields

[array([0]), array([47, 48, 49, 50]), array([97, 98, 99])] 
like image 77
unutbu Avatar answered Sep 28 '22 00:09

unutbu