2022数学建模LSTM
数据导入与清洗于网络设值
在数据导入时我使用的是pd.read_csv函数
#设置LSTM的时间窗等参数
window=1
lstm_units = 8
dropout = 0.001
epoch=200
#读取数据
data1=pd.read_csv('GOLD_2021.csv')
data2=pd.read_csv('LBMA-GOLD.csv')
test_ata=data2.iloc[:,0]
data1.drop('1978/12/29',axis = 1,inplace = True)
df1=data1
df1.rename(columns={'226':'Value'},inplace=True)
df1.dropna(inplace=True)
#df1.tail()
print(data1)
window:时间窗
lstm_units :基本单元
dropout :步长
epoch:迭代次数
归一化
from sklearn import preprocessing
min_max_scaler = preprocessing.MinMaxScaler()
df0=min_max_scaler.fit_transform(df1)
df = pd.DataFrame(df0, columns=df1.columns)
input_size=len(df.iloc[1,:])
数据集和训练集的划分
#构建lstm输入
stock=df
seq_len=window
amount_of_features = len(stock.columns)#有几列
data = stock.as_matrix() #pd.DataFrame(stock) 表格转化为矩阵
sequence_length = seq_len + 1#序列长度
result = []
for index in range(len(data) - sequence_length):#循环数据长度-sequence_length次
result.append(data[index: index + sequence_length])#第i行到i+sequence_length
result = np.array(result)#得到样本,样本形式为6天*3特征
row =9827#划分训练集测试集
train = result[:int(row), :]
x_train = train[:, :-1]
y_train = train[:, -1][:,-1]
x_test = result[int(row):, :-1]
y_test = result[int(row):, -1][:,-1]
#reshape成 6天*3特征
X_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], amount_of_features))
X_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], amount_of_features))
print(y_train[:10], X_train.shape, y_train.shape, X_test.shape, y_test.shape)
print(x_train.shape[0], x_train.shape[1], amount_of_features)
LSTM模型建立
#建立LSTM模型 训练
inputs=Input(shape=(window, input_size))
model=Conv1D(filters = lstm_units, kernel_size = 1, activation = 'sigmoid')(inputs)#卷积层
model=MaxPooling1D(pool_size = window)(model)#池化层
model=Dropout(dropout)(model)#droupout层
model=Bidirectional(LSTM(lstm_units, activation='tanh'), name='bilstm')(model)#双向LSTM层
attention=Dense(lstm_units*2, activation='sigmoid', name='attention_vec')(model)#求解Attention权重
model=Multiply()([model, attention])#attention与LSTM对应数值相乘
outputs = Dense(1, activation='tanh')(model)#输入只有一个
model = Model(inputs=inputs, outputs=outputs)
model.compile(loss='mse',optimizer='adam',metrics=['accuracy'])#损失函数,优化器,比较函数
model.summary()#展示模型结构
训练模型
history=model.fit(X_train, y_train, epochs = epoch, batch_size = 32,shuffle=False,validation_data=(X_test, y_test)) #训练模型epoch次
迭代图像
#迭代图像
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs_range = range(epoch)
plt.plot(epochs_range, loss, label='Train Loss')
plt.plot(epochs_range, val_loss, label='Test Loss')
plt.legend(loc='upper right')
plt.title('Train and Val Loss')
plt.show()
plt.savefig("LSTM_GOLD1.png")
训练结果
#在训练集上的拟合结果
y_train_predict=model.predict(X_train)
y_train_predict=y_train_predict[:,0]
draw=pd.concat([pd.DataFrame(y_train),pd.DataFrame(y_train_predict)],axis=1)
draw.iloc[0:row,0].plot(figsize=(12,6))
draw.iloc[0:row,1].plot(figsize=(12,6))
plt.legend(('real', 'predict'),fontsize='15')
plt.title("Train Data",fontsize='30') #添加标题
此文仅作为个人笔记,有问题可以私信