random
module : choices() random.choices()
we can make a weighted random choice with replacement. You can also call it a weighted random sample with replacement. The 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 | Description |
---|---|
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. Default None |
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]. Default None |
k | Optional. An integer defining the length of the returned list |
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.weights
or cum_weights
are used to define the selection probability for each element.First, define the probability for each element. As mentioned above we can define weights sequence using the following two ways :-
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 moduleimport random#use of weighted probabilitynumberList = [111, 222, 333, 444, 555]print(random.choices(numberList, weights=(10, 20, 30, 40, 50), k=5))
Output:
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
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 moduleimport random#use of cumulative weighted probabilitynameList = ["Kelly", "Scott", "Emma", "John"]print(random.choices(nameList, cum_weights=(5, 15, 30, 50), k=4))
Output:
['John', 'Kelly', 'John', 'Scott']
import randomList = ["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)print(x[0])
Output:
Iteration: 0 Random choice isJohnIteration: 1 Random choice isJohnIteration: 2 Random choice isScottIteration: 3 Random choice isScottIteration: 4 Random choice isEmma
As you can see we got John 3 times because it has the highest probability of being selected.
**********END OF ARTICLE********
Comments