NumPy – Using random.Generator.choice() method (5 examples)

Updated: February 27, 2024 By: Guest Contributor Post a comment

Introduction

NumPy is an essential library in the Python data science ecosystem, prized for its ability to efficiently perform array operations. Among its numerous capabilities, the np.random.Generator.choice() function stands out for its versatility in random selection tasks. This tutorial will walk you through five examples that demonstrate the function’s utility across various complexities.

Understanding random.Generator.choice()

The choice() method belongs to the NumPy’s random module, specifically within the new random generation system introduced in NumPy 1.17. This system offers enhanced randomness and provides a plethora of methods for generating random numbers. The choice() method, in particular, allows for random selection from a given 1-D array or list, with or without replacement, and with the ability to assign probabilities to each element.

The syntax for NumPy’s random.Generator.choice() method is as follows:

Generator.choice(a, size=None, replace=True, p=None, axis=0, shuffle=True)

Parameters:

  • a: The array or int from which to generate random samples. If an array, it can be a 1-D array or an array-like object. If an int, the random samples are generated from np.arange(a).
  • size: The output shape. If None (the default), a single value is returned.
  • replace: Whether the sample is with or without replacement. Default is True, meaning sampling is done with replacement.
  • p: The probabilities associated with each entry in a. If None (the default), uniform probabilities are assumed.
  • axis: The axis along which the samples are drawn. Default is 0.
  • shuffle: Whether to shuffle the elements of a before taking the sample. Default is True.

Returns:

  • A random sample from a. If size is None, a scalar is returned. If size is given, an array with that shape is returned.

Example 1: Basic Usage

In our first example, let’s explore the most straightforward use of choice(): randomly selecting an element from a list.

import numpy as np
rng = np.random.default_rng()
result = rng.choice(['apple', 'banana', 'cherry'])
print(result)

Output:

apple 

Note: As this is random, the output could be any of the given fruits.

Example 2: Selecting Multiple Elements

Next, let’s pick multiple items from an array, without replacement, to see how choice() can be used to sample a subset.

import numpy as np
rng = np.random.default_rng()
result = rng.choice([1, 2, 3, 4, 5], size=3, replace=False)
print(result)

Output (vary, due to the randomness):

[3, 1, 5]

Example 3: Selecting With Replacement

Choosing elements with replacement allows for the possibility of the same item being selected more than once. This is particularly useful in simulations and bootstrapping methods.

import numpy as np

np.random.seed(2024)

rng = np.random.default_rng()
result = rng.choice([10, 20, 30, 40, 50], size=5, replace=True)
print(result)

Output:

[10 40 40 20 50]

Example 4: Assigning Probabilities

One of the most powerful features of the choice() function is the ability to assign different probabilities to each element, allowing for weighted random selection. This can be incredibly useful for scenarios where fairness isn’t the goal, such as simulating biased experiments or creating diverse testing datasets.

import numpy as np

np.random.seed(2024)

rng = np.random.default_rng()
result = rng.choice(['red', 'blue', 'green', 'yellow'],
                    size=10, p=[0.1, 0.4, 0.2, 0.3])
print(result)

Output:

['yellow' 'yellow' 'yellow' 'red' 'blue' 'yellow' 'green' 'yellow' 'green'
 'blue']

Example 5: Advanced Sampling Tasks

For our final example, we’ll tackle a more sophisticated sampling task, illustrating the flexibility and power of the choice() method.

import numpy as np

np.random.seed(2024)

people = ['Alice', 'Bob', 'Carol', 'Dave']
activities = ['hiking', 'biking', 'swimming', 'running']
rng = np.random.default_rng()
# Simulate a random weekend activity for each person
for person in people:
    activity = rng.choice(activities)
    print(f'{person} will go {activity} this weekend.')

Output:

Alice will go running this weekend.
Bob will go running this weekend.
Carol will go biking this weekend.
Dave will go hiking this weekend.

This demonstration showcases how choice() can be used in simulations to randomly assign activities to people, a common requirement in Monte Carlo simulations or other statistical models.

Conclusion

In this tutorial, we explored five practical examples of using the np.random.Generator.choice() method in NumPy, ranging from simple random selection to more complex simulations involving custom probabilities and without replacement scenarios. These examples only scratch the surface of what’s possible with NumPy’s random module, highlighting its crucial role in data science and statistical computation.