Я пытаюсь создать прогноз по акциям, используя многовариантность. При использовании переменных переменных цены Open и High я получаю форму (1200,60,2). Это мой код ниже:
import pandas as pd
import numpy as np
import pandas_datareader as web
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout
data_training_complete = web.get_data_yahoo('AAPL', start='2013-01-01', end='2017-12-31')
data_training_processed = data_training_complete.loc[:, ['Open','High']].values
#print("checking if any null values are present\n", df.isna().sum())
min_max_scaler = MinMaxScaler(feature_range=(0,1))
data_training_scaled = min_max_scaler.fit_transform(data_training_processed)
X_train = []
y_train = []
for i in range(60, 1260):
X_train.append(data_training_scaled[i-60:i, :])
y_train.append(data_training_scaled[i,:])
X_train, y_train = np.array(X_train), np.array(y_train)
print(X_train.shape)
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 2))
print(X_train)
print(X_train.shape)
regressor = Sequential()
regressor.add(LSTM(units = 50, return_sequences = True, input_shape = (X_train.shape[1], 2)))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 50, return_sequences = True))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 50, return_sequences = True))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 50))
regressor.add(Dropout(0.2))
regressor.add(Dense(units = 1))
regressor.compile(optimizer = 'adam', loss = 'mean_squared_error')
regressor.fit(X_train, y_train, epochs = 10, batch_size = 32)
Сводка моей модели выглядит так:
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_1 (LSTM) (None, 60, 50) 10600
_________________________________________________________________
dropout_1 (Dropout) (None, 60, 50) 0
_________________________________________________________________
lstm_2 (LSTM) (None, 60, 50) 20200
_________________________________________________________________
dropout_2 (Dropout) (None, 60, 50) 0
_________________________________________________________________
lstm_3 (LSTM) (None, 60, 50) 20200
_________________________________________________________________
dropout_3 (Dropout) (None, 60, 50) 0
_________________________________________________________________
lstm_4 (LSTM) (None, 50) 20200
_________________________________________________________________
dropout_4 (Dropout) (None, 50) 0
_________________________________________________________________
dense_1 (Dense) (None, 1) 51
=================================================================
Total params: 71,251
Trainable params: 71,251
Non-trainable params: 0
_________________________________________________________________
None
Затем я получаю ошибку, подобную этой:
ValueError: Error when checking target: expected dense_1 to have shape (1,) but got array with shape (2,)
Это говоря ожидаемая форма (1,), но получил (2,), это потому, что у меня есть 2 измерения, поэтому я должен изменить Dense(units=1)
на Dense(units=2)