0% found this document useful (0 votes)
13 views3 pages

Forecast Live Approach2

The document outlines a Python notebook that implements a rolling forecast model using ARIMA, XGBoost, and LSTM for predicting energy consumption data from a PostgreSQL database. It includes data fetching, processing, model training, and evaluation, along with visualizations of actual versus predicted values. The results are saved to Excel files, and accuracy metrics such as MAE, RMSE, and MAPE are calculated for each model.

Uploaded by

tiwaripav9427
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Forecast Live Approach2

The document outlines a Python notebook that implements a rolling forecast model using ARIMA, XGBoost, and LSTM for predicting energy consumption data from a PostgreSQL database. It includes data fetching, processing, model training, and evaluation, along with visualizations of actual versus predicted values. The results are saved to Excel files, and accuracy metrics such as MAE, RMSE, and MAPE are calculated for each model.

Uploaded by

tiwaripav9427
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

# 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'

# Fetch from PostgreSQL


def fetch_data(start_date, end_date):
engine = create_engine(f"postgresql://{DB_CONFIG['user']}:
{DB_CONFIG['password']}@{DB_CONFIG['host']}:{DB_CONFIG['port']}/
{DB_CONFIG['dbname']}")
query = f"""
SELECT "timestamp", eaf_1_lf_1, eaf_2_lf_2, eaf_3_lf_3, eaf_4_lf_4
FROM {TABLE_NAME}
WHERE "timestamp" BETWEEN '{start_date}' AND '{end_date}'
"""
df = pd.read_sql(query, engine)
df['timestamp'] = pd.to_datetime(df['timestamp'])
return df

# Process with Approach 2


def process_data_approach2(df):
df.set_index('timestamp', inplace=True)
eaf_avg = []
for col in ['eaf_1_lf_1','eaf_2_lf_2','eaf_3_lf_3','eaf_4_lf_4']:
resampled = df[[col]].resample('15T').sum() / 180
eaf_avg.append(resampled)
total = sum(eaf_avg)
total.columns = ['total_eaf']
return total['total_eaf'].dropna()

# Rolling forecast function


def rolling_forecast_dynamic(start_time, interval_minutes=15):
results_arima, results_xgb, results_lstm = [], [], []
for i in range(96):
current_time = start_time + timedelta(minutes=i * interval_minutes)
df_raw = fetch_data(current_time - timedelta(days=5), current_time)
series = process_data_approach2(df_raw)
if len(series) < 30 or current_time not in series.index:
continue
actual = series.get(current_time)

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

df_feat = series[series.index < current_time].to_frame('total')


df_feat['lag1'] = df_feat['total'].shift(1)
df_feat['lag2'] = df_feat['total'].shift(2)
df_feat['lag3'] = df_feat['total'].shift(3)
df_feat.dropna(inplace=True)
model_xgb = XGBRegressor(n_estimators=100)
model_xgb.fit(df_feat[['lag1','lag2','lag3']], df_feat['total'])
xgb_pred = model_xgb.predict(np.array(series[-3:]).reshape(1, -1))[0]
results_xgb.append({'timestamp': current_time, 'actual': actual,
'predicted': xgb_pred})

train_data = series[series.index < current_time]


scaler = MinMaxScaler()
scaled = scaler.fit_transform(train_data.to_frame())
X_lstm, y_lstm = [], []
for j in range(3, len(scaled)):
X_lstm.append(scaled[j-3:j])
y_lstm.append(scaled[j])
model_lstm = Sequential()
model_lstm.add(LSTM(50, activation='relu', input_shape=(3, 1)))
model_lstm.add(Dense(1))
model_lstm.compile(optimizer='adam', loss='mse')
model_lstm.fit(np.array(X_lstm), np.array(y_lstm), epochs=30, verbose=0)
lstm_pred_scaled = model_lstm.predict(scaled[-3:].reshape(1, 3, 1))[0][0]
lstm_pred = scaler.inverse_transform([[lstm_pred_scaled]])[0][0]
results_lstm.append({'timestamp': current_time, 'actual': actual,
'predicted': lstm_pred})

return pd.DataFrame(results_arima), pd.DataFrame(results_xgb),


pd.DataFrame(results_lstm)

# 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}%")

You might also like