Weighted Random Selection Using Python
In Python, we can easily generate random numbers using the Random and NumPy libraries.
Selecting random elements from a list or array based on the possible outcomes of the elements is called weighted random selection. The selection of elements is determined by assigning a probability to each element present. Sometimes multiple elements are also selected from the list of elements made.
In this tutorial, we will discuss how to generate weighted random selections in Python.
Use random.choices()
the function to generate a weighted random selection
Here, Python’s random
module is used to generate random numbers.
In choices()
the function, weighted random selection is performed with replacement. It is also called weighted random sample with replacement. Moreover, in this function, weights play a vital role. Weights define the possible outcomes of selecting each element. There are two types of weights:
- Relative Weight
- Cumulative weight
Select elements with relative weights
weights
The parameters define the relative weights. For each element in the list, the possible outcomes are different. If the possible outcomes for each element have been determined using relative weights, the selection is made based on the relative weights only.
Here is an example:
import random
List = [12, 24, 36, 48, 60, 72, 84]
print(random.choices(List, weights=(30, 40, 50, 60, 70, 80, 90), k=7))
Here each element in the list has its own weight i.e. possible outcome. Also, k in the above example is the number of elements required in the given list.
Output:
[60, 84, 36, 72, 84, 84, 60]
Here, the sum of the weights does not add up to 100 because they are relative weights and not percentages. The number 84 appears 3 times because it has the highest weight among all the weights. So its probability of occurring is the highest.
Select elements with cumulative weights
cum_weight
The parameter is used to define the cumulative weight. The cumulative weight of an element is determined by the weight of the previous element plus the relative weight of the element. For example, the relative weights [10, 20, 30, 40] are equivalent to the cumulative weights [10, 30, 60, 100]
Here is an example:
import random
List = [13, 26, 39, 52, 65]
print(random.choices(List, cum_weights=(10, 30, 60, 100, 150), k=5))
Output:
[65, 65, 39, 13, 52]
Here too, the number 65 appears more often than any other number because it has the highest weight.
Use numpy.random.choice()
the function to generate a weighted random selection
To generate random weighted selections, NumPy is typically used when using Python versions lower than 3.6.
Here, numpy.random.choice
is used to determine the probability distribution. In this method, a random element of a one-dimensional array is obtained and choice()
a random element of a numpy array is returned using the function.
import numpy as np
List = [500, 600, 700, 800]
sNumbers = np.random.choice(List, 4, p=[0.10, 0.20, 0.30, 0.40])
print(sNumbers)
Here, the probability should be equal to 1. The number 4 represents the size of the list.
Output:
[800 500 600 800]
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Implementing a Low-Pass Filter in Python
Publish Date:2025/05/07 Views:89 Category:Python
-
Low pass filter is a term in signal processing basics and is often used to filter signals to obtain more accurate results. This tutorial will discuss the low-pass filter and how to create and implement it in Python. A low-pass filter is use
Implementing Curl command in Python using requests module
Publish Date:2025/05/07 Views:97 Category:Python
-
requests This article will discuss and implement different curl commands using the module in Python . requests Installing modules in Python Python provides us with requests the module to execute curl command. Install it in Python 3 using Pi
Using fetchall() in Python to extract elements from a database
Publish Date:2025/05/07 Views:171 Category:Python
-
This article aims to describe fetchall() the working methods of extracting elements from a database using and how to display them correctly. This article will also discuss list(cursor) how functions can be used in programs. fetchall() Extra
Parsing log files in Python
Publish Date:2025/05/07 Views:106 Category:Python
-
Log files contain information about events that occurred during the operation of a software system or application. These events include errors, requests made by users, bugs, etc. Developers can further scan these usage details to find poten
Declaring a variable without a value in Python
Publish Date:2025/05/07 Views:57 Category:Python
-
A variable is a reserved memory location that can store some value. In other words, variables in a Python program provide data to the computer to process operations. Every value in Python has a data type. There are numbers, lists, tuples, e
Defining class global variables in Python
Publish Date:2025/05/07 Views:81 Category:Python
-
A global variable is a variable that is visible and available in every part of the program. Global variables are also not defined in any function or method. On the other hand, local variables are defined in functions and can be used only in
Incrementing loop step by 2 in Python
Publish Date:2025/05/07 Views:199 Category:Python
-
In each iteration, for the loop increases the counter variable by a constant. A loop with the sequence 0, 2, 4, 6 for will increase the counter variable by 2 each iteration. This article will show you some for ways to increment by 2 in a lo
Pool map with multiple parameters in Python
Publish Date:2025/05/07 Views:104 Category:Python
-
multiprocessing This article will explain different ways to perform parallel function execution using the module in Python . multiprocessing The module provides functionality to perform parallel function execution using multiple inputs and
Python if...else in Lambda function
Publish Date:2025/05/07 Views:68 Category:Python
-
lambda Functions are used to implement some simple logic in Python and can be thought of as anonymous functions. It can have multiple parameters but only one expression, just def like any other function defined using the keyword. We can def