Last active
January 28, 2020 19:18
-
-
Save kevincdurand1/54e589751ff7f6db9e12c28a0fdfd1cf to your computer and use it in GitHub Desktop.
Covert Time series to Supervised
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://www.kaggle.com/dimitreoliveira/deep-learning-for-time-series-forecasting | |
| def series_to_supervised(data, window=1, lag=1, dropnan=True): | |
| cols, names = list(), list() | |
| # Input sequence (t-n, ... t-1) | |
| for i in range(window, 0, -1): | |
| cols.append(data.shift(i)) | |
| names += [('%s(t-%d)' % (col, i)) for col in data.columns] | |
| # Current timestep (t=0) | |
| cols.append(data) | |
| names += [('%s(t)' % (col)) for col in data.columns] | |
| # Target timestep (t=lag) | |
| cols.append(data.shift(-lag)) | |
| names += [('%s(t+%d)' % (col, lag)) for col in data.columns] | |
| # Put it all together | |
| agg = pd.concat(cols, axis=1) | |
| agg.columns = names | |
| # Drop rows with NaN values | |
| if dropnan: | |
| agg.dropna(inplace=True) | |
| return agg | |
| window = 29 | |
| lag = lag_size | |
| series = series_to_supervised(train_gp.drop('date', axis=1), window=window, lag=lag) | |
| series.head() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment