Forecast Live Approach2
Forecast Live Approach2
ipynb
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
from sqlalchemy import create_engine
from sklearn.metrics import mean_absolute_error, mean_squared_error
from xgboost import XGBRegressor
from statsmodels.tsa.arima.model import ARIMA
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore")
# Configuration
DB_CONFIG = {
'host': 'your_host',
'port': '5432',
'dbname': 'your_db',
'user': 'your_user',
'password': 'your_password'
}
TABLE_NAME = 'public.mrss_1'
try:
model_arima = ARIMA(series[series.index < current_time], order=(2, 1,
2))
fit_arima = model_arima.fit()
pred_arima = fit_arima.forecast(steps=1)[0]
results_arima.append({'timestamp': current_time, 'actual': actual,
'predicted': pred_arima})
except:
continue
# Main Execution
now = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
arima_df, xgb_df, lstm_df = rolling_forecast_dynamic(now)
arima_df.to_excel("arima_forecast_live_approach2.xlsx", index=False)
xgb_df.to_excel("xgboost_forecast_live_approach2.xlsx", index=False)
lstm_df.to_excel("lstm_forecast_live_approach2.xlsx", index=False)
# Visualization
for name, df in zip(['ARIMA', 'XGBoost', 'LSTM'], [arima_df, xgb_df, lstm_df]):
plt.figure(figsize=(14, 4))
plt.plot(df['timestamp'], df['actual'], label='Actual')
plt.plot(df['timestamp'], df['predicted'], label='Predicted')
plt.title(f"{name} - Actual vs Predicted")
plt.xlabel('Time')
plt.ylabel('EAF (MW)')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
# Accuracy
for name, df in zip(['ARIMA', 'XGBoost', 'LSTM'], [arima_df, xgb_df, lstm_df]):
mae = mean_absolute_error(df['actual'], df['predicted'])
rmse = mean_squared_error(df['actual'], df['predicted'], squared=False)
mape = np.mean(np.abs((df['actual'] - df['predicted']) / df['actual'])) * 100
print(f"{name} -> MAE: {mae:.2f}, RMSE: {rmse:.2f}, MAPE: {mape:.2f}%")