0

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.

2
  • 2
    Question has nothing to do with machine-learning or neural-network - kindly do not spam the tags (removed). Commented Oct 18, 2018 at 9:00
  • @desertnaut I am sorry actually I am preparing a time series data input to a Vanila RNN. So I thought it would be appropriaite to tag Neural Network. Commented Oct 18, 2018 at 9:15

1 Answer 1

3

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))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.