mercredi 29 juillet 2020

Rolling/Sliding Window Approach for machine learning models

I want to set up a rolling-window approach to do some backtesting on my ML model. The idea is:

  • Take 9 months of data (e.g., 2019 Jan to Sept) to make forecast on next 3 months (e.g., 2019 Oct to Dec)
  • Then drop earliest 3 months (2019 Jan to March) and add 2019 Oct to Dec
  • Make forecast on next three months (2020 Jan to March)
  • Repeat process until end of data enter image description here

Now, lest say I have 3 years of data and I can easy select each year (or month) with:

dataframe.loc()

Then next step is to actually create a loop that will make the sliding window on my requirements. And here I am stuck and need your help!

If I use a simple loop like the following:

X = dataframe.values
n_train = 500
n_records = len(X)
for i in range(n_train, n_records):
    train, test = X[0:i], X[i:i+1]
    print('train=%d, test=%d' % (len(train), len(test)))

I am able to take the first 500 data points of my data and then make forecast for the next 1 (501st). And then using 501 data points to make forecast for 502nd... etc until I finish my data.

enter image description here

Anyone with an idea on how to use a similar loop (or another technique) to achieve my desired outcome?

Thanks!

Aucun commentaire:

Enregistrer un commentaire