Numpy has many useful functions that allow you to do mathematical calculations over an array efficiently. In fact, It creates an array that performs calculations very fast. There is a Numpy random choice method that creates a random sample array from the given 1D NumPy array. In this entire tutorial, I will discuss it.
Syntax of the Numpy Random Choice Method
Before going to the example part, let’s know the syntax of the function.
numpy.random.choice(a, size=None, replace=True, p=None)
An explanation of the parameters is below.
a Your input 1D Numpy array. size The number of elements you want to generate. replace It Allows you for generating unique elements. The Default is true and is with replacement. p The probabilities of each element in the array to generate.
Examples of Numpy Random Choice Method
Example 1: Uniform random Sample within the range
You can generate an array within a range using the random choice() method. Here You have to input a single value in a parameter. Then define the number of elements you want to generate. The array will be generated.
np.random.choice(10, 5)
Output

You can see in the figure. The five elements have been generated within the range. But there is a repeated element also. And it is 8.
How you can avoid it? You can do so by using the replace argument. It generates unique elements within the range.
Execute the below lines of code.
np.random.choice(10, 5,replace=True)
Output

You can see that all the generated elements are unique.
Example 2: Non -Uniform random Sample within the range
The above case was generating a uniform random sample. Now let’s generate a non-uniform sample. Here each element has some probabilities. The sample will be created according to it. And if you generate the sample using it then random.choice() method, then it includes elements using it.
Secondly, Let p is the list of probabilities of each element.
Run the code given below.
p=[0.1, 0, 0.3, 0.6, 0]
np.random.choice(5,4,p=p)
Output

You can see it in the figure again, the duplicates elements have been included. If you want to get only unique elements then you have to use the replace argument.
p=[0.1, 0, 0.3, 0.6, 0]
np.random.choice(5,3,replace=False,p=p)
Output

Example 3: Random sample from 1D Numpy array
Firstly, Now let’s generate a random sample from the 1D Numpy array. In this example first I will create a sample array. And then use the NumPy random choice method to generate a sample.
Execute the below lines of code to generate it.
array_1d = np.array([1,2,3,4,5,6])
np.random.choice(array_1d,3)
Output

Conclusion
That’s all for now. The numpy random choice method is able to generate both a random sample that is a uniform or non-uniform sample. Hope the above examples have cleared your understanding on how to apply it.
Even,Further, if you have any queries then you can contact us for getting more help.
Source:
Numpy Random Choice Documentation
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.