Randomly select from numpy array

Question:

I have two related numpy arrays, X and y. I need to select n random rows from X and store this in an array, the corresponding y value and the appends to it the index of the points randomly selected.

I have another array index which stores a list of index which I dont want to sample.

How can I do this?

Sample data:

index = [2,3]
X = np.array([[0.3,0.7],[0.5,0.5] ,[0.2,0.8], [0.1,0.9]])
y = np.array([[0], [1], [0], [1]])

If these X‘s were randomly selected (where n=2):

randomylSelected = np.array([[0.3,0.7],[0.5,0.5]])

the desired output would be:

index = [0,1,2,3]
randomlySelectedY = [0,1]

How can I do this?

Asked By: scutnex

||

Answers:

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]
Answered By: MSeifert

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)
Answered By: Alon Gouldman
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.