Skip to content

Instantly share code, notes, and snippets.

@kevincdurand1
Last active January 28, 2020 19:18
Show Gist options
  • Select an option

  • Save kevincdurand1/54e589751ff7f6db9e12c28a0fdfd1cf to your computer and use it in GitHub Desktop.

Select an option

Save kevincdurand1/54e589751ff7f6db9e12c28a0fdfd1cf to your computer and use it in GitHub Desktop.
Covert Time series to Supervised
# 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