Numpy’s random choice() function

In this post we will learn how to use Numpy’s choice function. Numpy’s choice() function is a useful tool for selecting random items from a list or array.

The basic syntax of choice() function Numpy’s random module is this

choice(a, size=None, replace=True, p=None)

Here, a is the list or array from which you want to select items, size is the shape of the output array, replace determines whether or not items can be selected multiple times, and p is an array of probabilities for each item in a.
As we can see choice() allows you to specify the size of the output, as well as p, the probability of selecting each item in the list.

import numpy as np

Selecting a single random element from 1-D array with Numpy choice()

Here’s an example of using choice() to select a single random item from a list. We use Numpy’s random generate object to use choice() fucntion.

a = [1, 2, 3, 4, 5]
rng = np.random.default_rng()
rng.choice(a)
# Output could be any of the items in the list

4

Selecting multiple random elements from a list/array with Numpy choice

Here is an example to select multiple items from a list or array by specifying the size argument using Numpy choice().

a = [1, 2, 3, 4, 5]
random_items = rng.choice(a, 3)
print(random_items)  # Output will be an array of 3 random items from the list

Selecting multiple random elements without replacement from a list/array with Numpy choice

By default, Numpy’s choice() selects items with replacement, will allow items to be selected multiple times. If you want to prevent this, you can set the replace parameter to False:

a = [1, 2, 3, 4, 5]

random_items =  rng.choice(a, size=3, replace=False)
print(random_items)  # Output will be an array of 3 random, unique items from the list

Selecting multiple random elements with probability using Numpy choice

Let us assign some probability for each element in our list and randomly pick multiple items using the probability.

For example, an element with high probability is more likely to be randomly picked and an element with low probability is less likely to selected randomly.

To illustrate this we have assigned 0 probability for numbers 1 and 5 in our list, low probabilities for numbers 2 and 4. When we use choice() function randomly select multiple elements, we would not see 1 and 5 as they have zero probabilities. And we will see 3 more often as it has high probability. Similarly we will not 2 and 4 often as they have low probability.

Here is an example of using choice() 3 times with the probability.

a = np.array([1, 2, 3, 4, 5])
rng = np.random.default_rng()
probs = np.array([0.0, 0.2, 0.6, 0.2, 0.0])
rng.choice(a, size=3, p=probs)

array([3, 3, 3])
rng.choice(a, size=3, p=probs)

array([2, 3, 3])
rng.choice(a, size=3, p=probs)

array([2, 3, 2])