1

I have a sequence of numbers that I would like to insert into a larger array at irregular intervals:

dates = np.zeros(15)
pattern = np.arange(3) + 1
starts = [2, 6, 11]
for start in starts:
    dates[start:start + pattern.size] = pattern

> [0 0 1 2 3 0 1 2 3 0 0 1 2 3 0]

I have to do this many (100M+) times on large (10K+) arrays, so I'm looking for a way to do this with broadcasting or another efficient method, avoiding a for loop. pattern will always be a range if that helps.

Divakar
  • 218,885
  • 19
  • 262
  • 358
triphook
  • 2,915
  • 3
  • 25
  • 34

2 Answers2

3

We can leverage np.lib.stride_tricks.as_strided based scikit-image's view_as_windows to get sliding windowed views into the output array and hence assign the new values into it. This would be pretty efficient, as we are working with views, there's no generation of explicit indices and the assignment is a vectorized and broadcasted one.

The implementation would look something like this -

from skimage.util.shape import view_as_windows

view_as_windows(dates,pattern.size)[starts] = pattern

More info on use of as_strided based view_as_windows.

Divakar
  • 218,885
  • 19
  • 262
  • 358
  • 1
    Ah, of course. You can't construct a view of just the windows you want, but you can construct a view of *all* the windows and then assign into specific windows. – user2357112 Apr 16 '19 at 21:13
2

Construct a 2D selector array to select the indices of dates you want to modify with numpy.add.outer, then perform a broadcasted assignment of pattern into the selected indices:

dates[numpy.add.outer(starts, numpy.arange(len(pattern)))] = pattern
user2357112
  • 260,549
  • 28
  • 431
  • 505