Sliding window for splitting a ndarray into smaller overlapping ndarrays

时间:2017-04-10 02:48:30

标签: python numpy sliding-window

I am currently working out a method in which I can split a numpy.ndarray into a given number of sub-arrays, as long the number is less than the the axis which the window moves along.

Example:

Given an numpy.ndarray with shape (15, 40, 3) I want to divide in to 36 equal sized sub-arrays. Overlap is allowed, since in most cases the length of the array and the number of sub-arrays not be evenly divisible.

Each sub-array has to have (15, ?, 3) as shape, in which ? is the axis in which the sliding window moves. Overlapping is allowed.

How can I do that? numpy.splits requires items to be equally divisible and also does not allow overlap.

Expected output: Given a numpy.ndarray of shape (5,3), I want to extract 2 numpy.ndarrays of shape (5,2), with an sliding window moving axis = 1. it will always be this axis.

I don't want explicitly want set how much overlap there should be between both numpy.ndarrays, but only the number of ndarrays that there should be extracted..

So in this case, should each sub numpy.ndarray be placed like this:

enter image description here

1 个答案:

答案 0 :(得分:1)

可以在这里找到正在实现的内容的实现:

https://popi.ucdavis.edu/~travc/tmp/dev/racc/sliding_window.py