Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy split array into chunks of equal size with remainder

Tags:

python

numpy

Is there a numpy function that splits an array into equal chunks of size m (excluding any remainder which would have a size less than m). I have looked at the function np.array_split but that doesn't let you split by specifying the sizes of the chunks.

An example of what I'm looking for is below:

X = np.arange(10)
split (X, size = 3)
-> [ [0,1,2],[3,4,5],[6,7,8], [9] ]

split (X, size = 4)
-> [ [0,1,2,3],[4,5,6,7],[8,9]]

split (X, size = 5)
-> [ [0,1,2,3,4],[5,6,7,8,9]]
like image 508
pinnochio Avatar asked Sep 28 '20 10:09

pinnochio


People also ask

How do you split a list into evenly sized chunks?

The easiest way to split list into equal sized chunks is to use a slice operator successively and shifting initial and final position by a fixed number.

How to split an array into equal parts in NumPy?

We will use the splitting operation of NumPy arrays to split arrays into rows or columns. How to split NumPy arrays horizontally into equal parts? We can use the hsplit () method of NumPy module to split a given NumPy array into equal parts into columns.

How to split an array into chunks in Python?

The basic logic is to compute the parameters for splitting the array and then use array_split to split the array along each axis (or dimension) of the array. import math import numpy a = numpy.random.choice ( [1,2,3,4], (1009,1009)) First store the shape of the final chunk size along each dimension that you want to split it into in a tuple:

How to split an array into multiple sub-arrays?

For an array of length l that should be split into n sections, it returns l % n sub-arrays of size l//n + 1 and the rest of size l//n. Split array into multiple sub-arrays of equal size. >>> x = np.arange(8.0) >>> np.array_split(x, 3) [array ( [0., 1., 2.]), array ( [3., 4., 5.]), array ( [6., 7.])]

How to split an array to get the maximum possible shape?

This can be done so that the resulting arrays have shapes slightly less than the desired maximum or so that they have exactly the desired maximum except for some remainder at the end. The basic logic is to compute the parameters for splitting the array and then use array_split to split the array along each axis (or dimension) of the array.


1 Answers

Here's one way with np.split + np.arange/range -

def split_given_size(a, size):
    return np.split(a, np.arange(size,len(a),size))

Sample runs -

In [140]: X
Out[140]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [141]: split_given_size(X,3)
Out[141]: [array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8]), array([9])]

In [143]: split_given_size(X,4)
Out[143]: [array([0, 1, 2, 3]), array([4, 5, 6, 7]), array([8, 9])]

In [144]: split_given_size(X,5)
Out[144]: [array([0, 1, 2, 3, 4]), array([5, 6, 7, 8, 9])]
like image 68
Divakar Avatar answered Oct 06 '22 00:10

Divakar