本文介绍了我可以一次预测Keras时间序列的未来6个月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用keras进行时间序列预测.我的代码可以通过预测下一个月来预测下一个6个月,然后再次将其输入以进行下个月的预测,直到完成6个月为止.这意味着一个月预测6次.我可以一次预测下一个6个月吗?
I use keras for time series prediction. My code can predict next 6 months by predict next one month and then get it to be input for predict next month again untill complete 6 months. That means predict one month 6 times. Can I predict next 6 month in one time.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from keras.layers import LSTM
from pandas.tseries.offsets import MonthEnd
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense, Dropout
import keras.backend as K
from keras.layers import Bidirectional
from keras.layers import Embedding
from keras.layers import GRU
df = pd.read_csv('D://data.csv',
engine='python')
df['DATE_'] = pd.to_datetime(df['DATE_']) + MonthEnd(1)
df = df.set_index('DATE_')
df.head()
split_date = pd.Timestamp('03-01-2015')
train = df.loc[:split_date, ['data']]
test = df.loc[split_date:, ['data']]
sc = MinMaxScaler()
train_sc = sc.fit_transform(train)
test_sc = sc.transform(test)
X_train = train_sc[:-1]
y_train = train_sc[1:]
X_test = test_sc[:-1]
y_test = test_sc[1:]
K.clear_session()
model = Sequential()
model.add(Dense(12, input_dim=1, activation='relu'))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.summary()
model.fit(X_train, y_train, epochs=200, batch_size=2)
y_pred = model.predict(X_test)
real_pred = sc.inverse_transform(y_pred)
real_test = sc.inverse_transform(y_test)
print("Predict Value")
print(real_pred)
print("Test Value")
print(real_test)
推荐答案
是的,通过将输出层(最后一层)从Dense(1)更改为Dense(6).当然,您还必须将y_train和y_test更改为形状(1,6)而不是(1,1).
Yes, by changing your output layer (the last layer) from Dense(1) to Dense(6). Of course you also have to change your y_train and y_test to have shape (1,6) instead of (1,1).
好运.
这篇关于我可以一次预测Keras时间序列的未来6个月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!