python - Simple Numpy Vectorization -


i have 2 1d numpy arrays start , stop, both of contain integers (which used index other array). have following piece of code.

index_list = [] in range(len(start)):     temp = range(start[i], stop[i])     index_list.extend(temp) index_list = np.array(index_list) 

is there simple way vectorize this?

you can vectorize follows:

def make_index_list(start, stop):     lens = stop - start     cum_lens = np.cumsum(lens)     # sequential indices same length expected output     out = np.arange(cum_lens[-1])     # starting index each section of `out`     cum_lens = np.concatenate(([0], cum_lens[:-1]))     # how each section of `out` off correct value     deltas = start - out[cum_lens]     # apply correction     out += np.repeat(deltas, lens)      return out 

with made data:

start = np.random.randint(100, size=(100000,)) stop = start + np.random.randint(1, 10 ,size=start.shape) 

we can take code test ride:

in [39]: %%timeit    ....: index_list = []    ....: in range(len(start)):    ....:     temp = range(start[i], stop[i])    ....:     index_list.extend(temp)    ....: index_list = np.array(index_list)    ....: 10 loops, best of 3: 137 ms per loop  in [40]: %timeit make_index_list(start, stop) 100 loops, best of 3: 9.27 ms per loop  in [41]: np.array_equal(make_index_list(start, stop), index_list) out[41]: true 

so correct , 15x faster, not bad @ all...


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -