赞
踩
网上有很多关于lstm时间序列的博客,我从不同博客那里借鉴了许多。
理论知识:
项目已开源上传到gitee上:
时间序列预测仓库: 本项目从简单入门到涉及部分高级预测代码,正在维护中,涉及单变量单步预测,单变量多步预测,以及多变量单步预测,多变量多步预测。 (gitee.com)
1.首先是构建时间序列数据,然后转化为监督学习数据集。
- from pandas import DataFrame
- from pandas import concat
-
- def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):
- """
- Frame a time series as a supervised learning dataset.
- Arguments:
- data: Sequence of observations as a list or NumPy array.
- n_in: Number of lag observations as input (X).
- n_out: Number of observations as output (y).
- dropnan: Boolean whether or not to drop rows with NaN values.
- Returns:
- Pandas DataFrame of series framed for supervised learning.
- """
- n_vars = 1 if type(data) is list else data.shape[1]
- df = DataFrame(data)
- cols, names = list(), list()
- # input sequence (t-n, ... t-1)
- for i in range(n_in, 0, -1):
- cols.append(df.shift(i))
- names += [('var%d(t-%d)' % (j+1, i)) for j in range(n_vars)]
- # forecast sequence (t, t+1, ... t+n)
- for i in range(0, n_out):
- cols.append(df.shift(-i))
- if i == 0:
- names += [('var%d(t)' % (j+1)) for j in range(n_vars)]
- else:
- names += [('var%d(t+%d)' % (j+1, i)) for j in range(n_vars)]
- # put it all together
- agg = concat(cols, axis=1)
- agg.columns = names
- # drop rows with NaN values
- if dropnan:
- agg.dropna(inplace=True)
- return agg
2.
Reference
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。