Dan Burton
Dan Burton

Reputation: 53665

What is the numpy equivalent of random.sample?

I want to randomly choose 2 elements out of a list.

>>> import random
>>> random.sample(["foo", "bar", "baz", "quux"], 2)
['quux', 'bar']

But I want to use a numpy.random.Generator to do it, rather than using Python's global random number generator. Is there a built-in or easy way to do this?

>>> import numpy as np
>>> gen = np.random.default_rng()
>>> ???

[edit] the point is to make use of gen which allows you to seed it for reproducibility. I realize the same can hypothetically be accomplished by re-seeding global generators, but I specifically want to use gen, a local generator, rather than relying on global generators.

Upvotes: 7

Views: 3900

Answers (2)

Tal Raviv
Tal Raviv

Reputation: 11

The use of np.random.choice, as suggested above, is indeed equivalent if you are sampling from one-dimensional data (such as a list of numbers or strings).

However,

a = [(1,2),(1,4), (2,3), (1,7)]
np.random.choice(a,2)  

will produce an error. Your equivalent, in this case, is

[a[i] for i in np.random.choice(range(len(a)),2, replace=False)]

As commented above, it may be better to use the good old random.sample().

Upvotes: 1

Nathan
Nathan

Reputation: 10306

If you really want to do it from the numpy.random.Generator:

import numpy as np
gen = np.random.default_rng()
gen.choice(["foo", "bar", "baz", "quux"], 2, replace=False)

Note that np.random.choice selects with replacement by default (i.e. each item can be sampled multiple times), so turn this off if you want an equivalent method to random.sample (credit: @ayhan).

Upvotes: 7

Related Questions