How to Use np.random.choice in NumPy: Simple Guide
Use
np.random.choice to randomly select elements from a given array or range. You can specify the number of elements to pick, whether to allow repeats with replace, and assign probabilities with p.Syntax
The basic syntax of np.random.choice is:
a: The array or integer range to choose from.size: Number of elements to select (default is 1).replace: Whether to allow repeated picks (default is True).p: Optional probabilities for each element.
python
np.random.choice(a, size=None, replace=True, p=None)
Example
This example shows how to pick 3 random elements from an array without replacement and with custom probabilities.
python
import numpy as np arr = ['apple', 'banana', 'cherry', 'date'] # Pick 3 fruits without replacement selection = np.random.choice(arr, size=3, replace=False) # Pick 5 fruits with replacement and probabilities prob_selection = np.random.choice(arr, size=5, replace=True, p=[0.1, 0.2, 0.6, 0.1]) print('Without replacement:', selection) print('With replacement and probabilities:', prob_selection)
Output
Without replacement: ['banana' 'date' 'apple']
With replacement and probabilities: ['cherry' 'cherry' 'banana' 'cherry' 'apple']
Common Pitfalls
Common mistakes include:
- Setting
replace=Falsebut requesting more elements than available, causing an error. - Providing a probability array
pthat does not sum to 1 or does not match the size ofa. - Using an integer
abut misunderstanding it as an array.
python
import numpy as np # Wrong: size > array length with replace=False try: np.random.choice([1, 2, 3], size=5, replace=False) except ValueError as e: print('Error:', e) # Wrong: probabilities do not sum to 1 try: np.random.choice([1, 2, 3], size=2, p=[0.5, 0.3, 0.3]) except ValueError as e: print('Error:', e) # Right: use replace=True or adjust size correct = np.random.choice([1, 2, 3], size=5, replace=True) print('Correct selection:', correct)
Output
Error: Cannot take a larger sample than population when 'replace=False'
Error: probabilities do not sum to 1
Correct selection: [3 1 3 2 1]
Quick Reference
| Parameter | Description | Default |
|---|---|---|
| a | Array or int range to sample from | Required |
| size | Number of samples to draw | None (1 sample) |
| replace | Allow repeated samples | True |
| p | Probabilities for each element | None (equal chance) |
Key Takeaways
np.random.choice randomly picks elements from an array or range.
Use replace=False to avoid repeats but ensure size โค array length.
Probabilities can weight the selection with the p parameter.
If a is an integer, it samples from np.arange(a).
Always check that probabilities sum to 1 when using p.