Definition of NumPy random choice
The NumPy random choice() function is used to gets the random samples of a one-dimensional array which returns as the random samples of NumPy array. The NumPy random choice() function is a built-in function in the NumPy package of python. The NumPy random choice() function generate random samples which are commonly used in data statistics, data analysis, data-related fields, and all and also can be used in probability, machine learning, Bayesian statistics, and all.
Syntax of the jQuery zindex() function:
numpy.random.choice(list , size = None, replace = True, p = None)
Parameters:
- list – This is not an optional parameter, which specifies that one dimensional array which is having a random sample.
- size – This is an optional parameter, which specifies the size of output random samples of NumPy array.
- replace – This is an optional parameter, which specifies whether the sample is having to do the replacement or not.
- p – This is an optional parameter, which specifies the probability attach 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 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 pass the number as choice(30) then the function randomly select 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 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 number 13 is passed to the choice() function, so the choice() function randomly select the single number from the range [0, 12], so in the above output if we see every time it generates the random number are 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 generating 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 number 50 and 10 are passed to the choice() function, where the 50 is for the random number range (now 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 select element from a given list with replace –
Next, we write the python code to understand the NumPy random choice() function, where the choice() function is used to randomly select an element from a given list with replacing true, as below:
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 select the 3 elements from the list with replace which means the selected elements may be repeat, as we can see in the above output few elements are repeated in the random selected array. Whereas if replace=False then the elements will not repeat in the random selected array.
Conclusion
The NumPy random choice() function is a built-in function in the NumPy package, which is used to gets the random samples of a one-dimensional 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 –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses