The Wayback Machine - https://web.archive.org/web/20240204103600/https://www.geeksforgeeks.org/numpy-random-choice-in-python/
Open In App
Related Articles

numpy.random.choice() in Python

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

With the help of choice() method, we can get the random samples of one dimensional array and return the random samples of numpy array.

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

Parameters:

1) a – 1-D array of numpy having random samples.

2) size – Output shape of random samples of numpy array.

3) replace – Whether the sample is with or without replacement.

4) p – The probability attach with every samples in a. 

Output : Return the numpy array of random samples.

Example #1 :

In this example we can see that by using choice() method, we are able to get the random samples of numpy array, it can generate uniform or non-uniform samples by using this method.

Python3




# import choice
import numpy as np
import matplotlib.pyplot as plt
  
# Using choice() method
gfg = np.random.choice(13, 5000)
  
count, bins, ignored = plt.hist(gfg, 25, density = True)
plt.show()


Output :

Example #2 :

Python3




# import choice
import numpy as np
import matplotlib.pyplot as plt
  
# Using choice() method
gfg = np.random.choice(5, 1000, p =[0.2, 0.1, 0.3, 0.4, 0])
  
count, bins, ignored = plt.hist(gfg, 14, density = True)
plt.show()


Output :


Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.

Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.


Last Updated : 15 Jul, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials