mak
mak

Reputation: 63

How to slice arrays with a percantage of overlapping

I have a set of data like this:

numpy.array([[3, 7],[5, 8],[6, 19],[8, 59],[10, 42],[12, 54], [13, 32], [14, 19], [99, 19]])

which I want to split into number of chunkcs with a percantage of overlapping, for each column separatly... for example for column 1, splitting into 3 chunkcs with %50 overlapping (results in a 2-d array):

[[3, 5, 6, 8,],
        [6, 8, 10, 12,],
              [10, 12, 13, 14,]]

(ignoring last row which will result in [13, 14, 99] not identical in size as the rest). I'm trying to make a function that takes the array, number of chunkcs and overlpapping percantage and returns the results.

Upvotes: 0

Views: 140

Answers (2)

mathfux
mathfux

Reputation: 5949

For numpy only, quite fast approach is:

def rolling(a, window, step):
    shape = ((a.size - window)//step + 1, window)
    strides = (step*a.itemsize, a.itemsize)
    return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)

And you can call it like so:

rolling(arr[:,0].copy(), 4, 2)

Remark: I've got unexpected outputs for rolling(arr[:,0], 4, 2) so just took a copy instead.

Upvotes: 0

Daniel F
Daniel F

Reputation: 14399

That's a window function, so use skimage.util.view_as_windows:

from skimage.util import view_as_windows

out = view_as_windows(in_arr[:, 0], window_shape = 4, step = 2)

If you need numpy only, you can use this recipe

Upvotes: 1

Related Questions