Given a , an array of positive integers, you first need to calculate the frequency of each integer. For example, using bincount :
>>> a = [2,3,4,4,4,4,4,4,5,6,7,8,9,4,9,2,3,6,3,1] >>> b = np.bincount(a)
b indicates the frequency of each integer in a . The corresponding set of weights is therefore an array of b/len(a) . Using np.random.choice with these weights and replace=False , you should give you what you need:
>>> np.random.choice(np.arange(len(b)), 5, p=b/len(a), replace=False) array([5, 9, 4, 3, 8]) >>> np.random.choice(np.arange(len(b)), 5, p=b/len(a), replace=False) array([7, 4, 6, 9, 1]) >>> np.random.choice(np.arange(len(b)), 5, p=b/len(a), replace=False) array([3, 7, 4, 9, 6])
If you are not working only with positive integers or working with large positive integers, @ user2357112 indicates in the comments below that np.unique provides a different solution. Here you should write:
>>> choices, counts = np.unique(a, return_counts=True) >>> np.random.choice(choices, 5, p=counts/len(a), replace=False) array([9, 8, 2, 4, 5])