Example output
First part of the code: (output)
Certainly! Here's the Python code that follows the tasks outlined in the prompt:
```python
# Import necessary libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_absolute_error, mean_squared_error
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, GRU, Bidirectional, Dense, Flatten, Conv1D, MaxPooling1D
from tensorflow.keras.optimizers import Adam
# Load your data
# data = pd.read_csv('[YOUR FILE PATH]')
# For the sake of this example, I will use a placeholder. Replace this with your data.
data = pd.DataFrame({'date': pd.date_range(start='2023-01-01', periods=100, freq='D'),
'value': np.random.randn(100).cumsum()})
# Task 1: Data Extraction and Preparation
plt.plot(data['date'], data['value'])
plt.xlabel('Date')
plt.ylabel('Value')
plt.title('Initial Data Visualization')
plt.show()
# Ensure chronological order
data = data.sort_values(by='date')
# Normalize data
scaler = MinMaxScaler()
data['normalized_value'] = scaler.fit_transform(data[['value']])
# Task 2: Data Splitting
train_size = int(0.8 * len(data))
train_data = data[:train_size]
test_data = data[train_size:]
def create_sequences(data, sequence_length):
x, y = [], []
for i in range(len(data) - sequence_length):
x.append(data[i:i + sequence_length].values)
y.append(data[i + sequence_length])
return np.array(x), np.array(y)
sequence_length = 10
X_train, y_train = create_sequences(train_data['normalized_value'], sequence_length)
X_test, y_test = create_sequences(test_data['normalized_value'], sequence_length)
# For the sake of demonstration, we will define models for LSTM and GRU only. Extend as needed for other architectures.
# Task 4: Model Selection and Forecasting
# LSTM
lstm_model = Sequential([
LSTM(50, input_shape=(X_train.shape[1], X_train.shape[2]), return_sequences=True),
LSTM(50),
Dense(1)
])
lstm_model.compile(optimizer='adam', loss='mse')
lstm_model.fit(X_train, y_train, epochs=30, batch_size=8)
# GRU
gru_model = Sequential([
GRU(50, input_shape=(X_train.shape[1], X_train.shape[2]), return_sequences=True),
GRU(50),
Dense(1)
])
gru_model.compile(optimizer='adam', loss='mse')
gru_model.fit(X_train, y_train, epochs=30, batch_size=8)
# Extend for Bidirectional LSTM and 1D CNN...
# Task 6: Error Calculation
def evaluate_model(model, X_test, y_test):
y_pred = model.predict(X_test)
mae = mean_absolute_error(y_test, y_pred)
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
return mae, rmse
lstm_mae, lstm_rmse = evaluate_model(lstm_model, X_test, y_test)
gru_mae, gru_rmse = evaluate_model(gru_model, X_test, y_test)
# Output metrics for the models
print("LSTM - MAE:", lstm_mae, "RMSE:", lstm_rmse)
print("GRU - MAE:", gru_mae, "RMSE:", gru_rmse)
# Task 7: Summary & Visualization (limited to LSTM for demonstration)
plt.plot(y_test, label="Actual")
plt.plot(lstm_model.predict(X_test), label="Predicted by LSTM")
plt.legend()
plt.title('LSTM Forecast vs Actuals')
plt.show()
# Note: To make this code more comprehensive, add hyperparameter tuning, Bi-directional LSTM, 1D CNN model, and further visualizations.
```
This code provides a basic structure for executing time series forecasting with deep learning as described in your meta-prompt. To fully implement all the tasks, you'd need to expand the model-building, add the hyperparameter tuning, and make additional visualizations. Remember to replace the placeholder data with your actual time series data.
βββ-
Certainly! Let's continue with building the remaining models and then introduce hyperparameter tuning using grid search as an example:
```python
# Continuing from where we left off...
# Bidirectional LSTM
bidirectional_model = Sequential([
Bidirectional(LSTM(50, return_sequences=True), input_shape=(X_train.shape[1], X_train.shape[2])),
Bidirectional(LSTM(50)),
Dense(1)
])
bidirectional_model.compile(optimizer='adam', loss='mse')
bidirectional_model.fit(X_train, y_train, epochs=30, batch_size=8)
# 1D CNN
cnn_model = Sequential([
Conv1D(filters=64, kernel_size=2, activation='relu', input_shape=(X_train.shape[1], X_train.shape[2])),
MaxPooling1D(pool_size=2),
Flatten(),
Dense(50, activation='relu'),
Dense(1)
])
cnn_model.compile(optimizer='adam', loss='mse')
cnn_model.fit(X_train, y_train, epochs=30, batch_size=8)
# Task 6: Error Calculation (Continued)
bidirectional_mae, bidirectional_rmse = evaluate_model(bidirectional_model, X_test, y_test)
cnn_mae, cnn_rmse = evaluate_model(cnn_model, X_test, y_test)
print("Bidirectional LSTM - MAE:", bidirectional_mae, "RMSE:", bidirectional_rmse)
print("1D CNN - MAE:", cnn_mae, "RMSE:", cnn_rmse)
# Task 5: Hyperparameter Tuning
from tensorflow.keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import GridSearchCV
def create_lstm_model(neurons=50, optimizer='adam'):
model = Sequential([
LSTM(neurons, input_shape=(X_train.shape[1], X_train.shape[2]), return_sequences=True),
LSTM(neurons),
Dense(1)
])
model.compile(optimizer=optimizer, loss='mse')
return model
lstm_regressor = KerasRegressor(build_fn=create_lstm_model, epochs=30, batch_size=8, verbose=0)
parameters = {
'neurons': [30, 50],
'optimizer': ['adam', 'rmsprop'],
'batch_size': [8, 16],
'epochs': [30, 50]
}
grid_search = GridSearchCV(estimator=lstm_regressor, param_grid=parameters, scoring='neg_mean_squared_error', cv=10)
grid_search = grid_search.fit(X_train, y_train)
best_parameters = grid_search.best_params_
best_accuracy = grid_search.best_score_
print("Best Parameters:", best_parameters)
print("Best Accuracy:", best_accuracy)
# You can similarly extend hyperparameter tuning for other models like GRU, Bidirectional LSTM, and 1D CNN.
```
This code expands upon model building for Bidirectional LSTM and 1D CNN, and adds hyperparameter tuning using grid search for the LSTM model. The grid search process can be time-consuming depending on the range and number of hyperparameters being tuned.
For the sake of simplicity, the hyperparameters tuned for LSTM here are limited. For a more comprehensive approach, you would add additional hyperparameters and perform tuning for other models as well.