title: Efficiently slice windows from a 1D numpy array, around indices given by second 2D array
body: <p>I want to extract multiple slices from the same 1D numpy array, where the slice indices are drawn from a random distribution. Basically, I want to achieve the following:</p>
  
  <pre><code>import numpy as np
  import numpy.random
  
  # generate some 1D data
  data = np.random.randn(500)
  
  # window size (slices are 2*winsize long)
  winsize = 60
  
  # number of slices to take from the data
  inds_size = (100, 200)
  
  # get random integers that function as indices into the data
  inds = np.random.randint(low=winsize, high=len(data)-winsize, size=inds_size)
  
  # now I want to extract slices of data, running from inds[0,0]-60 to inds[0,0]+60
  sliced_data = np.zeros( (winsize*2,) + inds_size )
  for k in range(inds_size[0]):
      for l in range(inds_size[1]):
          sliced_data[:,k,l] = data[inds[k,l]-winsize:inds[k,l]+winsize]
  
  # sliced_data.shape is now (120, 100, 200)
  </code></pre>
  
  <p>The above nested loop works fine, but is very slow. In my real code, I will need to do this thousands of times, for data arrays a lot bigger than these. Is there any way to do this more efficiently?</p>
  
  <p>Note that <code>inds</code> will always be 2D in my case, but after getting the slices I will always be summing over one of these two dimensions, so an approach that only accumulates the sum across the one dimension would be fine.</p>
  
  <p>I found <a href="https://stackoverflow.com/questions/15527666/how-to-efficiently-select-multiple-slices-from-an-array">this question</a> and <a href="https://stackoverflow.com/a/15528771/1692028">this answer</a> which seem <em>almost</em> the same. However, the question is only about a 1D indexing vector (as opposed to my 2D). Also, the answer lacks a bit of context, as I don't really understand how the suggested <code>as_strided</code> works. Since my problem does not seem uncommon, I thought I'd ask again in the hope of a more explanatory answer rather than just code.</p>