In this article, we will go through the use of the Numpy random choice() function in Python. One of Numpy’s most powerful features is the random choice function.
Numpy offers a selection of functions for this, but we will focus on np.random.choice and numpy.random.choice in this tutorial as they are the most commonly used ones and have slightly different behavior that can be advantageous in certain cases.
This function generates a list where each element has an equal chance to be picked at any given time, which makes it perfect for picking representative samples from data sets with unknown dimensions or when you want all possible outcomes to occur with equal probability (i.e., sampling without replacement).
Table of Contents
How to Use the Choice Method in numpy?
The choice() method is used to get a random sample from an array. It can be used on any one dimensional array and returns a new numpy array with randomly chosen values.
Syntax
The syntax for using this function is:
numpy.random.choice(a, size=None, replace=True, p=None).
This requires some parameters which are listed below:
1) a – 1-D array of np having samples
2) size – Output shape of random samples
3) replace – Whether the sample is with or without replacement
4) p – Probability attach with every samples in a.
The choice() method can be very useful in different applications so let’s explore what it has to offer. The first argument that we supply is the array of numpy having samples i.e., a. This parameter is required and shouldn’t be ignored as without any type of input, this function doesn’t work properly.
Next up, after choosing our array for processing, we have to decide on an output shape with size. When dealing with random numbers each value needs to be unique which means they cannot repeat more than once within one sample selection (for example if you wanted all your values between 0-99).
If not set correctly, then no outputs will appear; there are two options here either choose None some other number greater than the number of samples within a or supply your desired output shape.
Next, the replace argument is there to tell choice() whether you want each element chosen with replacement ( True ) or without replacement ( False ). Finally we have p which needs to be supplied as a numpy array – this contains probabilities for every value in our sample so if it’s not provided then an array will need to be made first before calling upon choice().
This function can also work on two dimensional arrays but has different parameters being used such as size=None and p=None. In conclusion, the choice method from Numpy is very versatile and useful when wanting random numbers i.e., choosing cards at random from a deck etc.
This can be used in many different applications such as making a machine learning model or simulating probability experiments.
Let’s have a look at an example.
Example Code
import numpy as np
import matplotlib.pyplot as plt
data = np.random.choice(20, 20)
print(data)
count, bins, ignored = plt.hist(data, 20, density = True)
plt.show()
Output


Conclusion
NumPy Random Choice is a function that generates random numbers. This python module can help you generate data for your simulation or model, and it provides different methods to produce the desired results.