Updated July 1, 2023
Definition of NumPy random choice
The NumPy random choice() function is a built-in function in the NumPy package of Python. The NumPy random.choice() function generates random samples that find wide applications in data statistics, data analysis, and other fields. It is also useful in probability, machine learning, Bayesian statistics, and similar areas where random sampling is necessary.
Syntax of the jQuery zindex() function:
numpy.random.choice(list , size = None, replace = True, p = None)
Parameters:
- list – This is not an optional parameter, specifying that a one-dimensional array has a random sample.
- size – This optional parameter specifies the size of output random samples of the NumPy array.
- replace – This is an optional parameter specifying whether the sample has to do the replacement or not.
- p – This optional parameter specifies the probability attached for every sample in an array “list.”
- Return value – The return value of this function is the NumPy array of random samples.
Working of NumPy random choice() function
The NumPy random choice() function accepts four parameters. The mandatory parameter is the list or array of elements or numbers. When we pass the list of elements to the NumPy random choice() function, it randomly selects the single element and returns it as a one-dimensional array, but if we specify some size to the size parameter, then it returns the one-dimensional array of that specified size. Note that if just passing the number as a choice(30), then the function randomly selects one number in the range [0,29].
Examples
Example of NumPy random choice() function for generating a single number in the range.
Next, we write the Python code to understand the NumPy random choice() function more clearly with the following example, where the choice() function is used to randomly select a single number in the range [0, 12], as below –
Example #1
Code:
# import numpy package as np
import numpy as np
# Using choice() method of random class
rdm_no = np.random.choice(13)
print( "The output random choice sample number : " )
# printing the generated output random samples of the choice() function
print( rdm_no )
Output:
Again when we run the above program, it will generate another random number, as shown below.
And once again, when we run the above program, it will generate another random number, as shown below.
As in the above program, the number 13 is passed to the choice() function, so the choice() function randomly selects a single number from the range [0, 12]; so in the above output, if we see every time it generates the random number is in the range [0, 12].
Example for randomly generating specified size of numbers
Next, we write the Python code to understand the NumPy random choice() function, where the choice() function is used to randomly generate 10 sizes of numbers in the range [0, 49], as below –
Example #2
Code:
# import numpy package as np
import numpy as np
# Using choice() method of random class
rdm_no = np.random.choice( 50, 10 )
print( "The output of the random choice function for 10 sample numbers are : " )
# printing the generated output random samples of the choice() function
print( rdm_no )
Output:
Again, when we run the above program, it will generate another random number, as we can see below –
And once again, when we run the above program, it will generate another random number, as we can see below –
As in the above program, the numbers 50 and 10 are passed to the choice() function, where the 50 is for the random number range (now the range is [0, 49]) and the 10 is the size of the output array of random numbers, so the choice() function randomly select the 10 number from the range [0, 49], so in the above output if we see every time it generating the random numbers of 10 size which are in the range [0, 49].
Example for a randomly selected element from a given list with replace –
Example #3
Code:
# import numpy package as np
import numpy as np
# List of movies
list_movie = [ 'The Godfather', 'The Shawshank Redemption', 'The Wizard of Oz', 'Citizen Kane', 'Pulp Fiction' ]
# Using choice() method of random class
rdm_no = np.random.choice( list_movie, size = 3, replace = False )
print( "The output of the random choice function for the 2 movie sample numbers are : " )
# printing the generated output random samples of the choice() function
print( rdm_no )
Output:
Again, when we run the above program, it will generate another random number, as we can see below –
And once again, when we run the above program, it will generate another random number, as we can see below –
As in the above program, the list of movies is created, which is passed to the choice() function along with the size=3 and replace=True, so the choice() function randomly selects the 3 elements from the list with replace which means the selected elements may be repeated, as we can see in the above output few elements are repeated in the randomly selected array. Whereas if replace=False, the elements will not repeat in the randomly selected array.
Recommended Articles
This is a guide to NumPy random choice. Here we discuss the Description and Working of the NumPy random choice() function with examples. You may also have a look at the following articles to learn more –