Untitled
unknown
plain_text
2 months ago
2.3 kB
6
Indexable
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.ensemble import AdaBoostRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.metrics import mean_absolute_error
# 1. LOAD & ENGINEER FEATURES (Memory-Based Features)
# Context: Validated on Panama National Grid Dataset
df = pd.read_csv('Post-dispatch.xlsx - Post-dispatch Query.csv')
df['Fecha Hora'] = pd.to_datetime(df['Fecha Hora'])
df['Hour'] = df['Fecha Hora'].dt.hour
df['DayOfWeek'] = df['Fecha Hora'].dt.dayofweek
df['Is_Weekend'] = df['DayOfWeek'].apply(lambda x: 1 if x >= 5 else 0)
df.set_index('Fecha Hora', inplace=True)
# "Memory" Features (The LSTM replacement)
df['Lag_1h'] = df['Carga Real'].shift(1)
df['Lag_2h'] = df['Carga Real'].shift(2)
df['Lag_3h'] = df['Carga Real'].shift(3)
df['Lag_24h'] = df['Carga Real'].shift(24)
df['Lag_168h'] = df['Carga Real'].shift(168)
df = df.dropna()
# 2. SPLIT DATA
split = int(len(df) * 0.8)
train_df = df.iloc[:split]
test_df = df.iloc[split:].copy()
# 3. TRAIN 24 HOURLY EXPERTS & PLOT
fig, axes = plt.subplots(6, 4, figsize=(20, 25))
axes = axes.flatten()
features = ['Lag_1h', 'Lag_2h', 'Lag_3h', 'Lag_24h', 'Lag_168h', 'Is_Weekend']
for h in range(24):
train_h = train_df[train_df['Hour'] == h]
test_h = test_df[test_df['Hour'] == h].tail(30) # Evaluating last 30 days
# Using base_estimator for Scikit-Learn 0.24.1 compatibility
model = AdaBoostRegressor(
base_estimator=DecisionTreeRegressor(max_depth=5),
n_estimators=100,
learning_rate=0.1,
random_state=42
)
model.fit(train_h[features], train_h['Carga Real'])
preds = model.predict(test_h[features])
mae = mean_absolute_error(test_h['Carga Real'], preds)
# Plotting for Technical Review
ax = axes[h]
ax.plot(test_h.index, test_h['Carga Real'], label='Actual', color='black', alpha=0.6)
ax.plot(test_h.index, preds, label='Pred', color='blue', linestyle='--')
ax.set_title(f'Hour {h}:00 | MAE: {mae:.1f} MW')
ax.tick_params(axis='x', rotation=45)
plt.suptitle('QuantGrid-24: 24-Hour Expert Ensemble Performance', fontsize=24, y=1.02)
plt.tight_layout()
plt.savefig('24_hour_quant_ensemble.png')
plt.show()Editor is loading...
Leave a Comment