This repository contains an end-to-end Machine Learning project that predicts next-day household energy consumption. The project uses the Individual household electric power consumption subset from the UCI Machine Learning Repository.
The dataset contains over 2 million records (minute-resolution measurements gathered over almost 4 years) of electric power consumption in a household in Sceaux, France.
The goal of this project is to build a time-series forecasting model using Scikit-Learn and XGBoost regressors to predict the total energy usage (Global_active_power) for the next hour. This is achieved by creating rolling window features, finding lagged variables, and extracting meaningful date-time properties from the data.
- Python 3.9+
- Pandas & NumPy (Data processing and feature engineering)
- Scikit-Learn & XGBoost (Machine Learning regressors)
- Matplotlib & Seaborn (Exploratory Data Analysis)
- Streamlit (Interactive Dashboard)
- Joblib (Model Exportation)
The minute-level data is resampled to an hourly frequency, then we generate the following features:
- Datetime Properties:
hour,day_of_week,month, and anis_weekendflag. - Lag Features: Historical energy consumption (
lag_1hour,lag_24hour). - Rolling Statistics: 24-hour and 7-day moving averages (
rolling_mean_24handrolling_mean_7day).
Create a virtual environment and install the required dependencies:
python -m venv venv
# On Windows
venv\Scripts\activate.bat
# On macOS/Linux
source venv/bin/activate
pip install -r requirements.txtRun the data pipeline script. This will download the dataset, parse the 2 million datetimes, resample to hourly frequency, engineer features, and generate Exploratory Data Analysis (EDA) plots in the results/ folder.
python data_pipeline.pyRun the modeling training script. This script performs a temporal train-test split, trains a Naive Baseline, Linear Regression, Random Forest, and XGBoost regressor, then saves the best-performing model to models/best_model.joblib.
python train_evaluate.pyRun the Streamlit application to visually explore the forecasted predictions vs. the actual test data.
streamlit run app.pyModels are evaluated on Mean Absolute Error (MAE), Root Mean Squared Error (RMSE), and R² Score.
- XGBoost achieved the strongest performance with an R² of ~0.61 and an MAE of ~0.31 kW on the held-out test set, capturing the complex non-linear consumption patterns.
- Naive Baseline: Predicting next hour's usage using the previous hour's usage yielded an R² of -0.17, proving that the trained models are successfully extracting forward-looking patterns.
- The most important predictive features are
lag_1hourandhourof the day. - Plots comparing actual vs. predicted values, alongside feature importance distributions, are automatically saved to the
results/folder upon running the training script.