对于我的实验,我有一个格式化的CSV file,其中包含三列(time_stamp,X和Y-其中Y是实际值)。我想根据过去值的时间索引来预测Y的X值。在下图中可以看到,使用机器学习回归进行模式预测似乎效果很好。

python - Keras-使用LSTM进行模式预测-LMLPHP

我想使用深度学习技术(例如,使用LSTM)重现此预测(图),这就是我使用Keras处理问题的方式。

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import LSTM, Dense
from keras.preprocessing.sequence import TimeseriesGenerator
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split

np.random.seed(7)

df = pd.read_csv('test32_C_data.csv')
n_features=10

values = df.values

for i in range(0,n_features):
    df['X_t'+str(i)] = df['X'].shift(i)
    df['X_tp'+str(i)] = (df['X'].shift(i) - df['X'].shift(i+1))/(df['X'].shift(i))

print(df)
pd.set_option('use_inf_as_null', True)

#df.replace([np.inf, -np.inf], np.nan).dropna(axis=1)
df.dropna(inplace=True)

X = df.drop('Y', axis=1)
y = df['Y']


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.40)

X_train = X_train.drop('time', axis=1)
X_train = X_train.drop('X_t1', axis=1)
X_train = X_train.drop('X_t2', axis=1)
X_test = X_test.drop('time', axis=1)
X_test = X_test.drop('X_t1', axis=1)
X_test = X_test.drop('X_t2', axis=1)


sc = MinMaxScaler()

X_train = np.array(df['X'])
X_train = X_train.reshape(-1, 1)
X_train = sc.fit_transform(X_train)

y_train = np.array(df['Y'])
y_train=y_train.reshape(-1, 1)
y_train = sc.fit_transform(y_train)


model_data = TimeseriesGenerator(X_train, y_train, 100, batch_size = 10)

# Initialising the RNN
model = Sequential()

# Adding the input layerand the LSTM layer
model.add(LSTM(4, input_shape=(None, 1)))

# Adding the output layer
model.add(Dense(1))

# Compiling the RNN
model.compile(loss='mse', optimizer='rmsprop')

# Fitting the RNN to the Training set
model.fit_generator(model_data)

# evaluate the model
scores = model.evaluate(X_train, y_train)
#print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))

# Getting the predicted values
predicted = X_test
predicted = np.reshape(-1, 1)
predicted = sc.transform(predicted)
y_pred = model.predict(predicted)
y_pred = sc.inverse_transform(y_pred)


但是,当我尝试如上所述运行预测模型(y_pred = model.predict(predicted))时-我收到以下错误。

ValueError: Error when checking: expected lstm_2_input to have 3 dimensions, but got array with shape (1, 1)

我在这里做错了什么?我将不胜感激。

最佳答案

LSTM输入具有等级3:


批次大小:在您的情况下为1,但任何值都可以使用。
时间步长:模型接受任何值,因此1可以接受,但是更长的输入序列通常可以带来更好的预测。
特征:在模型中表示为1


因此,您应该将predicted重塑为:

predicted = predicted.values.reshape((-1, 1, 1))

07-28 02:45
查看更多