With numpy.array_splits, you can split an array into equal size chunks. Is there a way to split it into chunks based on a list?
How do I split this array into 4 chunks, with each chunk determined by the size of the chunk given in chunk_size, and consisting of random values from the array?
import numpy as np
np.random.seed(13)
a = np.arange(20)
chunk_size = [10, 5, 3, 2]
dist = [np.random.choice(a, c) for c in chunk_size]
print(dist)
but I get multiple duplications, as expected:
[array([18, 16, 10, 16, 6, 2, 12, 3, 2, 14]),
array([ 5, 13, 10, 9, 11]), array([ 2, 0, 19]), array([19, 11])]
For example,
- 16 is contained twice in the first chunks
- 10 is contained in the first and second chunk
With np.split, this is the answer I get:
>>> for s in np.split(a, chunk_size):
... print(s.shape)
...
(10,)
(0,)
(0,)
(0,)
(18,)
With np.random.choice and replace=False, still gives duplicate elements:
import numpy as np
np.random.seed(13)
a = np.arange(20)
chunk_size = [10, 5, 3, 2]
dist = [np.random.choice(a, c, replace=False) for c in chunk_size]
print(dist)
While each chunk now does not contain duplicates, it does not prevent that, for example, 7 is contained in both the first and second chunk:
[array([11, 12, 0, 1, 8, 5, 7, 15, 14, 13]),
array([16, 7, 13, 9, 19]), array([1, 4, 2]), array([15, 12])]
np.random? Do you want to get contiguous chunks or random elements from the original array?replace=Falsewith it.