Open In App

Choose element(s) from List with different probability in Python

Improve
Improve
Like Article
Like
Save
Share
Report

Have you ever wondered how to select random elements from a list with different probability in Python? In this article, we will discuss how to do the same. Let’s first consider the below example.

Python3




import random
  
sam_Lst = [10, 20, 3, 4, 100]
ran = random.choice(sam_Lst)
print(ran)


In the above example, the probability of getting any element from the list is equal. But we want such methods in which the probability of choosing one element from the list is different. This is known as the weighted random choice in Python. In order to find weighted random choices in Python, there exist two ways:

  1. Relative weights
  2. Cumulative weights

The function which will help us in this situation is random.choices(). This function allows making weighted random choices in python with replacement.

Syntax:

random.choices(population, weights=None, *, cum_weights=None, k=1)

Here, the ‘weight’ parameter plays an important role.

Case 1: Using Relative weights 

The weight assigned to an element is known as relative weight.

Example 1: 

Python3




import random
  
# Creating a number list
num_lst = [1, 22, 43, 19, 13, 29]
  
print(random.choices(num_lst, weights=(
  14, 25, 30, 45, 55, 10), k=6))


Output:

[19, 19, 13, 22, 13, 13]

In the above example, we assign weights to every element of the list. The weight of the element ‘13′ is highest i.e 55, so the probability of its occurrence is maximum. As we can see in the output, element 13 occurs 3 times, 19 occurs 2 times, and so on. So, now the probability of choosing an element from the list is different. 

Example 2: 

Python3




import random
  
# Creating a name list
name_lst = ['October', 'November', 'December',
            'January', 'March', 'June']
  
print(random.choices(name_lst, weights=(
    40, 25, 30, 5, 15, 80), k=3))


Output:

['June', 'October', 'June']

In the above example, the weight of element ‘June’ is maximum so its probability of selection will be maximum. And here, k=3 which means we are choosing only the top 3 elements from the list.

Example 3:

Python3




import random
  
# Creating a name list
name_lst = ['Fit', 'Infected', 'Recovered', 'Danger']
  
# Using FOR loop
# to choose the element from list with
# different probability
for i in range(6):
    print("Random choice", i+1)
      
    randomele = random.choices(name_lst, weights=(
      50, 80, 10, 5), k=1)
      
    print(randomele[0])


Output:

Random choice 1
Recovered
Random choice 2
Danger
Random choice 3
Infected
Random choice 4
Infected
Random choice 5
Infected
Random choice 6
Fit

In the above example, we use FOR loop to choose an element from a list with a different probability.

Case 2: Using Cumulative weights

The cumulative weight of an element is determined by adding the weight of its previous element and its own weight.

Example 1: 

Python3




import random
  
# Creating a number list
num_lst = [1, 22, 93, 19, 13, 25]
  
print(random.choices(num_lst, cum_weights=(
    7, 13, 15, 20, 25, 20), k=6))


Output:

[1, 22, 93, 22, 19, 1]

In the above example, the cumulative weight of element ‘19′ is maximum, so the probability of its selection will also be maximum.

Example 2:

Python3




import random
  
# Creating a name list
name_lst = ['October', 'November', 'December',
            'January', 'March', 'June']
  
print(random.choices(name_lst, cum_weights=(
  40, 20, 3, 7, 15, 15), k=3))


Output:

['January', 'March', 'January']

In the above example, we choose k=3 so we get the top 3 elements that have the maximum probability of selection.

Example 3:

Python3




import random
  
# Creating a name list 
name_lst = ['October', 'November', 'December'
            'January', 'March', 'June']
  
# Using FOR loop
# to choose the element from list with
# different probability
for i in range(6):
    print("Random choice", i+1)
      
    randomele = random.choices(name_lst, cum_weights=(
      7, 13, 15, 20, 25, 20), k=1)
      
    print(randomele[0])


Output:

Random choice 1
November
Random choice 2
January
Random choice 3
October
Random choice 4
December
Random choice 5
November
Random choice 6
January

In the above example, we use FOR loop to choose an element from a list with a different probability.

Note: The value of k depends on the users, and since its relative weight so the total sum of weights can exceed 100.



Last Updated : 11 Oct, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads