Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to choose elements from the list with different probability using NumPy?
NumPy provides several methods to randomly choose elements from a list with different probabilities. The probability values must sum to 1.0. Here are three main approaches using NumPy's random module.
Using numpy.random.choice()
The choice() function randomly samples elements from a 1-D array with specified probabilities ?
Syntax
numpy.random.choice(a, size=None, replace=True, p=None)
Parameters:
a Input array or list of elements
size Output shape (optional)
replace Whether sampling is with replacement (default: True)
p Probabilities for each element (must sum to 1)
Example 1: 1-D Array
import numpy as np
elements = [10, 12, 4, 5, 98]
probabilities = [0.1, 0.2, 0.3, 0.2, 0.2]
arr = np.random.choice(elements, size=8, p=probabilities)
print("Random selection:", arr)
Random selection: [10 4 5 10 98 12 4 12]
Example 2: 2-D Array
import numpy as np
elements = [1, 0, 12, 4, 5, 98, 34]
probabilities = [0.1, 0.1, 0.2, 0.2, 0.2, 0.1, 0.1]
arr = np.random.choice(elements, size=(3, 2), p=probabilities)
print("2-D random selection:")
print(arr)
2-D random selection: [[ 4 5] [12 4] [ 5 34]]
Using numpy.random.multinomial()
The multinomial() function generates samples from a multinomial distribution, returning counts of occurrences ?
Syntax
numpy.random.multinomial(n, pvals, size=None)
Parameters:
n Number of trials
pvals Probabilities for each outcome
size Output shape (optional)
Example
import numpy as np
probabilities = [0.1, 0.2, 0.3, 0.2, 0.2]
# Single trial
result1 = np.random.multinomial(10, probabilities)
print("Single trial (10 samples):", result1)
# Multiple trials
result2 = np.random.multinomial(10, probabilities, size=3)
print("Multiple trials:")
print(result2)
Single trial (10 samples): [1 2 3 2 2] Multiple trials: [[0 1 4 3 2] [2 2 2 2 2] [1 1 3 3 2]]
Using numpy.random.default_rng().choice()
The modern approach using NumPy's new random number generator (available in NumPy 1.17+) ?
Example 1: 1-D Selection
import numpy as np
rng = np.random.default_rng(42) # Seed for reproducibility
elements = [1, 2, 3, 4, 10]
probabilities = [0.1, 0.1, 0.2, 0.3, 0.3]
arr = rng.choice(elements, p=probabilities, size=10)
print("Modern RNG selection:", arr)
Modern RNG selection: [10 4 10 4 4 3 10 4 3 4]
Example 2: 2-D Selection
import numpy as np
rng = np.random.default_rng(42)
elements = [23, 43, 42, 5, 78, 90]
probabilities = [0.1, 0.2, 0.2, 0.3, 0.1, 0.1]
arr = rng.choice(elements, p=probabilities, size=(3, 3))
print("2-D selection with modern RNG:")
print(arr)
2-D selection with modern RNG: [[78 5 43] [ 5 5 42] [43 42 90]]
Comparison
| Method | Output Type | Best For |
|---|---|---|
choice() |
Selected elements | Direct element selection |
multinomial() |
Occurrence counts | Statistical distributions |
default_rng().choice() |
Selected elements | Modern NumPy applications |
Conclusion
Use numpy.random.choice() for simple weighted sampling, multinomial() for count-based distributions, and default_rng().choice() for modern NumPy applications. All methods require probabilities that sum to 1.0.
