How to choose elements from the list with different probability using NumPy?


There are multiple ways to choose elements from the list with the different probability using the numpy library.

In python, NumPy library provides a module named random, which has several functions such as choice(), multinomial() etc., which are used to choose elements from an array with different probabilities. The sum of all probability values defined in the list should be equal to 1. Let’s see each way one by one.

Using the random.choice() function

The random module provides the function choice(), which is used to calculate a random sample from the given 1-d array with the specified probability distribution. Following is the syntax for using the choice() function.

numpy.random.choice(list,size = (rows,columns),p = probability_values)

Where,

  • numpy is the library.

  • random is the module in numpy.

  • choice is the function to get the elements with the defined probability

  • size  is the array size with defined number of rows and columns.

  • list  is the list of elements.

  • p is the probability

Example

In the following example, we are passing different probability values and a list as parameters to the choice(), function and getting the elements with specified probabilities.

import numpy as np
list = [10,12,4,5,98]
probabilities = [0.1, 0.2, 0.3, 0.2, 0.2]
arr = np.random.choice(list, size=8, p=probabilities)
print("The array created with the defined probability of elements:", arr)  

Output

The array created with the defined probability of elements: [10  4  5 10 98 12  4 12]

Example

Following is another example, which creates 2-d array with a specified list of elements and list of probabilities using the choice() , function of the numpy library.

import numpy as np
lst = [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(lst, size=8, p=probabilities)
print("The array created with the defined probability of elements:",arr)  

Output

The array created with the defined probability of elements: [[ 0]
 [ 5]
 [ 5]
 [ 4]
 [12]
 [ 4]
 [34]]

Using the random.multinomial() function

The other function available in random module is multinomial() function which generates the elements from the multinomial distribution with the defined number of trails and the probabilities.

Syntax

The following is the syntax of the multinomial() function.

numpy.random.multinomial(n, pvals, size=None)

Where,

  • numpy is the library.

  • random is the module in numpy.

  • multinomial is the function to get the multinomial distribution elements with the defined probability.

  • size is the array size with defined number of rows and columns.

  • n is the number of trials.

  • pvals is the probability.

Example

In the following example, we are creating a 1-d array using multinomial() function, by passing the number of trials, the list of probabilities and size of the array as the input parameters. Then the array will be created with the defined probability of multinomial distribution elements.

import numpy as np
probabilities = [0.1, 0.2, 0.3, 0.2, 0.2]
arr = np.random.multinomial(5, probabilities)
print("The array created with the defined probability of elements:",arr)  

Output

The array created with the defined probability of elements: [0 1 1 3 0]

Example

Following is another example which creates the 2-d array with the multinomial distribution elements as per the defined probability.

import numpy as np
probabilities = [0.1, 0.2, 0.3, 0.2, 0.2]
arr = np.random.multinomial(5, probabilities,size = (2))
print("The array created with the defined probability of elements:",arr)  

Output

The array created with the defined probability of elements: [[1 0 2 2 0]
 [0 1 1 2 1]]

Using the random.default_rng().choice() function

This function is used to generate the array with randomly chosen elements with the defined list of mentioned probabilities. This function is available only in Numpy version of 1.17 or later.

Example

The following is the example of using this function to generate the 1-d array.

import numpy as np
rng = np.random.default_rng()
elements = [1, 2, 3, 4,10,3,4]
arr = rng.choice(elements, p=[0.1, 0.1, 0.2, 0.3,0.1,0.1,0.1],size = 10)
print("The array created with the defined probability of elements:",arr)  

Output

The below is the output of the random.default_rng().choice() used for creating the 1-d array

The array created with the defined probability of elements: [3 3 4 4 1 4 4 3 4 4]

Example

Let’s see another example for creating the 2-d array using the random.default_rng().choice() function of the random module with the defined probabilities.

import numpy as np
rng = np.random.default_rng()
elements = [23,43,42,5,78,90]
arr = rng.choice(elements, p=[0.1, 0.2, 0.2, 0.3,0.1,0.1],size = (5,5))
print("The array created with the defined probability of elements:",arr)  

Output

The array created with the defined probability of elements: [[ 5 78 78 90 43]
 [ 5 78  5 43 78]
 [42  5 42  5 90]
 [ 5 43  5 43 23]
 [78 42  5  5 78]]

Updated on: 09-Aug-2023

483 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements