Skip to content

Instantly share code, notes, and snippets.

@HeshamMeneisi
Created May 25, 2018 22:46
Show Gist options
  • Save HeshamMeneisi/a3afea0b7454c8af8b41a4f8e2f29d2a to your computer and use it in GitHub Desktop.
Save HeshamMeneisi/a3afea0b7454c8af8b41a4f8e2f29d2a to your computer and use it in GitHub Desktop.
def series_to_supervised(df, n_in=1, n_out=1, targets=[], dropnan=True):
"""
Converts a time series Pandas DataFrame into a supervised learning problem
returns: X(t-n_in+1,...,t), y(t+1,....,t+n_out)
"""
assert n_in > 0 and n_out > 0
assert all([t in df.columns for t in targets])
n_vars = len(df.columns)
cols, names = list(), list()
# input sequence (t-n_in+1, ... t-1)
for i in range(n_in-1, 0, -1):
cols.append(df.shift(i))
names += [(df.columns[j]+'(t-%d)' % (i)) for j in range(n_vars)]
# current point in time t
cols.append(df)
names += [(df.columns[j]+'(t)') for j in range(n_vars)]
# forecast sequence (t+1, ... t+n_out)
for i in range(1, n_out+1):
cols.append(df.shift(-i)[targets])
names += [(c+'(t+%d)' % (i)) for c in targets]
# put it all together
agg = pd.concat(cols, axis=1)
agg.columns = names
# drop rows with NaN values
if dropnan:
agg.dropna(inplace=True)
s = len(targets)*n_out
return (agg.loc[:, agg.columns[:-s]], agg.loc[:, agg.columns[-s:]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment