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?
[2, 1] -> [2, 2, 1]not[2, 1] -> [0, 0, 1]as per your example. Which is correct?