jeudi 13 juillet 2017

Using `np.random.choice` for 2D matrices?

np.random.choice is a handy tool for sampling random elements from a 1D array:

In [94]: np.random.choice(np.arange(5), 10)
Out[94]: array([3, 1, 4, 3, 4, 3, 2, 4, 1, 1])

But the docs specify that a param must be one dimensional. But if I want to get a random selection of rows from a 2D array (for example, random samples for a one hot encoder), then np.random.choice cannot be used anymore.

So if my input is:

array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])  

How can I get n rows in random fashion from this array, like this? (n = 10)

array([[ 0.,  0.,  1.],
       [ 1.,  0.,  0.],
       [ 0.,  0.,  1.],
       [ 0.,  0.,  1.],
       [ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 1.,  0.,  0.],
       [ 0.,  0.,  1.],
       [ 1.,  0.,  0.],
       [ 1.,  0.,  0.]])




Aucun commentaire:

Enregistrer un commentaire