Reputation: 5378
Say, I have a numpy array defined as:
X = numpy.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Now I want to draw 3 elements from this array, but with random indices and without repetition, so I'll get, say:
X_random_draw = numpy.array([5, 0, 9]
How can I achieve something like this with the least effort and with the greatest performance speed? Thank you in advance.
Upvotes: 3
Views: 3658
Reputation: 880937
With NumPy 1.7 or newer, use np.random.choice
, with replace=False
:
In [85]: X = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [86]: np.random.choice(X, 3, replace=False)
Out[86]: array([7, 5, 9])
Upvotes: 8