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