LSTM预测结果无法符合实际值

时间:2019-07-14 06:13:51

标签: tensorflow keras lstm

我是机器学习中的新手,我编写了一个LSTM网络以使用Google股票数据集预测时间序列数据,我从网页中进行引用:

https://medium.com/datadriveninvestor/multivariate-time-series-using-rnn-with-keras-7f78f4488679

我对程序进行编码的方式与网页编码完全相同,但结果完全破损,预测值无法满足读取值。

我想知道X_test和y是否加载了错误的数据

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from keras.layers import Dense,LSTM
from keras.models import Sequential
from sklearn.preprocessing import MinMaxScaler

stock_data = pd.read_csv("HistoricalQuotes.csv")
stock_data["average"] = (stock_data["high"] + stock_data["low"]) / 2
input_feature = stock_data.iloc[:, [2, 6]].values
input_data = input_feature

# Normalizing the input data using MinMaxScaler so that all the input features are on the scale from 0 to 1
sc = MinMaxScaler(feature_range=(0, 1))
input_data[:, 0:2] = sc.fit_transform(input_feature[:, :])

# loop through all the samples and for each day we go back 50 business days in the past and add the volume of the stocks traded an average stock price.
# Also, we will take 30 % of the latest data as our test dataset.
lookback = 50

test_size = int(.3 * len(input_data))
X = []
y = []
for i in range(len(input_data) - lookback - 1):
    t = []
    for j in range(0, lookback):
        t.append(input_data[[(i + j)], :])
        X.append(t)
        y.append(input_data[i + lookback, 1])

X, y = np.array(X), np.array(y)
X_test = X[:test_size + lookback]
X = X.reshape(X.shape[0], lookback, 2)
X_test = X_test.reshape(X_test.shape[0], lookback, 2)

model = Sequential()
model.add(LSTM(units=30, return_sequences= True, input_shape=(X.shape[1],2)))
model.add(LSTM(units=30, return_sequences=True))
model.add(LSTM(units=30))
model.add(Dense(units=1))

model.compile(optimizer='adam', loss='mean_squared_error', metrics=['mae', 'acc'])
model.fit(X, y, epochs=200, batch_size=32)
predicted_value= model.predict(X_test)

plt.plot(predicted_value, color= 'red')
plt.plot(input_data[lookback:test_size+(2*lookback),1], color='green')
plt.title("Opening price of stocks sold")
plt.xlabel("Time (latest-> oldest)")
plt.ylabel("Stock Opening Price")
plt.show()

1 个答案:

答案 0 :(得分:1)

下面的代码行中有错误:

test_size=int(.3 * len(input_data))
X=[]
y=[]
for i in range(len(input_data)-lookback-1):

应为stock_data,而不是input_data。因此,用下面的代码替换上面的代码将解决此问题。

test_size=int(.3 * len(stock_data))
X=[]
y=[]
for i in range(len(stock_data)-lookback-1):

如果此答案对您有用,请接受和/或投票。谢谢!