2

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

Carlton Banks
  • 395
  • 1
  • 6
  • 25
ülé
  • 41
  • 5
  • 1
    Possible duplicate of [Split array into equal sized windows](http://stackoverflow.com/questions/43313706/split-array-into-equal-sized-windows) – Arya McCarthy Apr 10 '17 at 02:58
  • 1
    Possible duplicate of [sliding window in numpy](http://stackoverflow.com/questions/15722324/sliding-window-in-numpy) – umutto Apr 10 '17 at 03:00
  • @aryamccarthy I am not sure i understand the solution.. where is the axis defined? – ülé Apr 10 '17 at 03:17
  • and @umutto ... – ülé Apr 10 '17 at 03:18
  • 1
    Welcome to Stackoverflow! To get the most out of the site it is important to [ask good questions](http://stackoverflow.com/help/how-to-ask), that includes creating a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example. – Stephen Rauch Apr 10 '17 at 03:28
  • Added an example.. Didn't a code example.. Made an image of how i hope it would work.. – ülé Apr 10 '17 at 03:42
  • Closest duplicate I could find: http://stackoverflow.com/questions/4936620/using-strides-for-an-efficient-moving-average-filter?noredirect=1&lq=1 – Daniel F Apr 10 '17 at 07:05
  • The problem is underdefined. Consider an array of size 20 that you want to divide in 5 subarrays. You have three options (sub-array length l, overlay o): [l=8, o=5], [l=12, o=10], [l=16, o=15]. You will need to specify either array length or overlap, or a rule by which to select either. – MB-F Apr 10 '17 at 07:26

1 Answers1

1

An implementation of what is being seemed can possibly be found here:

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

Carlton Banks
  • 395
  • 1
  • 6
  • 25