Let's propose that we have an array arr
and we want to divide the array into pieces saving the order of elements.
It can be easily done using np.array_split:
import numpy
arr = np.array([0,1,2,3,4,5,6,7,8])
pieces = 3
np.array_split(arr,pieces)
>>> [array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8])]
If arr.size % pieces != 0 the output of np.array_split will be uneven:
arr = np.array([0,1,2,3,4,5,6,7])
pieces = 3
np.array_split(arr,pieces)
>>> [array([0, 1, 2]), array([3, 4, 5]), array([6, 7])]
I am wondering what is the best way to add randomization to the procedure to get the following outputs with equal probability:
>>> [array([0, 1]), array([2, 3, 4]), array([5, 6, 7])]
>>> [array([0, 1, 2]), array([3, 4]), array([5, 6, 7])]
>>> [array([0, 1, 2]), array([3, 4, 5]), array([6, 7])]
I am interested in generalized solution which will also work for other combinations of array size and number of pieces, for example:
arr = np.array([0,1,2,3,4,5,6,7,8,9])
pieces = 6
array_splita list of indices instead of just one number. Then it's up to you to figure out the different splits, e.g.np.array_split(x,[2,5])[3,5],[3,6]