4

Is there a convenient way to split an array such that regardless of the number of elements in each section, the range of values it contains is the same?

Say we have data in range (0, 100). Let the size of the array be 1000. The first 500 elements are all in (0, 20), 300 elements in (20, 40) and so on. I'd like to manipulate values in the subsections split by 20, 40, 60 and 80.

The data could look something like this:

1st div:  0,  0,  0, ... 17, 18
2nd div: 22, 22, 24, ... 37, 39
3rd div: 40, 41, 41, ... 55, 59
4th div: 65, 68, 73, 76, 76
5th div: 93, 96

It's very easy to split an array in equal size sections by the section size. But I'm plotting a trend line using some simple averaging, and the amount of data in each section varies. I know the split points.

It could be made with np.where with a condition like arr > border1 taking only the first element, combining and then splitting, but that seems like a long-ish way of doing things.

Any pointers would be greatly appreciated. I can't be the only one with this problem. Also, if another library does this kind of thing, I'd certainly be open to using it.

2 Answers 2

3

Use np.searchsorted to get those indices that would split into groups and then split those with np.split -

In [41]: np.random.seed(0)

In [42]: a = np.sort(np.random.randint(0,100,(10000)))

In [43]: bins = [20,40,60,80]

In [46]: idx = np.searchsorted(a, bins)

In [47]: np.split(a,idx)
Out[47]: 
[array([ 0,  0,  0, ..., 19, 19, 19]),
 array([20, 20, 20, ..., 39, 39, 39]),
 array([40, 40, 40, ..., 59, 59, 59]),
 array([60, 60, 60, ..., 79, 79, 79]),
 array([80, 80, 80, ..., 99, 99, 99])]
2

If the input elements are already sorted, consider the use of groupby:

import itertools

l=[0,1,5,17,18,22,27,37,39,40,41,48,57,65,68,72,77,79,81,85,88,91,99]

for k, v in itertools.groupby(l, key=lambda x: x//20):
    # k=0, v=[0, 1, 5, 17, 18]
    # k=1, v=[22, 27, 37, 39]
    # k=2, v=[40, 41, 48, 57]
    # ...
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.