gpt4 book ai didi

python - Keras 时间序列我可以一次性预测下 6 个月吗

转载 作者:行者123 更新时间:2023-11-30 09:45:27 25 4
gpt4 key购买 nike

我使用keras进行时间序列预测。我的代码可以通过预测下一个月来预测接下来的 6 个月,然后将其输入以再次预测下个月,直到完成 6 个月。这意味着一个月预测 6 次。我可以一次性预测下 6 个月吗?

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)。

祝你好运。

关于python - Keras 时间序列我可以一次性预测下 6 个月吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53252152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com