Given a
, an array of positive integers, you'll first need to compute 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
tells you the frequency of each integer in a
. The corresponding set of weights is therefore the array b/len(a)
. Using np.random.choice
with these weights and replace=False
should then 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're not working with only positive integers, or if you are working with large positive integers, @user2357112 points out in the comments below that np.unique
provides another solution. Here you'd 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])