Generate 1D NumPy array of concatenated ranges

2019-06-06 02:24发布

问题:

I want to generate a following array a:

nv = np.random.randint(3, 10+1, size=(1000000,))
a = np.concatenate([np.arange(1,i+1) for i in nv])

Thus, the output would be something like -

[0, 1, 2, 3, 0, 1, 2, 3, 4, 0, 1, 2, 0, 1, 2, 3, 4, 5, 0, ...]

Does there exist any better way to do it?

回答1:

Here's a vectorized approach using cumulative summation -

def ranges(nv, start = 1):
    shifts = nv.cumsum()
    id_arr = np.ones(shifts[-1], dtype=int)
    id_arr[shifts[:-1]] = -nv[:-1]+1
    id_arr[0] = start # Skip if we know the start of ranges is 1 already
    return id_arr.cumsum()

Sample runs -

In [23]: nv
Out[23]: array([3, 2, 5, 7])

In [24]: ranges(nv, start=0)
Out[24]: array([0, 1, 2, 0, 1, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 6])

In [25]: ranges(nv, start=1)
Out[25]: array([1, 2, 3, 1, 2, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6, 7])

Runtime test -

In [62]: nv = np.random.randint(3, 10+1, size=(100000,))

In [63]: %timeit your_func(nv) # @MSeifert's solution
10 loops, best of 3: 129 ms per loop

In [64]: %timeit ranges(nv)
100 loops, best of 3: 5.54 ms per loop


回答2:

Instead of doing this with numpy methods you could use normal python ranges and just convert the result to an array:

from itertools import chain
import numpy as np

def your_func(nv):
    ranges = (range(1, i+1) for i in nv)
    flattened = list(chain.from_iterable(ranges))
    return np.array(flattened)

This doesn't need to utilize hard to understand numpy slicings and constructs. To show a sample case:

import random

>>> nv = [random.randint(1, 10) for _ in range(5)]
>>> print(nv)
[4, 2, 10, 5, 3]

>>> print(your_func(nv))
[ 1  2  3  4  1  2  1  2  3  4  5  6  7  8  9 10  1  2  3  4  5  1  2  3]


回答3:

Why two steps?

a = np.concatenate([np.arange(0,np.random.randint(3,11)) for i in range(1000000)])