Time Series Forecasting with LSTM
Duration: 5 min
This module delves into the application of Long Short-Term Memory (LSTM) networks for time series forecasting. LSTMs are a type of recurrent neural network (RNN) designed to learn long-term dependencies, making them particularly effective for time series data. Understanding and implementing LSTMs can significantly enhance the accuracy of your time series predictions.
Understanding LSTM Networks
LSTM networks are an advanced type of RNN that can capture long-term dependencies in sequential data. Unlike traditional RNNs, LSTMs incorporate memory cells that allow them to retain information over long sequences. This makes them highly suitable for time series forecasting, where past values can influence future predictions.
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
# Generate sample time series data
np.random.seed(0)
data = np.sin(np.linspace(0, 3 * np.pi, 100)) + np.random.normal(scale=0.5, size=100)
# Prepare data for LSTM
def create_dataset(data, look_back=1):
X, Y = [], []
for i in range(len(data) - look_back):
X.append(data[i:(i + look_back)])
Y.append(data[i + look_back])
return np.array(X), np.array(Y)
look_back = 10
X, Y = create_dataset(data, look_back)
# Reshape input to be [samples, time steps, features]
X = np.reshape(X, (X.shape[0], 1, X.shape[1]))
# Create and fit the LSTM network
model = Sequential()
model.add(LSTM(50, input_shape=(1, look_back)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(X, Y, epochs=100, batch_size=1, verbose=2)
# Make predictions
train_predict = model.predict(X)
print(train_predict[:5])[[-0.08832055]
[ 0.2858177 ]
[ 0.61521256]
[ 0.7674112 ]
[ 0.72562135]]Evaluating LSTM Model Performance
After training an LSTM model, it's crucial to evaluate its performance to ensure it generalizes well to unseen data. Common metrics for time series forecasting include Mean Absolute Error (MAE) and Root Mean Squared Error (RMSE). Additionally, visualizing the predicted vs. actual values can provide insights into the model's accuracy and areas for improvement.
from sklearn.metrics import mean_absolute_error, mean_squared_error
import matplotlib.pyplot as plt
# Calculate MAE and RMSE
mae = mean_absolute_error(Y, train_predict)
rMSE = np.sqrt(mean_squared_error(Y, train_predict))
print(f'MAE: {mae}')
print(f'RMSE: {rMSE}')
# Plot actual vs predicted values
plt.plot(data, label='Actual')
plt.plot(range(look_back, look_back + len(train_predict)), train_predict, label='Predicted')
plt.legend()
plt.show()💡 Tip: When working with LSTMs for time series forecasting, ensure your data is properly scaled and normalized. This can significantly impact the model's performance and convergence during training.
❓ What is the primary advantage of using LSTM networks for time series forecasting?
❓ Which metric is commonly used to evaluate the performance of an LSTM model in time series forecasting?
Key Concepts
| Concept | Description |
|---|---|
| Trend | Core principle in this module |
| Seasonality | Core principle in this module |
| Stationarity | Core principle in this module |
| Autocorrelation | Core principle in this module |
Check Your Understanding
❓ How does Time handle edge cases?
❓ What is the computational complexity of Time?
❓ Which hyperparameter is most critical for Time?