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.
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.
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])]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With