资源简介
本代码采用python语言写的一个LSTM时间序列来预测销量
代码片段和文件信息
from math import sqrt
from numpy import concatenate
from matplotlib import pyplot
from pandas import read_csv
import numpy as np
from pandas import Dataframe
from pandas import concat
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import mean_squared_error
from keras.models import Sequential
from keras.layers import LSTM Dense DropoutActivation
from keras import optimizers
from keras import regularizers
# 将序列转换为监督学习问题
def series_to_supervised(data n_in=1 n_out=1 dropnan=True):
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 togethe
agg = concat(cols axis=1)
agg.columns = names
# drop rows with NaN values
if dropnan:
agg.dropna(inplace=True)
return agg
# 加载数据集
dataset = read_csv(‘D:\data\YA\predict2.csv‘ header=0 index_col=0)
values = dataset.values
values=np.log1p(values)
# 确保所有数据是浮动的
values = values.astype(‘float64‘)
# 归一化特征
scaler = MinMaxScaler(feature_range=(0 1))
scaled = scaler.fit_transform(values)
# 指定滞后时间大小
n_hours =1
n_features =3
# 构建监督学习问题
reframed = series_to_supervised(scaled n_hours 1)
print(reframed)
# 分为训练集和测试集
values = reframed.values
n_train_hours = int(len(dataset) * 0.9)
train = values[:n_train_hours :]
test = values[n_train_hours: :]
# 分为输入和输出
n_obs = n_hours * n_features
train_X train_y = train[: :n_obs] train[: -n_features]
test_X test_y = test[: :n_obs] test[: -n_features]
print(train_X.shape len(train_X) train_y.shape)
# 重塑为3D形状 [samples timesteps features]
train_X = train_X.reshape((train_X.shape[0] n_hours n_features))
test_X = test_X.reshape((test_X.shape[0] n_hours n_features))
print(train_X.shape train_y.shape test_X.shape test_y.shape)
# 设计网络
model = Sequential()
model.add(LSTM(128input_shape=(train_X.shape[1] train_X.shape[2])return_sequences=True))
model.add(Dropout(0.5))
model.add(LSTM(256 return_sequences=False ))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation(‘sigmoid‘))
adam = optimizers.Adam(lr=0.003 beta_1=0.9 beta_2=0.999 epsilon=1e-08)
model.compile(loss=‘mae‘ optimizer=adam)
# fit network
history = model.fit(train_X train_y epochs=200 batch_size=8 validation_data=(test_X test_y) shuffle=False)
# plot history
pyplot.plot(history.history[‘loss‘] label=‘train‘)
pyplot.plot(history.history[‘val_loss‘] label=‘test‘)
pyplot.legend()
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 4082 2018-12-21 19:46 LSTM时间序列预测\LSTM2.py
文件 4101 2018-12-21 20:03 LSTM时间序列预测\LSTM时间序列.py
文件 5452 2018-12-21 19:48 LSTM时间序列预测\zxq.csv
目录 0 2018-12-26 19:20 LSTM时间序列预测\
- 上一篇:Python实现简单遗传,粒子群,蚁群,模拟退火算法
- 下一篇:单纯形法python
相关资源
- 基于lstm的语义相似度计算模型代码
- 使用LSTM进行时间序列预测
- lstm_attention文本分类代码
- 基于LSTM的RNN网络人体骨骼关节点检测
- 时间长短序列网络LSTM
- Tensorflow-BiLSTM分类
- lstm实现时间序列一维预测
- keras上LSTM长短期记忆网络金融时序预
- 四种聚类算法实现对控制图时间序列
- 基于LSTM的航班乘客预测
- lstm_tensorflow
- LSTM股票预测
- arima预测附Python和测试数据
- lstm情感分析代码
- tensorflow下用LSTM网络进行时间序列预测
- 股票预测 LSTM 时间序列rnn 代码程序数
- 计算时间序列变化率
- LSTMLong Short-Term Memory长短期记忆网络
- 卷积LSTM代码
- LSTM上证指数收盘价预测.zip
- 非常简易的keras函数式Functional模型学
评论
共有 条评论