title: Sliding window train/test split for time series data
body: <p>I have a series that contains 36 data points and I would like to do a sliding window training and test on it. I've looked at TimeSeriesSplit() but it only does something like </p>
  
  <pre><code>('TRAIN:', array([0, 1, 2]), 'TEST:', array([3, 4, 5]))
  ('TRAIN:', array([0, 1, 2, 3, 4, 5]), 'TEST:', array([6, 7, 8]))
  ('TRAIN:', array([0, 1, 2, 3, 4, 5, 6, 7, 8]), 'TEST:', array([ 9, 10, 11]))
  </code></pre>
  
  <p><b>I would like to have something like a fix length of 12 sliding window which moves 1 point every time and a fix length of 3 sliding window for test set too.</b>
  E.g. </p>
  
  <pre><code>('TRAIN:', array([0,1,2,3,4,5,6,7,8,9,10,11]), 
   'TEST:', array([12,13,14]))
  ('TRAIN:', array([1,2,3,4,5,6,7,8,9,10,11,12]), 
   'TEST:', array([13,14,15]))
  ('TRAIN:', array([2,3,4,5,6,7,8,9,10,11,12,13]), 
   'TEST:', array([14,15,16]))
  ...
  </code></pre>
  
  <p>I read this post (<a href="https://ntguardian.wordpress.com/2017/06/19/walk-forward-analysis-demonstration-backtrader/" rel="nofollow noreferrer">https://ntguardian.wordpress.com/2017/06/19/walk-forward-analysis-demonstration-backtrader/</a>) and tried</p>
  
  <pre><code>from sklearn.model_selection import TimeSeriesSplit
  from sklearn.utils import indexable
  from sklearn.utils.validation import _num_samples
  import numpy as np
  
  class TimeSeriesSplitImproved(TimeSeriesSplit):
      def split(self, X, y=None, groups=None, fixed_length=False,
                train_splits=1, test_splits=1):
          X, y, groups = indexable(X, y, groups)
          n_samples = _num_samples(X)
          n_splits = self.n_splits
          n_folds = n_splits + 1
          train_splits, test_splits = int(train_splits), int(test_splits)
          if n_folds &gt; n_samples:
              raise ValueError(
                  ("Cannot have number of folds ={0} greater"
                   " than the number of samples: {1}.").format(n_folds,
                                                               n_samples))
          if (n_folds - train_splits - test_splits) &lt;= 0 and test_splits &gt; 0:
              raise ValueError(
                  ("Both train_splits and test_splits must be positive"
                   " integers."))
          indices = np.arange(n_samples)
          split_size = (n_samples // n_folds)
          test_size = split_size * test_splits
          train_size = split_size * train_splits
          test_starts = range(train_size + n_samples % n_folds,
                              n_samples - (test_size - split_size),
                              split_size)
          if fixed_length:
              for i, test_start in zip(range(len(test_starts)),
                                       test_starts):
                  rem = 0
                  if i == 0:
                      rem = n_samples % n_folds
                  yield (indices[(test_start - train_size - rem):test_start],indices[test_start:test_start + test_size])
          else:
              for test_start in test_starts:
                  yield (indices[:test_start],indices[test_start:test_start + test_size])
  
  
  model = TimeSeriesSplitImproved(n_splits=5)
  for train_index, test_index in model.split(X,fixed_length=True,train_splits=2, test_splits=1):
      print("TRAIN:", train_index, "TEST:", test_index)
      train, test = X[train_index], X[test_index]
  </code></pre>
  
  <p>Only got this:</p>
  
  <pre><code>TRAIN: [ 0  1  2  3  4  5  6  7  8  9 10 11] TEST: [12 13 14 15 16 17]
  TRAIN: [ 6  7  8  9 10 11 12 13 14 15 16 17] TEST: [18 19 20 21 22 23]
  TRAIN: [12 13 14 15 16 17 18 19 20 21 22 23] TEST: [24 25 26 27 28 29]
  TRAIN: [18 19 20 21 22 23 24 25 26 27 28 29] TEST: [30 31 32 33 34 35]
  </code></pre>
  
  <p>Thank you for the help in advance!</p>