2

Given two vectors v=['a','b','c'] and i=np.random.randint(len(v),size=10), I can get the "substitution" vector

vi = [v[i[x]] for x in range(len(i))]

E.g., vi is

['a', 'a', 'c', 'c', 'b', 'a', 'c', 'a', 'c', 'a']

if

i = array([0, 0, 2, 2, 1, 0, 2, 0, 2, 0])

Is there a vectorized operation for this?

0

1 Answer 1

6

You can simply use numpy indexing (note that you need to convert v to a numpy.array for this to work):

v = np.array(['a','b','c'])
i = np.random.randint(len(v),size=10)

>>> v[i]
array(['c', 'b', 'a', 'b', 'c', 'b', 'a', 'a', 'b', 'b'], dtype='<U1')

Timings

In [26]: i = np.random.randint(len(v),size=1000000)

In [27]: %timeit [v[i[x]] for x in range(len(i))]
554 ms ± 6.41 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [28]: %timeit v[i]
4.85 ms ± 12.5 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [29]: %timeit [v[s] for s in i]
505 ms ± 1.95 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
Sign up to request clarification or add additional context in comments.

3 Comments

can you also compare what (imo) most would do, [v[s] for s in i]?
Done. Looks like it trims a bit of overhead
Awesome, thanks! Once again numpy proving too fast..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.