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 20 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
>>> a = np.arange(0, 3286, 1)
>>> chunk_size = [975, 708, 515, 343, 269, 228, 77, 57, 42, 33, 11, 9, 7, 4, 3, 1, 1, 1, 1, 1]
I tried using something like this:
[np.random.choice(a,_) for _ chunk_size]
but I get multiple duplications, as expected.
With np.split
, this is the answer I get:
>>> for _ in np.split(a, chunk_size): print (_.shape)
...
(975,)
(0,)
(0,)
(0,)
(0,)
(0,)
(0,)
(0,)
(0,)
(0,)
(0,)
(0,)
(0,)
(0,)
(0,)
(0,)
(0,)
(0,)
(0,)
(0,)
(3285,)
With np.random.choice
and replace=False
, still gives duplicate elements
import numpy as np
np.random.seed(13)
a = np.arange(0, 3286, 1)
chunk_size = [975, 708, 515, 343, 269, 228, 77, 57, 42, 33, 11, 9, 7, 4, 3, 1, 1, 1, 1, 1]
dist = [np.random.choice(a,_, replace=False) for _ in chunk_size]
for _ in dist:
if 2828 in list(_):
print ("found")
Aucun commentaire:
Enregistrer un commentaire