Numpy Choose Elements from 2 arrays

1.8k Views Asked by At

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...

4

There are 4 best solutions below

2
On BEST ANSWER

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)
0
On

You can use numpy.random.permutation to create a shuffled array of indices. Then you can select as many randomly selected common elements as you like:

import numpy as np

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

indices= np.random.permutation(np.size(x, 0))

x_chosen = x[indices[:2]]
y_chosen = y[indices[:2]]

print(x_chosen)
>>>[ 0.8873  0.1123]
print(y_chosen)
>>>[2 1]
0
On

How about first choosing the indices:

import numpy as np

choice = np.random.choice(np.array(range(len(x))), 2)

and then choosing according to them:

x_chosen, y_chosen = x[choice], y[choice]
0
On

Here are two methods that I think might work. The first is kind of the same as your method above, but it does get rid of one line:

index = np.random.choice(np.arange(len(x)), 2, replace=False)
x_chosen = x[index]
y_chosen = y[index]

In [15]: x_chosen
Out[15]: array([ 0.8873,  0.223 ])

I'm not sure that this is what you want, but it's a one-liner that will give you tuples of (x, y):

import random
random.sample(zip(x, y), 2)

Out[22]: [(0.223, 1), (0.1123, 1)]