Numpy random.choice – Python NumPy random.choice() Function

Numpy random.choice: The random module is part of the NumPy library. This module includes the functions for generating random numbers. This module includes some basic random data generating methods, as well as permutation and distribution functions and random generator functions.

NumPy random.choice() Function:

np.random.choice(): The choice() function of the NumPy random module is used to generate a random sample from a specified 1-D array.

Syntax:

numpy.random.choice(a, size=None, replace=True, p=None)

Parameters

a: This is required. When an ndarray is specified, a random sample is generated from its elements. If the parameter is an int, the random sample is created as if “a” were np.arange (a).

size: This is optional. It specifies the shape of the output. If the supplied shape is (a, b, c), for example, a * b * c samples are drawn. The default value is None, which results in a single item being returned.

replace: This is optional. A boolean indicating whether the sample is with or without replacement.

p: This is optional. Indicate the probabilities associated with each entry in “a.” If no sample is provided, the sample assumes a uniform distribution over all elements in “a.”

Return Value:

The randomly generated samples are returned.

NumPy random.choice() Function in Python

Example1

Numpy random choice: Here, the random.choice() function is used to produce random samples from a provided list.

Approach:

  • Import numpy module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass the above-given list, size(row_size, col_size) as arguments to the random.choice() function to get random samples from the given list.
  • Store it in a variable.
  • Print the random samples from the given list of the size given.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np  
# Give the list as static input and store it in a variable.
gvn_lst = [4, 3, 1, 9, 6]
# Pass the above given list, size(row_size, col_size) as arguments to the 
# random.choice() function to get random samples from the given list.
# Store it in a variable.
rslt = np.random.choice(gvn_lst, (2,2))
# Print the random samples from the given list of the size given.
print(rslt)

Output:

[[4 1]
 [6 3]]

Example2

The replace argument draws a sample with replacement.

Approach:

  • Import numpy module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass the above-given list, size(row_size, col_size), and replace as “True” as arguments to the random.choice() function to get random samples from the given list.
  • Store it in a variable.
  • Print the random samples from the given list of the size given.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np  
# Give the list as static input and store it in a variable.
gvn_lst = [4, 3, 1, 9, 6]
# Pass the above given list, size(row_size, col_size) and replace as "True" as arguments to the 
# random.choice() function to get random samples from the given list.
# Store it in a variable.
rslt = np.random.choice(gvn_lst, (2,2), True)
# Print the random samples from the given list of size given.
print(rslt)

Output:

[[1 4]
 [9 9]]

Example3

We can give probability to each entry in the input array or sequence using the p parameter.

Approach:

  • Import numpy module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Give the list of probabilities as static input and store it in another variable.
  • Pass the above-given list, size(row_size, col_size), replace as “True” and probabilities list as arguments to the random.choice() function to get random samples from the given list.
  • Store it in a variable.
  • Print the random samples from the given list of the size given.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np  
# Give the list as static input and store it in a variable.
gvn_lst = [4, 9, 10, 15, 18, 19]
# Give the list of probabilities as static input and store it in another variable.
probity_lst = [0.2, 0.3, 0.1, 0.2 , 0.1, 0.1]
# Pass the above given list, size(row_size, col_size), replace as "True"
# and probabilities list as arguments to the random.choice() function
# to get random samples from the given list.
# Store it in a variable.
rslt = np.random.choice(gvn_lst, (3,3), True, probity_lst)
# Print the random samples from the given list of size given.
print(rslt)

Output:

[[ 9 9 4]
 [ 4 18 9]
 [10 19 15]]