A rigorous scientific approach to residential energy consumption analysis and forecasting using machine learning.
Author: Dhouha Meliane
Duration: 2-day intensive research project
Date: December 2025
- Executive Summary
- Research Objectives
- Theoretical Framework & Research Papers
- Methodology Pipeline
- Data Processing Stages
- Scientific Impact
- Contact & License
- References
This research project implements a comprehensive pipeline for residential energy consumption analysis and predictive modeling, addressing the critical challenge of smart home energy management. Using the UCI Individual Household Electric Power Consumption dataset containing over 2 million timestamped measurements (2006-2010), we developed an end-to-end machine learning system achieving R² = 0.938 in consumption forecasting.
| Metric | Target | Achieved | Status |
|---|---|---|---|
| R² Score | > 0.85 | 0.938 | Exceeded |
| RMSE | < 0.15 kWh | 0.265 kWh | Acceptable |
| MAPE | < 5% | 9.7% | Acceptable |
| Data Quality | < 1% missing | 0% after cleaning | Achieved |
| F1-Score (±10%) | > 0.70 | 0.776 | Exceeded |
- Data Infrastructure: Design a normalized SQL database schema following Third Normal Form (3NF) principles
- Data Quality Assurance: Apply scientifically validated techniques for missing value imputation and outlier detection
- Feature Engineering: Develop temporally-aware features respecting forecast horizons to prevent data leakage
- Predictive Modeling: Compare machine learning algorithms (Linear Regression, Random Forest, XGBoost) optimized for computational efficiency
- Operational Deployment: Create a reproducible, well-documented system for smart home applications
- Anti-leakage feature engineering methodology ensuring realistic model performance
- Hybrid evaluation framework combining regression metrics with tolerance-based classification accuracy
- CPU-optimized hyperparameter configurations for resource-constrained environments
- Comprehensive temporal analysis of residential energy consumption patterns
This project synthesizes methodologies from cutting-edge research in energy forecasting, feature selection, and machine learning optimization. The following table maps each research paper to its specific application in our pipeline:
| Research Paper | Application in Project | Pipeline Stage |
|---|---|---|
| Machine Learning Methods for Forecasting and Modeling in Smart Grid (Ahmad et al., 2024) | Feature selection methods combining correlation, mutual information, and tree-based importance | [3] Feature Engineering, [5] EDA |
| Deep Learning Based Ensemble Approach for Probabilistic Wind Power Forecasting (Wang et al., 2023) | Multi-method correlation analysis (Pearson, Spearman, Kendall) for smart home datasets | [5] Exploratory Data Analysis |
| A Deep Learning Architecture for Predictive Analytics in Energy Systems (Mocanu et al., 2023) | Feature importance in deep learning validation | [5] EDA - Feature Analysis |
| Data Consistency for Data-Driven Smart Energy Assessment (Zhang & Chen, 2022) | Multicollinearity detection and mitigation strategies | [3] Feature Engineering |
| A Review on Artificial Intelligence Based Load Demand Forecasting Techniques for Smart Grid and Buildings (Raza & Khosravi, 2015) | Tree-based methods for capturing feature interactions in energy data | [5] EDA, [6] Modeling |
| A Practical Time Series Forecasting Guideline for Machine Learning (Servis, 2024) | Anti-leakage methodology: lag period ≥ forecast horizon | [3] Feature Engineering |
| Short-Term Load Forecasting Based on Optimized Random Forest and Optimal Feature Selection (Shi et al., 2024) | Random Forest hyperparameter optimization for short-term load forecasting (STLF) on CPU-constrained systems | [6] Predictive Modeling |
| Variance Reduced Training with Stratified Sampling for Forecasting Models (Lu et al., 2021) | Stratified sampling and temporal validation principles | [6] Predictive Modeling |
| XGBoost: A Scalable Tree Boosting System (Chen & Guestrin, 2016) | XGBoost histogram-based algorithm and early stopping mechanisms | [6] Predictive Modeling |
| Energy Forecasting in a Public Building: A Benchmarking Analysis on LSTM, SVR, and XGBoost Networks (Chung & Gu, 2022) | Comparative benchmarking of Random Forest, XGBoost, and SVR | [7] Evaluation Metrics |
-
Feature Selection (Ahmad et al., 2024): https://onlinelibrary.wiley.com/doi/10.1002/9781394231522.ch12
Ensemble approaches combining correlation analysis, mutual information, and Random Forest importance achieve superior feature selection compared to single-method approaches, reducing overfitting by 15-20%. -
Anti-Leakage Engineering (Servis, 2024): https://sertiscorp.medium.com/a-practical-time-series-forecasting-guideline-for-machine-learning-part-ii-aea360b06ce2
Enforcing lag periods greater than or equal to forecast horizons prevents look-ahead bias, reducing artificially inflated R² scores from >0.99 to realistic ranges of 0.85-0.94. -
Temporal Interpolation: Time-based interpolation for missing values preserves temporal continuity better than forward-fill or mean imputation, reducing RMSE by 8-12% in energy time series.
-
CPU Optimization (Shi et al., 2024): https://www.mdpi.com/1996-1073/17/8/1926
Reducing Random Forest depth (max_depth=15) and tree count (n_estimators=100) while increasing min_samples_leaf (10) maintains 95% of full-model accuracy with 60% faster training. -
Stratified Sampling (Lu et al., 2021): https://arxiv.org/abs/2103.02062
Variance-reduced training with stratified sampling for forecasting models addresses heterogeneity in temporal patterns, improving gradient estimation and reducing training time in large-scale time series forecasting. -
XGBoost Optimization (Chen & Guestrin, 2016): https://arxiv.org/abs/1603.02754
Histogram-based split-finding algorithm with built-in cross-validation enables early stopping to prevent overfitting while maintaining computational efficiency through cache-aware prefetching and sparsity-aware algorithms. -
Hybrid Evaluation Framework: Tolerance-based classification metrics (±10% accuracy, F1-score) complement regression metrics, providing operational deployment context where exact predictions are less critical than acceptable ranges.
Source: UCI Machine Learning Repository - Individual Household Electric Power Consumption
http://archive.ics.uci.edu/dataset/235/individual+household+electric+power+consumption
- Temporal Coverage: December 2006 - November 2010 (47 months)
- Granularity: 1-minute sampling intervals
- Total Observations: 2,075,259 measurements
- Variables: 8 features (timestamp,active/reactive power, voltage, current, sub-metering)
The raw dataset employs semicolon-separated values with non-standard missing value encodings (?). Our parsing pipeline implements:
- Datetime Consolidation: Merging separate date and time columns into unified ISO 8601 timestamps
- Type Optimization: Converting to appropriate numeric dtypes (float32)
- Missing Value Marking: Explicit identification of
?markers asNaN
Technical Implementation: pandas.read_csv() with custom date parser ensuring correct temporal ordering.
Research Justification: Follows data quality assessment principles from energy forecasting literature emphasizing proper temporal indexing.
- Missing Values: 25,979 records (1.25% of dataset)
- Temporal Gaps: Irregular intervals due to sensor failures
- Physical Anomalies: Voltage readings outside European standard range (220-260V)
- Statistical Outliers: Power consumption exceeding 3σ from mean
Method: Temporal interpolation (method='time') with bidirectional filling (limit_direction='both')
Research Foundation: Time-based interpolation preserves temporal continuity essential for energy time series (Wang et al., 2023).
Mathematical Formulation:
For a missing value at time
Results: 100% imputation success rate (25,979 → 0 missing values)
Interquartile Range (IQR) Method:
where
Rationale: Less sensitive to extreme values compared to z-score approaches, making it suitable for energy data with natural variability.
Enforced domain-specific constraints based on European electrical standards:
| Constraint | Minimum | Maximum | Justification |
|---|---|---|---|
| Active Power | 0 kW | 15 kW | Physical impossibility of negative consumption |
| Voltage | 220 V | 260 V | EN 50160 standard: 230V ±10% |
| Current | 0 A | 60 A | Residential circuit breaker ratings |
Result: Removed 0.8% of records violating physical constraints.
Research Foundation: Servis (2024) emphasizes that lag period must be ≥ forecast horizon to prevent look-ahead bias.
A fundamental requirement in time series forecasting is ensuring features are constructed using only information available at prediction time, avoiding look-ahead bias that artificially inflates model performance.
Variables directly computed from the target or exhibiting correlation > 0.95 were systematically removed:
Sub_metering_1/2/3(computed from target components)Global_intensity(perfect correlation r=1.00)Voltage(high correlation r>0.85)Global_reactive_power(derived feature)apparent_power(calculated field)
Research Justification: Zhang & Chen (2022) demonstrate that multicollinearity degrades model interpretability and stability.
Calendar-based features capturing daily, weekly, and annual patterns:
- Linear:
hour,day_of_week,month,quarter - Binary:
is_weekend,is_business_hour,is_peak_hour - Categorical:
season(winter/spring/summer/fall)
Cyclical Encoding: Preserving periodicity using trigonometric transformation:
Scientific Justification: Sine-cosine encoding ensures the model recognizes that hour 23 is temporally close to hour 0 (Wang et al., 2023).
Historical consumption values delayed by predefined intervals:
Implemented lags: 1, 2, 3, 5, 10 minutes | 1, 6, 12 hours | 1, 7 days
Anti-leakage guarantee: All lags
Moving averages and standard deviations over temporal windows:
Windows: 60 minutes (1h), 360 minutes (6h), 1440 minutes (24h)
Critical Safeguard: .shift(1) applied before rolling calculation to exclude current timestep.
Research Foundation: Ahmad et al. (2024) recommend rolling statistics for capturing recent trends without data leakage.
Final Feature Count: 39 engineered features
households (1) ──────── (*) energy_measurements
│
├──── (*) sub_meters
└──── (*) predictions
households (1) ──────── (*) hourly_consumption
households (1) ──────── (*) daily_consumption
energy_measurements (Fact Table): Minute-level observations with B-tree indexes on timestamp and household_id
hourly_consumption (Aggregated View): Pre-computed hourly statistics reducing query time by 95%
predictions (Model Outputs): Forecasted values with confidence metrics
Research Foundation: Multi-method approach combining Pearson, Spearman, Kendall, Mutual Information, and Random Forest importance (Ahmad et al., 2024; Wang et al., 2023).
Daily Profile:
- Morning surge: 7:00-9:00 AM (avg 1.8 kW)
- Evening peak: 18:00-21:00 PM (avg 2.4 kW)
- Nighttime baseline: 0:00-6:00 AM (avg 0.5 kW)
Weekly Seasonality:
- Weekend consumption 12% higher during mid-day
- Weekday morning peak 15% sharper (compressed window)
Annual Trends:
- Winter months: 18% higher average consumption
- Summer months: 8% lower than annual mean
Top correlates with target (after removing forbidden variables):
Sub_metering_3: r = 0.73 (electric heating)Sub_metering_1: r = 0.21 (kitchen appliances)
Interpretation: All three correlation matrices (Pearson, Spearman, Kendall) show consistent patterns, validating linear relationships.
Key Finding: Variables showing high MI but low Pearson correlation indicate non-linear relationships captured by mutual information analysis (Ahmad et al., 2024).
Variance Explained:
- PC1: 32.7% (individual variance)
- Cumulative: 95% threshold reached at PC5
- Total components needed: 5 out of 6
Interpretation: High dimensionality reduction potential exists, but we retain original features for interpretability in operational deployment.
Research Foundation: Comparative study by HAL (2024) and optimization strategies from Shi et al. (2024) for CPU-constrained environments.
Method: Quantile-based stratification to preserve target distribution while reducing computational load.
Research Justification: Asghar et al. (2024) demonstrate stratified sampling maintains statistical properties while reducing overfitting risk.
Configuration: 80/20 chronological split
- Training: December 2006 - May 2010
- Testing: June 2010 - November 2010
Rationale: Chronological split preserves temporal ordering, simulating real-world deployment scenarios.
Purpose: Establishes baseline performance for comparison.
Optimization Strategy (Shi et al., 2024):
| Parameter | Optimized Value | Default Value | Justification |
|---|---|---|---|
n_estimators |
100 | 500 | 60% faster training, 95% accuracy retention |
max_depth |
15 | None | Prevents overfitting, reduces memory |
min_samples_leaf |
10 | 1 | Smooths leaf predictions, regularization |
max_features |
'sqrt' | 'auto' | Reduces tree correlation |
max_samples |
0.8 | None | Bootstrap sampling efficiency |
Algorithmic Principle:
where
Optimization Strategy (Chen & Guestrin, 2016):
| Parameter | Optimized Value | Default Value | Technical Detail |
|---|---|---|---|
n_estimators |
300 | 100 | Balanced convergence |
learning_rate |
0.1 | 0.3 | Step size shrinkage η |
max_depth |
5 | 6 | Shallow trees prevent overfitting |
tree_method |
'hist' | 'auto' | Histogram-based algorithm (faster) |
max_bin |
128 | 256 | Reduced memory footprint |
Objective Function:
Gradient Boosting Update:
Definition:
Results:
| Model | R² Score | Performance |
|---|---|---|
| Linear Regression | 0.938 | Exceeds target (0.85) |
| Random Forest | 0.927 | Exceeds target (0.85) |
| XGBoost | 0.938 | Exceeds target (0.85) |
Definition:
Results:
| Model | RMSE (kW) | % of Mean |
|---|---|---|
| Linear Regression | 0.265 | 24.5% |
| Random Forest | 0.287 | 26.5% |
| XGBoost | 0.265 | 24.5% |
Results:
| Model | MAE (kW) | MAPE (%) |
|---|---|---|
| Linear Regression | 0.100 | 10.4% |
| Random Forest | 0.119 | 12.7% |
| XGBoost | 0.100 | 9.7% |
Analysis: XGBoost achieves <10% MAPE, approaching the 5% threshold for operational deployment.
Definition:
Results:
| Tolerance | Linear Regression | Random Forest | XGBoost |
|---|---|---|---|
| ±5% | 52.6% | 49.0% | 59.3% |
| ±10% | 73.0% | 68.9% | 77.6% |
| ±15% | 81.9% | 78.5% | 84.5% |
| ±20% | 87.3% | 83.8% | 88.2% |
Interpretation: XGBoost achieves 77.6% accuracy within ±10% operational threshold, suitable for smart grid deployment.
Results:
| Model | F1-Score |
|---|---|
| Linear Regression | 0.730 |
| Random Forest | 0.689 |
| XGBoost | 0.776 |
target_lag_1(30.0%) - 1-minute autoregressive lagrolling_mean_60(21.8%) - 1-hour moving averagerolling_std_60(15.2%) - 1-hour volatilityrolling_mean_360(10.6%) - 6-hour trendhour_sin(7.1%) - Cyclical hour encoding
target_lag_1(58.3%) - 1-minute autoregressive lagrolling_mean_60(12.4%) - 1-hour moving averagerolling_mean_1440(5.8%) - 24-hour trendtarget_lag_60(3.7%) - 1-hour laghour(2.9%) - Linear hour feature
Research Insight: XGBoost's stronger reliance on lag-1 (58.3% vs 30.0%) confirms gradient boosting leverages autoregressive signals more aggressively (Raza & Khosravi, 2023).
Pattern Analysis:
- All models successfully capture daily oscillations
- Peak events (>4 kW) tracked accurately
- Nighttime baselines (<1 kW) well-predicted
- Minimal heteroscedasticity across power ranges
- Source Code: Modular Python scripts with comprehensive docstrings
- SQL Schema: Normalized database design with indexing strategies
- Data Exports: CSV, JSON, SQL dump formats, png
- Visualizations: High-resolution figures for all analyses
- Data Dictionary: Complete variable descriptions and units
-
Methodological Innovation: Anti-leakage feature engineering framework preventing overly optimistic performance metrics
-
Practical Deployment: CPU-optimized configurations enabling real-time forecasting on edge devices in smart homes
-
Hybrid Evaluation: Tolerance-based metrics bridging gap between regression accuracy and operational requirements
-
Interpretability: Feature importance analysis identifying lag-1 consumption and hourly rolling statistics as dominant predictors
- Demand Response: 77.6% accuracy within ±10% enables reliable load scheduling
- Anomaly Detection: MAPE <10% facilitates identification of abnormal consumption patterns
- Energy Management: Hourly forecasts support optimization of renewable energy integration
- Behavioral Analysis: Temporal patterns inform user feedback systems
Current Limitations:
- Single-household dataset limits generalizability
- RMSE exceeds 0.15 kW target due to natural consumption variability
- No incorporation of external factors (weather, occupancy sensors)
Future Directions:
- Deep learning architectures (LSTM, Transformers) for longer forecast horizons
- Multi-household analysis for transfer learning validation
- Integration of exogenous variables (temperature, day type)
- Real-time deployment on IoT edge computing platforms
- Ahmad et al. (2024). Machine Learning Forecasting Growth Trends in Smart Grid. IEEE Access
- Wang et al. (2023). Short-term Load Forecasting using Deep Learning. Energy & Buildings
- Shi et al. (2024). Random Forest Hyperparameter Optimization for STLF
- Asghar et al. (2024). Machine Learning for Electricity Forecasting
- Chen & Guestrin (2016). XGBoost: A Scalable Tree Boosting System. KDD
Dhouha Meliane
Email: [dhouha.meliane@esprit.tn]
Linkedin: https://www.linkedin.com/in/dhouha-meliane/
This project is licensed under the MIT License - see LICENSE file for details.
