Random choice with probability python

As of Python v3.6, random.choices could be used to return a list of elements of specified size from the given population with optional weights.

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

  • population : list containing unique observations. (If empty, raises IndexError)

  • weights : More precisely relative weights required to make selections.

  • cum_weights : cumulative weights required to make selections.

  • k : size(len) of the list to be outputted. (Default len()=1)


Few Caveats:

1) It makes use of weighted sampling with replacement so the drawn items would be later replaced. The values in the weights sequence in itself do not matter, but their relative ratio does.

Unlike np.random.choice which can only take on probabilities as weights and also which must ensure summation of individual probabilities upto 1 criteria, there are no such regulations here. As long as they belong to numeric types (int/float/fraction except Decimal type) , these would still perform.

>>> import random
# weights being integers
>>> random.choices(["white", "green", "red"], [12, 12, 4], k=10)
['green', 'red', 'green', 'white', 'white', 'white', 'green', 'white', 'red', 'white']
# weights being floats
>>> random.choices(["white", "green", "red"], [.12, .12, .04], k=10)
['white', 'white', 'green', 'green', 'red', 'red', 'white', 'green', 'white', 'green']
# weights being fractions
>>> random.choices(["white", "green", "red"], [12/100, 12/100, 4/100], k=10)
['green', 'green', 'white', 'red', 'green', 'red', 'white', 'green', 'green', 'green']

2) If neither weights nor cum_weights are specified, selections are made with equal probability. If a weights sequence is supplied, it must be the same length as the population sequence.

Specifying both weights and cum_weights raises a TypeError.

>>> random.choices(["white", "green", "red"], k=10)
['white', 'white', 'green', 'red', 'red', 'red', 'white', 'white', 'white', 'green']

3) cum_weights are typically a result of itertools.accumulate function which are really handy in such situations.

From the documentation linked:

Internally, the relative weights are converted to cumulative weights before making selections, so supplying the cumulative weights saves work.

So, either supplying weights=[12, 12, 4] or cum_weights=[12, 24, 28] for our contrived case produces the same outcome and the latter seems to be more faster / efficient.

Weighted random choices mean selecting random elements from a list or an array by the probability of that element. We can assign a probability to each element and according to that element(s) will be selected. By this, we can select one or more than one element from the list, And it can be achieved in two ways.

  1. By random.choices()
  2. By numpy.random.choice()

Using Random.choices() method

The choices() method returns multiple random elements from the list with replacement. You can weigh the possibility of each result with the weights parameter or the cum_weights parameter.

Syntax : random.choices(sequence, weights=None, cum_weights=None, k=1)

Parameters :
1. sequence is a mandatory parameter that can be a list, tuple, or string.
2. weights is an optional parameter which is used to weigh the possibility for each value.
3. cum_weights is an optional parameter which is used to weigh the possibility for each value but in this the possibility is accumulated
4. k is an optional parameter that is used to define the length of the returned list.

Example 1:

Python3

import random

sampleList = [100, 200, 300, 400, 500]

randomList = random.choices(

  sampleList, weights=(10, 20, 30, 40, 50), k=5)

print(randomList)

Output:

[200, 300, 300, 300, 400]

You can also use cum_weight parameter. It stands for commutative weight. By default, if we will use the above method and send weights than this function will change weights to commutative weight. So to make the program fast use cum_weight. Cumulative weight is calculated by the formula:

Random choice with probability python

let the relative weight of 5 elements are [5,10,20,30,35]

than there cum_weight will be [5,15,35,65,100]

Example:

Python3

import random

sampleList = [100, 200, 300, 400, 500]

randomList = random.choices(

  sampleList, cum_weights=(5, 15, 35, 65, 100), k=5)

print(randomList)

Output:

[500, 500, 400, 300, 400]

Using numpy.random.choice() method

If you are using Python older than 3.6 version, than you have to use NumPy library to achieve weighted random numbers. With the help of choice() method, we can get the random samples of one dimensional array and return the random samples of numpy array.

Syntax: numpy.random.choice(list,k, p=None)

List: It is the original list from you have select random numbers.

k: It is the size of the returning list. i.e, the number of elements you want to select.

p: It is the probability of each element.

Note: the total sum of the probability of all the elements should be equal to 1.

Example:

Python

from numpy.random import choice

sampleList = [100, 200, 300, 400, 500]

randomNumberList = choice(

  sampleList, 5, p=[0.05, 0.1, 0.15, 0.20, 0.5])

print(randomNumberList)

Output:

[200 400 400 200 400]

How do you do random probability in Python?

Use the random. choices() function to get the weighted random samples in Python..
Choose 10 – 10% of the time..
Choose 20 – 25% of the time..
Choose 30 – 50% of the time..
Choose 40 – 15% of the time..

How do I get Python to generate random choices?

To implement a random choice selector in Python, you can use the random. choice() and random. choices() function. These functions allow you to retrieve a single random item and multiple random items from a sequence of items, respectively.

How do you generate a random number from a given distribution in Python?

How to Generate Random Numbers in Python.
To start, import numpy..
The first thing we need to do to generate random numbers in Python with numpy is to initialize a Random Generator. ... .
Random decimal (floating point, or float) numbers. ... .
To generate an array of random float we can pass a size argument to random ..

What is the output of random choice (' ABCD ')?

Explanation: The function random. choice(a,b,c,d) returns a random number which is selected from a, b, c and d. The output can be either a, b, c or d. Hence the output of the snippet of code shown above can be either 10.4, 56.99 or 76.