Keras time series can I predict next 6 month in one time

ぐ巨炮叔叔 提交于 2020-01-06 05:38:07

问题


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)

回答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).

Best of luck.



来源:https://stackoverflow.com/questions/53252152/keras-time-series-can-i-predict-next-6-month-in-one-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!