How to Use NumPy Random choice() in Python?

Spread the love

NumPy random.choice() function in Python is used to return a random sample from a given 1-D array. It creates an array and fills it with random samples. It has four parameters and using these parameters we can manipulate the random samples of an array.

In this article, I will explain how to use the NumPy random.choice() function and using its syntax and parameters how to generate random samples of a given 1-D array with examples.

1. Quick Examples of random.choice() in NumPy

Following are quick examples of random.choice()


Below are the quick examples
# Example 1: Get the single element from random choice
arr = np.random.choice(7)

# Example 2:  Get an array of uniform random samples 
arr = np.random.choice(5, 5) 

# Example 3: Get the random values without replace
arr1 = np.random.choice(5, 5, replace = False)

# Example 4: Get the random values without replace
arr1 = np.random.choice(5, 5, replace = False)

# Example 5: #Get the Non-random values without replace
arr1 = np.random.choice(5, 3, replace = False, p=[0.1, 0, 0.3, 0.6, 0])

2. Syntax of random.choice()

Following is the syntax of NumPy random.choice() function.


# Syntax of random.choice
random.choice(arr, size=None, replace=True, p=None)

2.1 Parameters of random.choice()

Following are the parameters of random.choice() function.

  • arr – 1-D NumPy array or int. If a ndarray a random sample is generated from its elements.
  • size -(optional) Which specifies the size of the output array of random samples.
  • replace – (optional)Whether the random sample is with or without replacement. Default is True, meaning that a value of arr can be selected multiple times.
  • p – (optional)The probabilities related to each entry in arr.

2.2 Return value of random.choice()

It returns an ndarray of random samples.

3. Usage of NumPy random.choice()

The NumPy random.choice() function is a built-in function in the NumPy module package and is used to create a one-dimensional NumPy array of random samples. We know that the NumPy module is a data manipulation library for Python. Some special tools of NumPy operate on arrays of numbers. For example, manipulation of numeric data is a big task in data analysis and statistics for getting random data samples.

If we pass numpy.arange() to the NumPy random.choice() function, it will randomly select the single element from the sequence and return it. For example, pass the number as a choice(7) then the function randomly selects one number in the range [0,6]. Using this function we will get a different single random element for every execution of the same code. Let’s take an example,


import numpy as np
# Get the single element from random choice
arr = np.random.choice(7)
print( arr)

# Output :
# 4

4. Get Uniform random samples of NumPy Array

Create a uniform random sample from arange() of size 5. For that we specify the size to the size parameter, then it returns the one-dimensional array of the specified size. Let’s take the example,


# Get an array of uniform random samples 
arr = np.random.choice(5, 5)
print(arr)

# Output :
# [2 1 3 2 4]

5. Get Non-Uniform random samples of NumPy Array

Create a non-uniform random sample from arange(5) of size 5. For that, we pass the p parameter as an array, it can be the same size as a given array. It will return probability-related random samples of an array.


# Get an array of Non uniform random samples  
arr1 = np.random.choice(5, 5, p=[0.1, 0, 0.3, 0.6, 0])
print(arr1)

# Output :
# [3 0 3 3 3]

6. Get the Uniform Random sample without Replacement

Create a uniform random sample from arange(5) of size 5 without replacement. 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 then the elements will not repeat in the randomly selected array.


# Get the random values without replace
arr1 = np.random.choice(5, 5, replace = False)
print(arr1)

# Output :
#[3 4 1 2 0]

7. Get the Non-Uniform Random sample without Replacement

Create a non-uniform random sample from arange(5) of size 3 without replacement. For that, pass p parameter of the same size as the given array and set replace = False into this function, it will return Non-repeated and Non-uniform random samples of the given array.


#Get the Non-random values without replace
arr1 = np.random.choice(5, 3, replace = False, p=[0.1, 0, 0.3, 0.6, 0])
print(arr1)

# Output :
# [2 3 0]

8. Get the Graphical presentation of Random Values

Let’s plot the graph of the random values using the matplotlib library.


import matplotlib.pyplot as plt
# Using choice() method
arr = np.random.choice(7, 300)
count, bins, ignored = plt.hist(arr, 25, density=True)
plt.show()
NumPy random choice

Conclusion

In this article, I have explained NumPy random.choice() and using this how we can get the random samples of 1-D NumPy array with examples.

References

Leave a Reply

You are currently viewing How to Use NumPy Random choice() in Python?