我正在尝试根据历史数据预测股价。我正在使用LSTM训练我的模型。但是当我训练时,损失为NaN,准确度为0。 我使用的数据来自Yahoo Finance。是银行股票的5年数据。我将数据拆分为测试和训练集,并按比例缩放(尽管不是必需的)。添加了2层LSTM进行训练。
我的代码如下:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout
#Get the Data
data = pd.read_csv('YESBANK.NS.csv')
X = data.iloc[:, [5]].values
# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test = train_test_split(X, test_size = 0.2, random_state = 0)
# Feature Scaling
from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler(feature_range = (0, 1))
training_set_scaled = sc.fit_transform(X_train)
# Creating a data structure with 60 timesteps and 1 output
X_train1 = []
y_train1 = []
for i in range(60, training_set_scaled.shape[0]):
X_train1.append(training_set_scaled[i-60:i, 0])
y_train1.append(training_set_scaled[i, 0])
X_train, y_train = np.array(X_train1), np.array(y_train1)
# Reshaping for LSTM
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
#Initialize the RNN
model = Sequential()
#Adding first LSTM layer
model.add(LSTM(units = 50, return_sequences = True, input_shape = (X_train.shape[1], 1)))
model.add(Dropout(0.2))
# #Adding second LSTM layer
# model.add(LSTM(units=50, return_sequences=True))
# model.add(Dropout(0.2))
# #Adding third LSTM layer
# model.add(LSTM(units= 50, return_sequences=True))
# model.add(Dropout(0.2))
#Adding fourth LSTM layer
model.add(LSTM(units=50, return_sequences=False))
model.add(Dropout(0.2))
#Adding Output layer
model.add(Dense(units=1))
#Compiling the RNN
model.compile(optimizer = 'adam', loss = 'mean_squared_error', metrics = ['accuracy'])
#Fitiing the RNN
model.fit(X_train, y_train, epochs = 500, batch_size = 10) ```
答案 0 :(得分:0)
您正在做的是regression
以使股票价格(即连续价格)回归。因此,不应将accuracy
用作metric
函数。您可以使用均方误差:“ mse”或均方根绝对误差:“ mae”作为度量函数。
答案 1 :(得分:0)
您的输入数据中很可能包含NaN
。如果您使用的是this data,那么在撰写本文时,除日期列外,第142行完全是NaN
。这些将通过缩放传播,并导致网络返回NaN
。一种快速而合理的解决方案是在读取以下数据后添加此行:
data = data.fillna(data.mean())
还有两点需要注意:
pd.read_csv
可以直接从URL读取。train_test_split
与shuffle=True
一起用于时间序列问题并不理想。测试集的目的是将其视为看不见的数据,而这种安排无法满足要求。您应该考虑在截止日期之前用train
拆分数据,并在截止日期之后用test
拆分数据,因为这将更能代表预测情况(如果您的意图是这样的话,并且具有不可解释的性质我认为是这样的。