I have a list of vectors:
>>> import numpy as np
>>> num_dim, num_data = 10, 5
>>> data = np.random.rand(num_data, num_dim)
>>> data
array([[ 0.0498063 , 0.18659463, 0.30563225, 0.99681495, 0.35692358,
0.47759707, 0.85755606, 0.39373145, 0.54677259, 0.5168117 ],
[ 0.18034536, 0.25935541, 0.79718771, 0.28604057, 0.17165293,
0.90277904, 0.94016733, 0.15689765, 0.79758063, 0.41250143],
[ 0.80716045, 0.84998745, 0.17893211, 0.36206016, 0.69604008,
0.27249491, 0.92570247, 0.446499 , 0.34424945, 0.08576628],
[ 0.35311449, 0.67901964, 0.71023927, 0.03120829, 0.72864953,
0.60717032, 0.8020118 , 0.36047207, 0.46362718, 0.12441942],
[ 0.1955419 , 0.02702753, 0.76828842, 0.5438226 , 0.69407709,
0.20865243, 0.12783666, 0.81486189, 0.95583274, 0.30157658]])
From the data
, I need to randomly pick 3 vectors, I could do it with:
>>> import random
>>> random.sample(data, 3)
[array([ 0.80716045, 0.84998745, 0.17893211, 0.36206016, 0.69604008,
0.27249491, 0.92570247, 0.446499 , 0.34424945, 0.08576628]), array([ 0.18034536, 0.25935541, 0.79718771, 0.28604057, 0.17165293,
0.90277904, 0.94016733, 0.15689765, 0.79758063, 0.41250143]), array([ 0.35311449, 0.67901964, 0.71023927, 0.03120829, 0.72864953,
0.60717032, 0.8020118 , 0.36047207, 0.46362718, 0.12441942])]
I've checked the docs at http://docs.scipy.org/doc/numpy/reference/routines.random.html and I couldn't figure out whether there is such a functionality in numpy
as random.sample()
.
Is it right that the numpy.random.sample()
isn't the same as random.sample()
?
Is there an equivalence of random.sample()
in numpy
?
Select randomly n elements from a list using choice() The choice() method is used to return a random number from given sequence. The sequence can be a list or a tuple. This returns a single value from available data that considers duplicate values in the sequence(list).
Method 1: We will be using the function shuffle(). The shuffle() function shuffles the rows of an array randomly and then we will display a random row of the 2D array.
Use the random. choices() function to select multiple random items from a sequence with repetition. For example, You have a list of names, and you want to choose random four names from it, and it's okay for you if one of the names repeats.
In Python, you can randomly sample elements from a list with choice() , sample() , and choices() of the random module. These functions can also be applied to a string and tuple. choice() returns one random element, and sample() and choices() return a list of multiple random elements.
As @ayhan confirmed, it can be done as such:
>>> data[np.random.choice(len(data), size=3, replace=False)]
array([[ 0.80716045, 0.84998745, 0.17893211, 0.36206016, 0.69604008,
0.27249491, 0.92570247, 0.446499 , 0.34424945, 0.08576628],
[ 0.35311449, 0.67901964, 0.71023927, 0.03120829, 0.72864953,
0.60717032, 0.8020118 , 0.36047207, 0.46362718, 0.12441942],
[ 0.1955419 , 0.02702753, 0.76828842, 0.5438226 , 0.69407709,
0.20865243, 0.12783666, 0.81486189, 0.95583274, 0.30157658]])
From the docs:
numpy.random.choice(a, size=None, replace=True, p=None)
Generates a random sample from a given 1-D array
The np.random.choice(data, size=3, replace=False)
selects 3 elements from the list of indices of the data
without replacement.
Then data[...]
slices the index and retrieve the indices selected with np.random.choice
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With