1

What I'd like to do is take an input integer array, and expand its data into indices (e.g., [2, 1] -> [2, 2, 1]). I apologize if the terminology is off -- I wasn't sure of the best way to describe this, as such, it is possible that this is a duplicate.

Here is an example of the current method I have in place:

>>> def expand(a):
...     b = np.empty(a.sum(), dtype=np.int32)
...     idx = 0
...     for i in a:
...         for j in range(i):
...             b[idx] = i
...             idx += 1
...     return b
... 
>>> a = np.array([3, 2, 1, 4])
>>> expand(a)
array([3, 3, 3, 2, 2, 1, 4, 4, 4, 4], dtype=int32)

This method is called within a nested for loop that I'd like to squeeze additional performance out of. Below is a current timing call:

>>> a = np.random.randint(0, 1000, 1000)
>>> %timeit expand(a)
10 loops, best of 3: 86.9 ms per loop 

Is there a different approach that could be used to lower the expense of the method?

3
  • Your code gives [2, 1] -> [2, 2, 1] not [2, 1] -> [0, 0, 1] as per your example. Which is correct? Commented Aug 19, 2014 at 0:47
  • Ah, good catch, sorry about that. Will fix shortly Commented Aug 19, 2014 at 0:55
  • Embarassingly, the code is actually whats wrong for what I had intended. The solution presented by @hpaulj is correct given the code in the question however, so I will close this and open a new, fixed, question in a little bit. Sorry for the confusion Commented Aug 19, 2014 at 1:33

1 Answer 1

9

The np.repeat should do most of what you want:

a.repeat(a)

I timeit at 5ms v your 88.

Your first example would be

arange(2).repeat([2,1])
Sign up to request clarification or add additional context in comments.

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.