Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to split a numpy array of integers, into chunks that have successive values with a difference below a threshold

I have the following numpy array with positive integers, in ascending order:

import numpy as np

arr = np.array([222, 225, 227, 228, 230, 232, 241, 243, 244, 245, 252, 253, 258])

I want to split it, into parts, where at each part, each number has maximum difference of 2 from the next one.

So the following array should be split as:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[[222], [225,227,228,230,232], [241, 243, 244, 245], [252, 253], [258]]

Any ideas how I can I achieve that ?

>Solution :

You can compute the diff, get the indices of differences above threshold with flatnonzero, and split with array_split:

threshold = 2

out = np.array_split(arr, np.flatnonzero(np.diff(arr)>threshold)+1)

output:

[array([222]),
 array([225, 227, 228, 230, 232]),
 array([241, 243, 244, 245]),
 array([252, 253]),
 array([258])]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading