1

So if the shape is 100,10 the list should have 100 elements. Each element is a numpy array of shape (10,)

Sid Anand
  • 167
  • 1
  • 10
  • `list(arr)` makes a list of arrays; `arr.tolist()` makes a list of lists. `tolist` is faster. – hpaulj Oct 04 '21 at 21:05
  • Just curious, why do you want this, OP? – ddejohn Oct 04 '21 at 21:31
  • @ddejohn One example when this can be useful are scenarios where you are forced to iterate over an axis of your array using a `for` loop because the loop's body can not be vectorized. – FirefoxMetzger Oct 05 '21 at 16:27
  • @FirefoxMetzger you can iterate over an array without needing to convert it to a list though. Can you give an explicit example? I'm not sure I follow. – ddejohn Oct 05 '21 at 16:39
  • 1
    @ddejohn Indeed you can. It will, however, be slower than via `np.split` or `np.ndarray.tolist`, because you pay the cost of creating a new `np.view` at each iteration. `tolist` and `split` can create a series of objects in one go which is slightly faster. It is a pretty niche use-case though. Here is a related question: https://stackoverflow.com/questions/40593444/what-is-the-quickest-way-to-iterate-through-a-numpy-array – FirefoxMetzger Oct 05 '21 at 16:48

1 Answers1

1

Anything wrong with np.split?

Here is a quick example:

import numpy as np

some_array = np.random.rand(100, 10)
list_of_arrays = [np.squeeze(x, axis=0) for x in np.split(some_array, len(some_array))]

# some assertions that it does what you ask
assert len(list_of_arrays) == 100
assert isinstance(list_of_arrays, list)
assert all([isinstance(x, np.ndarray) for x in list_of_arrays])
assert all([x.shape == (10,) for x in list_of_arrays])

If you are okay with a shape of (1, 10) you can save the list comprehension:

import numpy as np

some_array = np.random.rand(100, 10)
list_of_arrays = np.split(some_array, len(some_array))
FirefoxMetzger
  • 2,880
  • 1
  • 18
  • 32