Predicts whether Total Harmonic Distortion (THD) on a power system is in a HIGH or LOW state, based on simultaneous real power, voltage, and current readings. Built on real power-quality measurement data (harmonics 1-49, 3 phases).
[Try the live app here]](https://harmonic-distortion-e5bzwwfkatyc5knbaw9jrm.streamlit.app/)
This project started as an attempt to forecast tomorrow's THD from the last few days of readings a natural first approach for time-series data.
That approach didn't work, and the notebook documents why:
- Lag features (1-3 days), rolling averages, rolling volatility, and day-of-week were all tested as predictors
- Every feature showed near-zero correlation with next-day THD (all
|r| < 0.11) - The tuned SVR model converged to its most conservative setting (
C=0.1) and scored a negative R² - meaning it performed no better than simply guessing the average value every time
Rather than force a result that wasn't there, the project was reframed: instead of predicting the future, classify the present given a moment's power, voltage, and current readings, is the system in a HIGH or LOW THD state right now?
This approach worked:
- Real power showed a real difference between HIGH and LOW THD states (~28% lower during high-distortion periods)
- An SVM classifier (RBF kernel) reached 61% accuracy on held-out test data - a real, if modest, signal
- A Random Forest comparison scored slightly lower (58%), so the SVM was kept as the final model
| Section | Content |
|---|---|
| 1. Setup | Imports |
| 2. Load & Prepare Data | Read raw Excel export, remove duplicate columns |
| 3. Feature Engineering | Real power (3 phases), THD (3 phases), RMS voltage/current, power factor |
| 4. Modeling | THD-state classification: SVM (final model) vs. Random Forest, saved with joblib |
| Appendix | The abandoned day-ahead forecasting attempt, kept for transparency |
- Data & modeling:
pandas,numpy,scikit-learn - Demo app:
streamlit - Model persistence:
joblib
git clone https://github.com/AlmaFatima/Harmonic-Distortion.git
cd harmonic-distortion
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txt
streamlit run App.pyharmonic-distortion-final.ipynb- full analysis and modeling notebookApp.py- Streamlit demo appthd_state_classifier.pkl/thd_state_scaler.pkl- the trained model and its feature scalerhistorical_data.csv- exported data used for the app's context chart
| Approach | Result |
|---|---|
| Day-ahead THD forecast (SVR, 7 engineered features) | No signal - negative R² |
| Same-moment THD-state classification (SVM) | 61% accuracy - final model |
| Same-moment THD-state classification (Random Forest) | 58% accuracy |
- How to calculate real power and THD from raw harmonic voltage/current data
- That a "negative result" (no forecastable signal) is still a valid, useful finding - and how to recognize one rather than force a model to fit
- How to reframe a problem (forecasting → classification) when the original framing doesn't hold up
- Feature scaling matters for SVM - an unscaled RBF kernel initially collapsed to predicting a single constant value
- Building an end-to-end pipeline from raw data through to a deployable, interactive demo