Search code examples
pythonnumpynumpy-random

Numpy Choose Elements from 2 arrays


I need to choose n items from 2 arrays such that the indices are the same. So, for instance, I need to choose two items from x randomly, and pick elements from y such that y's chosen indices are the same as x's:

x = np.asarray([0.1123,0.223,0.8873])
y = np.asarray([1,1,2])

x_chosen = np.random.choice(x,(2,))

Say x_chosen winds up being: x_chosen = [0.1123,0.223]... then I would need:

y_chosen = [1,1]

I currently have a workaround...but I would like a basic numpy or scipy function rather than my own function that is basically only 3 lines, to keep my functions in this project in scope...and I'd rather not have this throwaway variable creation all over my main code:

x_index = np.asarray([i for i in range(len(x))])
x_chosen_indices = np.random.choice(x_index,(2,))
x_chosen = x[x_chosen_indices]
y_chosen = y[x_chosen_indices]

Alternatively, I could stack, choose and split...but I guess that still leaves me with a throwaway function I have to stick somewhere...or 4-5 lines of code that won't make sense...


Solution

  • Use np.random.randint() to find your indices:

    x = np.asarray([0.1123,0.223,0.8873])
    y = np.asarray([1,1,2])
    
    indices = np.random.randint(0, x.size, 2)
    >>> x[indices]
    array([ 0.1123,  0.223 ])
    >>> y[indices]
    array([1, 1])
    

    EDIT

    As mentioned in a comment by B. M use np.random.choice(.., replace=False) to avoid using the same index multiple times:

    indices = np.random.choice(x.size, 2, replace=False)