Modern manufacturing depends heavily on reliable industrial machinery. Unexpected machine failures can lead to production downtime, costly repairs, and inefficient maintenance. Instead of waiting for equipment to fail or relying entirely on fixed maintenance schedules, predictive maintenance uses machine data to identify patterns that may indicate an upcoming failure.
There are several ways machine learning can contribute to manufacturing, from analyzing production patterns to monitoring equipment through sensor data. In particular, sensor measurements can reveal relationships and operating conditions associated with machine failures.
In this project, I explore predictive maintenance using the AI4I 2020 dataset. I compare three classification approaches: Logistic Regression, Random Forest, and XGBoost to predict whether a machine will fail based on sensor readings and product type.
The main objective is not only to find the best-performing model, but also to examine the trade-off between missing actual failures and generating unnecessary alerts, and determine which model is more suitable depending on the priorities of the application.
Manufacturing equipment failures are costly and often preventable.
This project uses machine sensor data to predict whether a machine will experience a failure. The available measurements include:
- Air temperature
- Process temperature
- Rotational speed
- Torque
- Tool wear
- Product type
The project follows a simple machine learning workflow:
- Data inspection and sanity checking
- Exploratory data analysis
- Data preprocessing
- Train/test splitting and cross-validation
- Logistic Regression baseline
- Random Forest
- XGBoost
- Model evaluation and comparison, including error and performance analysis of each model's precision-recall trade-off
Because machine failures represent only ~3.4% of the observations, particular attention is given to the severe class imbalance and to metrics that are more informative than accuracy.
The project uses the AI4I 2020 Predictive Maintenance Dataset from the UCI Machine Learning Repository.
The dataset contains:
- 10,000 observations
- 14 columns
- Sensor measurements
- Product type
- Machine failure labels
| Feature | Description |
|---|---|
Type |
Product quality/type |
Air temperature [K] |
Air temperature |
Process temperature [K] |
Process temperature |
Rotational speed [rpm] |
Machine rotational speed |
Torque [Nm] |
Torque generated by the machine |
Tool wear [min] |
Tool usage time |
Machine failure |
Overall binary failure target |
The dataset also contains five individual failure-mode indicators:
TWF- Tool Wear FailureHDF- Heat Dissipation FailurePWF- Power FailureOSF- Overstrain FailureRNF- Random Failure
These individual failure-mode labels were treated as target-leaking information and were therefore excluded from the predictive features.
Before modeling, the dataset was inspected to understand the distributions of the numerical features, relationships between variables, and patterns associated with machine failures.
The numerical features show noticeably different distributions. Air temperature and process temperature are relatively concentrated within narrow ranges, while rotational speed is right-skewed with a long upper tail. Torque follows a roughly bell-shaped distribution, whereas tool wear is distributed more uniformly across its observed range.
Comparing the numerical features between failed and non-failed machines reveals several noticeable differences. Failed machines tend to operate at higher temperatures, lower rotational speeds, and higher torque levels. Tool wear is also generally higher among machines that experience failure.
Machines do not fail randomly across all operating conditions. The analysis revealed two distinct operating zones where failures tend to cluster:
- Low-speed / high-torque
- High-speed / low-torque
These regions are associated with different failure mechanisms.
An interesting finding was that the lowest-quality product variant (Type L) had the highest observed failure rate rather than the highest-quality variant.
The complete exploratory analysis is available in
01_inspection_and_EDA.ipynb.
Before training the models, the dataset was prepared through the following steps:
- Checked for missing values and duplicate observations
- Verified feature types and data integrity
- Removed identifier columns
- Removed the individual failure-mode flags to prevent target leakage
- One-hot encoded the categorical
Typefeature - Performed a stratified train/test split
- Used 10-fold stratified cross-validation during model evaluation
Stratification was used to preserve the minority failure class across the training and test sets.
Three classification approaches were evaluated.
Logistic Regression was used as an interpretable baseline.
It provides a simple reference point for evaluating whether more complex ensemble models provide a meaningful improvement.
Random Forest was selected as a bagging-based ensemble method.
It can capture nonlinear relationships and interactions between the sensor measurements while remaining relatively robust to noisy features.
XGBoost was selected as a boosting-based ensemble method.
It builds an ensemble of decision trees sequentially, allowing later trees to focus on errors made by previous trees.
The goal was to compare these models under the same classification task and determine how their performance differs when predicting the minority failure class.
Because the dataset is highly imbalanced, accuracy alone would be misleading.
The following metrics were used:
Precision measures the proportion of predicted failures that are actually failures.
A higher precision means fewer false alarms.
Recall measures the proportion of actual machine failures that the model successfully detects.
A higher recall means fewer missed failures.
F1-score combines precision and recall into a single metric and is useful when both false alarms and missed failures matter.
ROC-AUC measures how well the model separates failure and non-failure observations across different classification thresholds.
PR-AUC summarizes the precision-recall trade-off across classification thresholds.
Because the failure class is highly imbalanced, PR-AUC was given particular importance when comparing the models.
The final models were evaluated using precision, recall, F1-score, ROC-AUC, and PR-AUC.
| Model | Precision | Recall | F1 | ROC-AUC | PR-AUC |
|---|---|---|---|---|---|
| Logistic Regression | 0.144 | 0.824 | 0.245 | 0.907 | 0.396 |
| Random Forest | 0.750 | 0.706 | 0.727 | 0.970 | 0.781 |
| XGBoost | 0.675 | 0.794 | 0.730 | 0.963 | 0.812 |
The bar chart provides a direct comparison of the models across precision, recall, F1-score, and PR-AUC, making the differences in their performance easy to see at a glance.
The precision-recall curves show the trade-off between detecting more machine failures and generating additional false alarms across different classification thresholds. XGBoost maintains the strongest overall precision-recall performance, reflected by its highest PR-AUC.
The ROC curves show how effectively each model separates failing machines from non-failing machines across different thresholds. Random Forest achieves the highest ROC-AUC (0.970), followed closely by XGBoost (0.963).
The results reveal an important trade-off between detecting failures and avoiding false alarms.
Logistic Regression achieved the highest recall (0.824), meaning it detected the largest proportion of actual machine failures, making it useful when detecting as many failures as possible is the primary concern.
However, its precision was only 0.144.
This means that approximately 86% of its failure alerts were false alarms at the evaluated operating point.
Despite its high recall, Logistic Regression provides a poor precision-recall balance for this dataset, and this comes at the cost of a large number of false alarms.
Random Forest achieved the highest precision (0.750) and the highest ROC-AUC (0.970).
This makes it a strong alternative when minimizing false alarms is particularly important.
Its recall of 0.706, however, means that it misses more actual failures than XGBoost and Logistic Regression.
XGBoost achieved the highest PR-AUC (0.812) and the highest F1-score (0.730), indicating the strongest overall precision-recall trade-off among the evaluated models.
It also achieved a recall of 0.794, while maintaining substantially better precision than Logistic Regression.
This gives XGBoost the best overall balance between detecting failures and limiting false alarms among the three evaluated models, making it the recommended choice when both failure detection and false-alarm reduction are important.
In a real predictive-maintenance system, the final model should depend on the relative operational cost of missed failures versus unnecessary maintenance alerts:
- XGBoost - recommended when both failure detection and false-alarm reduction matter, given its best PR-AUC (0.812) and F1-score (0.730)
- Random Forest - recommended when minimizing false alarms is the higher priority, given its highest precision (0.750)
- Logistic Regression - useful when detecting as many failures as possible is the primary concern, given its highest recall (0.824), but its low precision means a large number of false alarms
π
βββ π assets
β βββ π plots
β βββ πΌοΈ boxplot_Air_temperature_by_failure.png
β βββ πΌοΈ boxplot_Process_temperature_by_failure.png
β βββ πΌοΈ boxplot_Rotational_speed_by_failure.png
β βββ πΌοΈ boxplot_Tool_wear_by_failure.png
β βββ πΌοΈ boxplot_Torque_by_failure.png
β βββ πΌοΈ boxplots_by_failure_combined.png
β βββ πΌοΈ correlation_matrix.png
β βββ πΌοΈ distribution_Air_temperature.png
β βββ πΌοΈ distribution_Process_temperature.png
β βββ πΌοΈ distribution_Rotational_speed.png
β βββ πΌοΈ distribution_Tool_wear.png
β βββ πΌοΈ distribution_Torque.png
β βββ πΌοΈ failure_rate_by_type.png
β βββ πΌοΈ numeric_distributions_combined.png
β βββ πΌοΈ scatter_speed_vs_torque_by_failure.png
β βββ πΌοΈ scatter_speed_vs_torque_by_failure_mode.png
β βββ πΌοΈ Target_Distribution.png
βββ π config
β βββ π constants.py
β βββ π paths.py
βββ π Dataset
β βββ π processed
β β βββ π x_test.csv
β β βββ π x_train.csv
β β βββ π y_test.csv
β β βββ π y_train.csv
β βββ π ai4i2020.csv
βββ π notebooks
β βββ π 01_inspection_and_EDA.ipynb
β βββ π 02_preprocessing.ipynb
β βββ π 03_logistic_regression.ipynb
β βββ π 04_random_forest.ipynb
β βββ π 05_xgboost.ipynb
β βββ π 06_comparison.ipynb
βββ π results
β βββ π logistic_regression
β β βββ π charts
β β β βββ πΌοΈ coefficients.png
β β β βββ πΌοΈ confusion_matrix.png
β β β βββ πΌοΈ pr_curve.png
β β β βββ πΌοΈ roc_curve.png
β β βββ π metrics.csv
β β βββ π model.pkl
β βββ π random_forest
β β βββ π charts
β β β βββ πΌοΈ confusion_matrix.png
β β β βββ πΌοΈ feature_importance.png
β β β βββ πΌοΈ pr_curve.png
β β β βββ πΌοΈ roc_curve.png
β β βββ π metrics.csv
β β βββ π model.pkl
β βββ π xgboost
β β βββ π charts
β β β βββ πΌοΈ confusion_matrix.png
β β β βββ πΌοΈ feature_importance.png
β β β βββ πΌοΈ pr_curve.png
β β β βββ πΌοΈ roc_curve.png
β β βββ π metrics.csv
β β βββ π model.pkl
β βββ πΌοΈ model_comparison_bars.png
β βββ πΌοΈ model_comparison_pr_curves.png
β βββ πΌοΈ model_comparison_roc_curves.png
βββ βοΈ LICENSE
βββ π README.md
βββ π requirements.txt
Generated using directory-tree-printer
Clone the repository:
git clone https://github.com/AmirmasoudCS/Predictive-Maintenance.git
cd Predictive-MaintenanceCreate a virtual environment:
python -m venv .venvActivate the virtual environment:
Windows:
.venv\Scripts\activateLinux/macOS:
source .venv/bin/activateInstall the requirements:
pip install -r requirements.txtThe analysis workflow is organized into six notebooks that should be run in order:
01_inspection_and_EDA.ipynb- dataset inspection and exploratory data analysis02_preprocessing.ipynb- data preparation and train/test splitting03_logistic_regression.ipynb- Logistic Regression training and evaluation04_random_forest.ipynb- Random Forest training and evaluation05_xgboost.ipynb- XGBoost training and evaluation06_comparison.ipynb- final model comparison and visualization
This project has several limitations:
- The analysis uses a relatively small dataset of 10,000 observations.
- Machine failures represent only approximately 3.4% of the observations, making the classification problem highly imbalanced.
- The dataset represents a specific simulated manufacturing environment and may not generalize directly to real-world industrial systems.
- The models are evaluated on the AI4I 2020 dataset and should not be assumed to perform equally well on different machines, factories, or operating environments.
- The evaluated precision and recall values depend on the selected classification threshold.
- The dataset contains a limited number of sensor measurements and failure categories.
- The current models should therefore be viewed as an experimental comparison rather than a production-ready predictive-maintenance system.
The current project focuses on comparing three classification approaches under the same dataset and evaluation framework.
Potential extensions include:
- Threshold optimization based on different false-alarm and failure costs
- Hyperparameter tuning
- More detailed failure-mode classification
- Feature engineering from sensor measurements
- Additional ensemble and boosting models
- Cost-sensitive learning
- Calibration of predicted failure probabilities
- Evaluation on additional predictive-maintenance datasets
- Testing the models on real-world industrial sensor data
This project investigated the use of machine learning for predictive maintenance by comparing Logistic Regression, Random Forest, and XGBoost on the AI4I 2020 Predictive Maintenance Dataset.
Model performance depended strongly on which aspect of failure prediction was prioritized, and the Model Analysis and Recommendation section above breaks down that trade-off in detail for each of the three models.
These results highlight an important aspect of predictive maintenance: the best model is not necessarily the one with the highest accuracy, but the one whose error profile best matches the operational costs of the application.
This project is licensed under the MIT License.






