An end-to-end Machine Learning pipeline designed for Predictive Maintenance of IoT sensor systems. It generates robust time-series features, handles extreme class imbalances (<1% failure rate), incorporates SHAP-based local explainability, and deploys the final XGBoost model via a high-performance Flask API.
feature_engineering.py: Generates foundational time-series signals including Rolling Means, Exponential Moving Averages (EMA), and standard deviations over 1h, 6h, and 12h windows. Serializes the prepared data intoengineered_features.joblib.model_selection.py: Handles extreme class imbalance usingimblearnSMOTE andscale_pos_weight. Maximizes Precision-Recall AUC (PR-AUC) rather than pure accuracy to avoid false alarms. Employs Optuna to aggressively search for the bestXGBoosthyperparameters, saving the final champion model toxgboost_production_model.joblib.model_explainability.py: Provides both visual and analytical ML explainability. It isolates positive failure predictions from the model and draws SHAP (SHapley Additive exPlanations) Waterfall plots and Global summaries to inform the Maintenance Engineer exactly why a failure alert triggered.api.py: A low-latency real-time inference Flask Server. Accepting JSON payload histories of sensor metrics, it calculates the rolling features dynamically in memory and evaluates them using the XGBoost production model within<50msruntime, outputting real-time Risk indicators.test_api_client.py: A helper automation script simulating the physical IoT network, sending payloads to test network round-trip latency againstapi.py.
Ensure you are using your target Python environment (e.g., Python 3.13) and install the entire dependency stack:
pip install pandas numpy scikit-learn xgboost optuna imbalanced-learn shap matplotlib flask requests joblibTo correctly train the model from scratch and deploy the inference API, strictly follow this execution order:
- Feature Engineering:
python feature_engineering.py
- Model Selection & Tuning:
python model_selection.py
- Model Explainability (Optional):
(Produces
.pngSHAP visualizations and terminal logic paths)python model_explainability.py
- Boot up Inference API:
python api.py
- Test Latency / Network Simulation (Open in a new terminal):
python test_api_client.py