Randomly select from numpy array

You can create random indices with np.random.choice:

n = 2  # for 2 random indices
index = np.random.choice(X.shape[0], n, replace=False)  

Then you just need to index your arrays with the result:

x_random = X[index]
y_random = Y[index]

Leave a Comment