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]
just to wrap @MSeifert 's answer in a function:
def random_sample(arr: numpy.array, size: int = 1) -> numpy.array:
return arr[np.random.choice(len(arr), size=size, replace=False)]
useage:
randomly_selected_y = random_sample(Y)