choices() method returns a list with the randomly selected element from the specified sequence.You can weigh the possibility of each result with the weights parameter or the cum_weights parameter.The sequence can be a string, a range, a list, a tuple or any other kind of sequence.
random.choices(sequence, weights=None, cum_weights=None, k=1)
Parameter Values :
|sequence||Required. A sequence like a list, a tuple, a range of numbers etc.|
|weights||Optional. A list were you can weigh the possibility for each value.|
|cum_weights||Optional. A list were you can weigh the possibility for each value, only this time the possibility is accumulated.|
Example: normal weights list: [2, 1, 1] is the same as this cum_weights list; [2, 3, 4].
|k||Optional. An integer defining the length of the returned list|
Return Value :
Every time output will be different as the system returns random elements.
Note: You cannot specify both weights and cum_weights.
random.choices() return a k sized list of elements chosen from the population with replacement.
cum_weights are used to define the selection probability for each element.
- If a weights sequence is specified, random selections are made according to the relative weights.
- Alternatively, if a cum_weights sequence is given, the random selections are made according to the cumulative weights.
First, define the probability for each element. As mentioned above we can define weights sequence using the following two ways :-
- Relative weights
- Cumulative weights(cum_weights)
Relative weights to choose elements from the list with different probability :
If you specified the relative weight, the selections are made according to the relative weights. You can specify relative weights using weight parameter.
Example: Choose 5 elements from the list with different probability
#importing random module
#use of weighted probability
numberList = [111, 222, 333, 444, 555]
print(random.choices(numberList, weights=(10, 20, 30, 40, 50), k=5))
[555, 222, 555, 222, 555]
- As you can see in the output, we got 555 three times because we specified the highest weight for it. So it has the highest probability to be selected
- We specified K=5 to choose 5 elements, you can specify any number you want. for example, to choose only 1 element you can specify k=1.
- Weights sum is not 100 because they're relative weights, not percentages.
The weighted probability of selecting each element is determined by the following rule.
Probability = element_weight/ sum of all weights
In the above example, the probability of occurring each element is determined is as follows
The total weight is 10+20+30+40+50 = 150
List is [111, 222, 333, 444, 555]
It returns 111 with probability 10/150 = 0.06
It returns 222 with probability 20/150 = 0.13
It returns 333 with probability 30/150 = 0.20
It returns 444 with probability 40/150 = 0.26
It returns 555 with probability 50/150 = 0.33
Cumulative weights to choose elements from the list with different probability :
Alternatively, if a cum_weights sequence is given, the selections are made according to the cumulative weights. You can specify cumulative weights using the cum_weights parameter.
Note: Python converts the relative weights to cumulative weights before making selections. So, I suggest you pass cumulative weights to saves time and extra work
The cumulative weight of each element is determined by using the following formula.
cum_weight= Weight of previous element + own weight.
For example, the relative weights [5, 10, 15, 20] are equivalent to the cumulative weights [5, 15, 30, 50].
Example: Choose 4 elements from the list with different probability
#importing random module
#use of cumulative weighted probability
nameList = ["Kelly", "Scott", "Emma", "John"]
print(random.choices(nameList, cum_weights=(5, 15, 30, 50), k=4))
['John', 'Kelly', 'John', 'Scott']
Example: Choose a single element form list with different probability
List = ["Kelly", "Scott", "Emma", "John"]
for i in range(5):
print("Iteration:", i, "Random choice is ")
x= random.choices(List, cum_weights=(5, 15, 30, 50), k=1)
Iteration: 0 Random choice is
Iteration: 1 Random choice is
Iteration: 2 Random choice is
Iteration: 3 Random choice is
Iteration: 4 Random choice is
As you can see we got John 3 times because it has the highest probability of being selected.
Points to remember before implementing weighted random choices :
- If you don't specify the relative or cumulative weight, random.choices() will choose elements with equal probability.
- The specified weights sequence must be of the same length as the population sequence.
- Don't specify relative weights and cumulative weight at the same time to avoid Type Error.(TypeError: Cannot specify both weights and cumulative weights)
- You can specify The weights or cum_weights only as integers, floats, and fractions but excludes decimals.
- Weights must be non-negative.
**********END OF ARTICLE********