Ricky
Ricky

Reputation: 2750

How to slice array into sequences arrays?

I have a time series array for features and label like

x=[1,2,3,4,5,6......100]
y=[0.5,0.8,0.9,0.5,0.9,0.8,....,0.9]

I want to make this as into dynamic sub arrays like if i=3 then

x=[1,2,3],[2,3,4],[3,4,5],...
y=[0.5,0.8,0.9],[0.8,0.9,0.5],[0.9,0.5,0.9],...

So I know t[1:i] gives the first element to the i th element but how to do it consecutively. Any help is appreciated.

Upvotes: 0

Views: 69

Answers (1)

lightalchemist
lightalchemist

Reputation: 10221

What you want is a way to compute sliding windows from sequences.

Modifying the solution from Rolling or sliding window iterator? by @Daniel DiPaolo

from itertools import islice

def window(seq, n=2):
    "Returns a sliding window (of width n) over data from the iterable"
    "   s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ...                   "
    it = iter(seq)
    result = list(islice(it, n))
    if len(result) == n:
        yield result
    for elem in it:
        result = result[1:] + [elem]
        yield result

from functools import partial

def group_slice(*args, winsize):
    yield from zip(*map(partial(window, n=winsize), args))

def group_slice(*args, winsize):
     # Slightly clearer version of the above 
     partial_func = partial(window, n=winsize)
     yield from zip(*(partial_func(s) for s in args))

What group_slice is doing is

  1. Create a partial function out of window with the given value of window size.

  2. Apply this partial "modified" window function to each sequence to get a collection of generators.

  3. Then yield each slice from each generator.

You use it like this

x = [1,2,3,4,5,6]
y = [0.5,0.8,0.9,0.5,0.9,0.8]

for x_slice, y_slice in group_slice(x, y, winsize=3):
    print(x_slice)
    print(y_slice)

which will output

[1, 2, 3]
[0.5, 0.8, 0.9]
[2, 3, 4]
[0.8, 0.9, 0.5]
[3, 4, 5]
[0.9, 0.5, 0.9]
[4, 5, 6]
[0.5, 0.9, 0.8]

or if you just want lists of individual group

x_slices = list(window(x, n=3))

Upvotes: 3

Related Questions