0
0
NumpyHow-ToBeginner ยท 3 min read

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=False but requesting more elements than available, causing an error.
  • Providing a probability array p that does not sum to 1 or does not match the size of a.
  • Using an integer a but 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

ParameterDescriptionDefault
aArray or int range to sample fromRequired
sizeNumber of samples to drawNone (1 sample)
replaceAllow repeated samplesTrue
pProbabilities for each elementNone (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.