Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partition array into N chunks with Numpy

There is this How do you split a list into evenly sized chunks? for splitting an array into chunks. Is there anyway to do this more efficiently for giant arrays using Numpy?

like image 977
Eiyrioü von Kauyf Avatar asked Jan 18 '13 19:01

Eiyrioü von Kauyf


People also ask

How do you split an array into multiple arrays in Python?

Splitting NumPy Arrays Splitting is reverse operation of Joining. Joining merges multiple arrays into one and Splitting breaks one array into multiple. We use array_split() for splitting arrays, we pass it the array we want to split and the number of splits.

How do I split a 2D NumPy array?

An array needs to explicitly import the array module for declaration. A 2D array is simply an array of arrays. The numpy. array_split() method in Python is used to split a 2D array into multiple sub-arrays of equal size.


1 Answers

Try numpy.array_split.

From the documentation:

>>> x = np.arange(8.0) >>> np.array_split(x, 3)     [array([ 0.,  1.,  2.]), array([ 3.,  4.,  5.]), array([ 6.,  7.])] 

Identical to numpy.split, but won't raise an exception if the groups aren't equal length.

If number of chunks > len(array) you get blank arrays nested inside, to address that - if your split array is saved in a, then you can remove empty arrays by:

[x for x in a if x.size > 0] 

Just save that back in a if you wish.

like image 63
Prashant Kumar Avatar answered Sep 28 '22 11:09

Prashant Kumar