[Numpy-discussion] how to efficiently select multiple slices from an array?
Andreas Hilboll
lists@hilboll...
Wed Mar 20 12:59:22 CDT 2013
> Hey,
>
> On Wed, 2013-03-20 at 16:31 +0100, Andreas Hilboll wrote:
>> Cross-posting a question I asked on SO
>> (http://stackoverflow.com/q/15527666/152439):
>>
>>
>> Given an array
>>
>> d = np.random.randn(100)
>>
>> and an index array
>>
>> i = np.random.random_integers(low=3, high=d.size - 5, size=20)
>>
>> how can I efficiently create a 2d array r with
>>
>> r.shape = (20, 8)
>>
>> such that for all j=0..19,
>>
>> r[j] = d[i[j]-3:i[j]+5]
>>
>> In my case, the arrays are quite large (~200000 instead of 100 and 20),
>> so something quick would be useful.
>
>
> You can use stride tricks, its simple to do by hand, but since I got it,
> maybe just use this: https://gist.github.com/seberg/3866040
>
> d = np.random.randn(100)
> windowed_d = rolling_window(d, 8)
> i = np.random_integers(len(windowed_d))
> r = d[i,:]
>
> Or use stride_tricks by hand, with:
> windowed_d = np.lib.stride_tricks.as_strided(d, (d.shape[0]-7, 8),
> (d.strides[0],)*2)
>
> Since the fancy indexing will create a copy, while windowed_d views the
> same data as the original array, of course that is not the case for the
> end result.
>
> Regards,
>
> Sebastian
>
>>
>> Cheers, Andreas.
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion@scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
cool, thanks!
More information about the NumPy-Discussion
mailing list