Nicola Vianello
Nicola Vianello

Reputation: 1946

strange behaviour of numpy array_split

I do not understand the behaviour of numpy.array_split with subindices. Indeed when I consider an array of a given length, I determine a subindices and I try to use array_split. I obtain different behaviour if the number of subindices is odd or even. Let's make an example

import numpy as np
a = np.ones(2750001) # fake array
t = np.arange(a.size) # fake time basis
indA = ((t>= 5e5) & (t<= 1e6)) # First subindices odd number
indB = ((t>=5e5+1) & (t<= 1e6)) # Second indices even number
# now perform array_split
print(np.shape(np.array_split(a[indA],10)))
# (10,)
print(np.shape(np.array_split(a[indB],10)))
# (10, 50000)

Now we have different results, basically for the even number we have that the shape command gives actually (10,50000) whereas the shape command in case of odd indices gives (10,) (the 10 lists supposed). I'm a bit surprise actually and I would like to understand the reason. I know that array_split can be used also when the number of splitting does not equally divide the array. But I would like some clue also because I need to insert in a loop where I do not know a priori if the indices will be even or odd.

Upvotes: 2

Views: 786

Answers (1)

unutbu
unutbu

Reputation: 879321

I think the suprising behavior has more to do with np.shape than np.array_split:

In [58]: np.shape([(1,2),(3,4)])
Out[58]: (2, 2)

In [59]: np.shape([(1,2),(3,4,5)])
Out[59]: (2,)

np.shape(a) is showing the shape of the array np.asarray(a):

def shape(a):
    try:
        result = a.shape
    except AttributeError:
        result = asarray(a).shape
    return result

So, when np.array_split returns a list of arrays of unequal length, np.asarray(a) is a 1-dimensional array of object dtype:

In [61]: np.asarray([(1,2),(3,4,5)])
Out[61]: array([(1, 2), (3, 4, 5)], dtype=object)

When array_split returns a list of arrays of equal length, then np.asarray(a) returns a 2-dimensional array:

In [62]: np.asarray([(1,2),(3,4)])
Out[62]: 
array([[1, 2],
       [3, 4]])

Upvotes: 2

Related Questions