2

I was surprised that numpy.split yields a list and not an array. I would have thought it would be better to return an array, since numpy has put a lot of work into making arrays more useful than lists. Can anyone justify numpy returning a list instead of an array? Why would that be a better programming decision for the numpy developers to have made?

kilojoules
  • 7,600
  • 17
  • 63
  • 123

2 Answers2

4

A comment pointed out that if the slit is uneven, the result can't be a array, at least not one that has the same dtype. At best it would be an object dtype.

But lets consider the case of equal length subarrays:

In [124]: x = np.arange(10)
In [125]: np.split(x,2)
Out[125]: [array([0, 1, 2, 3, 4]), array([5, 6, 7, 8, 9])]
In [126]: np.array(_)     # make an array from that
Out[126]: 
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])

But we can get the same array without split - just reshape:

In [127]: x.reshape(2,-1)
Out[127]: 
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])

Now look at the code for split. It just passes the task to array_split. Ignoring the details about alternative axes, it just does

sub_arys = []
for i in range(Nsections):
    # st and end from `div_points
    sub_arys.append(sary[st:end])
return sub_arys

In other words, it just steps through array and returns successive slices. Those (often) are views of the original.

So split is not that sophisticate a function. You could generate such a list of subarrays yourself without a lot of numpy expertise.

Another point. Documentation notes that split can be reversed with an appropriate stack. concatenate (and family) takes a list of arrays. If give an array of arrays, or a higher dim array, it effectively iterates on the first dimension, e.g. concatenate(arr) => concatenate(list(arr)).

miradulo
  • 24,374
  • 5
  • 66
  • 85
hpaulj
  • 175,871
  • 13
  • 170
  • 282
-1

Actually you are right it returns a list

import numpy as np 
a=np.random.randint(1,30,(2,2))
b=np.hsplit(a,2)
type(b)

it will return type(b) as list so, there is nothing wrong in the documentation, i also first thought that the documentation is wrong it doesn't return a array, but when i checked

type(b[0])
type(b[1])

it returned type as ndarray.

it means it returns a list of ndarrary's.

akki
  • 1
  • 2