如何更改Python LSTM模型代码以预测年度而不是月份(数据集包含年度数据),但模型给出每月

问题描述 投票:0回答:1

这是我使用 LSTM 的第一个方法。我正在尝试预测年度时间序列。我正在关注这个 Github 示例。但代码给出的是每月预测而不是年度预测。感谢您对识别代码更改的支持。

MY data set cointained annual data

,

我想更改Python代码来预测明年的值而不是下个月的值

from sklearn.preprocessing import MinMaxScaler

# Normalize the data
scaler = MinMaxScaler()
scaled_data = scaler.fit_transform(df_final)

# Define sequence length and features
sequence_length = 1  # Number of time steps in each sequence
num_features = len(df_final.columns)  # Assuming 12 features

# Create sequences and corresponding labels
sequences = []
labels = []
for i in range(len(scaled_data) - sequence_length):
    seq = scaled_data[i:i+sequence_length, :]  # Use all features with [:, :]
   # label = scaled_data[i+sequence_length, 11]  # 'Indnpa' column index
    label = scaled_data[i+sequence_length, 11]  # 'Indnpa' column index
    sequences.append(seq)
    labels.append(label)

# Convert to numpy arrays
sequences = np.array(sequences)
labels = np.array(labels)

# Split into train and test sets
train_size = int(0.8 * len(sequences))
train_x, test_x = sequences[:train_size], sequences[train_size:]
train_y, test_y = labels[:train_size], labels[train_size:]

print("Train X shape:", train_x.shape)
print("Train Y shape:", train_y.shape)
print("Test X shape:", test_x.shape)
print("Test Y shape:", test_y.shape)

下一个代码

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Dropout
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint

# Create the LSTM model
model = Sequential()

# Add LSTM layers with dropout
model.add(LSTM(units=128, input_shape=(train_x.shape[1], train_x.shape[2]), return_sequences=True))
model.add(Dropout(0.2))

model.add(LSTM(units=64, return_sequences=True))
model.add(Dropout(0.2))

model.add(LSTM(units=32, return_sequences=False))
model.add(Dropout(0.2))

# Add a dense output layer
model.add(Dense(units=1))

# Compile the model
model.compile(optimizer='adam', loss='mean_squared_error')

下一个代码

early_stopping = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)
model_checkpoint = ModelCheckpoint('/content/drive/MyDrive/dl/weather_prediction/best_model_weights.h5', monitor='val_loss', save_best_only=True)

# Train the model
history = model.fit(
    train_x, train_y,
    epochs=100,
    batch_size=64,
    validation_split=0.2,  # Use part of the training data as validation
    callbacks=[early_stopping, model_checkpoint]
)

最后一个代码

# Reshape predicted_temp to match the shape of df_final.index
predicted_temp = predicted_temp.reshape(-1, 1)

# Plot the data
plt.figure(figsize=(10, 6))
plt.plot(df_final.index[-2:], true_temp[-2:], label='Actual')
plt.plot(df_final.index[-2:], predicted_temp[-2:], label='Predicted')
plt.title('Industry NPA Prediction vs Actual')
plt.xlabel('Year')
plt.ylabel('NPA Ratio')
plt.legend()
plt.show()

enter image description here

python lstm
1个回答
0
投票

该模型正在预测年度值,但由于 matplotlib 绘制它们的方式,它显示为每月值。当您使用

.index[-2:]
时,您正在绘制最后两个值。您绘制的值位于 2023-01-01 和 2024-01-01,matplotlib 只是在它们之间绘制一条直线。

我认为如果你绘制更多的值,它会变得更清楚,我在下面的图中标记了值:

Plot with values marked out

© www.soinside.com 2019 - 2024. All rights reserved.