1

I have 2 arrays, for the sake of simplicity let's say the original one is a random set of numbers:

import numpy as np
a=np.random.rand(N)

Then I sample and shuffle a subset from this array:

b=np.array()  <------size<N

The shuffling I do do not store the index values, so b is an unordered subset of a

Is there an easy way to get the original indexes of b, so they are in the same order as a, say, if element 2 of b has the index 4 in a, create an array of its assignation.

I could use a for cycle checking element by element, but perhaps there is a more pythonic way

Thanks

2 Answers 2

2

I think the most computationally efficient thing to do is to keep track of the indices that associate b with a as b is created.

For example, instead of sampling a, sample the indices of a:

indices = random.sample(range(len(a)), k)   # k < N
b = a[indices]
Sign up to request clarification or add additional context in comments.

1 Comment

Or, with Numpy (which the OP is already using): indices = np.arange(len(a)); np.random.shuffle(indices); indices = indices[:k]
0

On the off chance a happens to be sorted you could do:

>>> from numpy import array
>>> a = array([1, 3, 4, 10, 11])
>>> b = array([11, 1, 4])
>>> a.searchsorted(b)
array([4, 0, 2])

If a is not sorted you're probably best off going with something like @unutbu's answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.