What is random choice() using NumPy in Python?
Overview
The random module in Python comes with various methods that return randomly generated data distribution.
-
Data distribution is a function or list that shows all the possible values or intervals of the data.
-
A random distribution is a collection of random numbers that obeys a particular probability density function.
-
The probability density function is the probability of all the values in an array.
Generating random numbers based on defined probabilities
The random module in NumPy generates random numbers based on a defined probability density function. It uses the choice(() function to do so.
The choice() function
The choice() function specifies the probability for each array value and generates a 1D array.
Syntax
choice(list, p=None, size=None)
Parameter value
The choice() function takes the following parameter values:
list: This represents the1Darray with the random values.p: This represents the probability for each array value. They all must sum up to1.size: This represents the size of the output1Darray of the random values.
Code
from numpy import random# generating a random distribution with a defined probabilty density functionmyarray = random.choice([1, 2, 3, 4, 5], p = [0.1, 0.3, 0.5, 0.1, 0.0], size = 50)print(myarray)
Explanation
-
Line 1: We import the
randommodule fromNumPy. -
Line 4: We generate a random distribution, using the
choice()function. We define a probability density function for all the values of the array. This is as follows:- The probability of
1being returned in the distribution is0.1. - The probability of
2being returned in the distribution is0.3. - The probability of
3being returned in the distribution is0.5. - The probability of
4being returned in the distribution is0.1. - The probability of
5being returned in the distribution is0.0.
We give the size of the output
1Darray to be50. - The probability of
-
Line 7: We print the
myarrayarray.
Note: No matter how many times we rerun that code, it never returns the value
5. This is because we already assigned its probability as0.0.