Divya499/ReliabilityPulse
1
1**PROJECT INSTRUCTION FILE**2 3**Predictive Maintenance**4 5Machine Learning Project | Classification + Anomaly Detection | Manufacturing Domain6 7|<p>Dataset</p><p>**AI4I 2020 Predictive Maintenance (Kaggle)**</p>|<p>Rows</p><p>**10,000 records**</p>|<p>Difficulty</p><p>**Easy**</p>|<p>Target Metric</p><p>**F1-Score (critical) 88–95%**</p>|8| :-: | :-: | :-: | :-: |9 10 11# **1. Project Overview**12Predictive maintenance (PdM) uses sensor data and machine telemetry to predict equipment failures before they occur, allowing scheduled maintenance instead of reactive repairs. This reduces unplanned downtime, extends machine lifespan, and lowers maintenance costs significantly.13 14|**Real-World Use Case**|15| :- |16|Manufacturing companies like Bosch, Siemens, and Tata Steel deploy PdM systems on CNC machines, turbines, and assembly lines. A single hour of unplanned downtime on an auto assembly line can cost ₹1–5 crore. PdM models monitoring temperature, torque, and vibration can detect anomalies 24–72 hours before mechanical failure.|17 18# **2. Dataset Details**19**Source**20 21- Name: AI4I 2020 Predictive Maintenance Dataset22- Platform: Kaggle — https://www.kaggle.com/datasets/stephanmatzka/predictive-maintenance-dataset-ai4i-202023- Format: CSV — single file24- License: Public / Open Use25 26**Dataset Statistics**27 28|**Property**|**Value**|29| :- | :- |30|Total Rows|10,000 machine readings|31|Total Columns|14 features|32|Target Column|Machine failure (0 = no failure, 1 = failure)|33|Class Distribution|~96.5% no failure, ~3.5% failure (highly imbalanced)|34|Missing Values|None|35|Data Types|Mix of numeric and categorical|36 37**Key Features**38 39- UDI — unique identifier (drop before modeling)40- Product ID — product serial with quality type prefix (L/M/H) — extract quality type41- Type — product quality: L (Low), M (Medium), H (High) — encode as ordinal42- Air temperature [K] — ambient air temperature in Kelvin43- Process temperature [K] — machine process temperature in Kelvin44- Rotational speed [rpm] — motor rotational speed45- Torque [Nm] — applied torque46- Tool wear [min] — cumulative tool usage time in minutes47- Machine failure — **TARGET**: 1 if any failure occurred48- TWF — Tool Wear Failure (sub-label)49- HDF — Heat Dissipation Failure (sub-label)50- PWF — Power Failure (sub-label)51- OSF — Overstrain Failure (sub-label)52- RNF — Random Failure (sub-label)53 54|**Multi-Label Insight**|55| :- |56|The dataset has 5 specific failure mode sub-labels (TWF, HDF, PWF, OSF, RNF) in addition to the overall Machine failure target. For the main model, predict Machine failure. For advanced analysis, build separate models for each failure mode or use multi-label classification.|57 58# **3. Step-by-Step Workflow**59## **Step 1 — Environment Setup**60Install the required Python libraries before starting:61 62|pip install pandas numpy scikit-learn xgboost imbalanced-learn matplotlib seaborn|63| :- |64 65## **Step 2 — Load & Explore Data (EDA)**661. Load CSV: df = pd.read\_csv('ai4i2020.csv')672. Check shape, dtypes, nulls — confirm no missing values683. Plot failure distribution — confirm ~3.5% failure rate (highly imbalanced)694. Plot failure rate by product Type (L/M/H)705. Plot distributions of temperature, torque, rotational speed, tool wear716. Box plots: compare sensor readings for failure vs non-failure cases727. Correlation heatmap for numeric features738. Plot failure count by each sub-label (TWF, HDF, PWF, OSF, RNF)74 75|**Key EDA Finding**|76| :- |77|Tool wear > 200 min combined with high torque is the strongest predictor of failure. Heat Dissipation Failures (HDF) occur when temperature difference between process and air temperature is < 8.6 K. Power Failures (PWF) occur when power (torque × rotational speed) falls outside 3500–9000 W range. Engineering these derived features significantly improves model performance.|78 79## **Step 3 — Feature Engineering**80Engineer domain-informed features before preprocessing:81 821. temp\_diff = df['Process temperature [K]'] - df['Air temperature [K]'] (HDF signal)832. power = df['Torque [Nm]'] * (df['Rotational speed [rpm]'] * 2 * 3.14159 / 60) (PWF signal — power in Watts)843. tool\_wear\_torque = df['Tool wear [min]'] * df['Torque [Nm]'] (OSF/TWF signal)854. Extract quality type: df['Quality'] = df['Product ID'].str[0] → L=0, M=1, H=2 (ordinal encoding)865. Drop: UDI, Product ID, TWF, HDF, PWF, OSF, RNF (sub-labels — data leakage for main target)87 88## **Step 4 — Data Preprocessing**891. Encode Type column: map({'L': 0, 'M': 1, 'H': 2}) — ordinal makes sense here (quality order)902. Scale numeric features (Air temp, Process temp, RPM, Torque, Tool wear, engineered features) using StandardScaler — required for SVM and Isolation Forest913. Split: X\_train, X\_test, y\_train, y\_test = train\_test\_split(X, y, test\_size=0.2, random\_state=42, stratify=y)924. Confirm class distribution in train and test sets93 94## **Step 5 — Handle Class Imbalance (Critical)**95With only ~3.5% failure rate, imbalance handling is essential:96 97- **Option A — SMOTE**: from imblearn.over\_sampling import SMOTE — generate synthetic failure samples (apply only on training data, AFTER split)98- **Option B — class\_weight='balanced'**: automatic weight adjustment in sklearn models99- **Option C — Threshold tuning**: lower classification threshold from 0.5 to 0.3 to maximize recall on failures100- **Recommended**: Use SMOTE for XGBoost + threshold tuning for final deployment101 102|**Critical: Recall is the Priority Metric**|103| :- |104|In predictive maintenance, a missed failure (False Negative) causes unplanned downtime and equipment damage. A false alarm (False Positive) triggers an unnecessary inspection — costly but not catastrophic. Always optimize for Recall > 85% on the failure class. Use F1-Score as the primary tuning metric, never accuracy.|105 106## **Step 6 — Model Building**107 108|**Model**|**When to Use**|**Expected F1 (Failure)**|109| :- | :- | :- |110|Logistic Regression|Baseline, fast, interpretable|55 – 65%|111|Random Forest|Handles class imbalance well with balanced weights|75 – 82%|112|XGBoost|Best overall performer for this dataset|80 – 88%|113|SVM (RBF kernel)|Works well on small-medium sensor datasets|72 – 80%|114|Isolation Forest|Anomaly detection — unsupervised baseline|60 – 70% (approx.)|115 116Recommended order: Isolation Forest for anomaly baseline → Logistic Regression → SVM → XGBoost as final classifier.117 118**Isolation Forest usage (anomaly detection approach):**119```python120from sklearn.ensemble import IsolationForest121iso = IsolationForest(contamination=0.035, random_state=42)122iso.fit(X_train)123preds = iso.predict(X_test) # -1 = anomaly (potential failure), 1 = normal124```125 126## **Step 7 — Hyperparameter Tuning**1271. Use GridSearchCV or RandomizedSearchCV with cv=51282. XGBoost key params: n\_estimators (100–400), max\_depth (3–7), learning\_rate (0.01–0.2), scale\_pos\_weight (set to ratio of negatives/positives ≈ 27 for imbalanced data)1293. SVM key params: C (0.1–100), gamma ('scale', 'auto', 0.001–0.1), kernel ('rbf', 'poly')1304. Use scoring='f1' as primary metric — not 'accuracy'131 132## **Step 8 — Evaluate the Model**133 134|**Metric**|**What it Measures**|**Target Value**|135| :- | :- | :- |136|Accuracy|Overall correct predictions|> 96% (easy due to imbalance — not reliable)|137|Precision (Failure)|Of predicted failures, how many were actual|> 75%|138|Recall (Failure)|Of actual failures, how many did we catch|> 85%|139|F1-Score (Failure)|Harmonic mean — primary metric|88 – 95%|140|AUC-ROC|Separation between classes|> 0.90|141|Confusion Matrix|Full TP/TN/FP/FN breakdown|Always visualize|142 143# **4. Feature Importance**144 145|**Rank**|**Feature**|**Importance Level**|**Business Insight**|146| :- | :- | :- | :- |147|1|tool\_wear\_torque (engineered)|Very High|Combined stress = primary failure driver|148|2|Tool wear [min]|Very High|Aging tools fail more — schedule replacements|149|3|Torque [Nm]|High|Overload indicator|150|4|temp\_diff (engineered)|High|Low temp diff = heat dissipation failure risk|151|5|power (engineered)|High|Out-of-range power = motor failure|152|6|Rotational speed [rpm]|Medium-High|Speed anomalies precede mechanical failures|153|7|Process temperature [K]|Medium|High process temp accelerates wear|154|8|Type (Quality)|Medium|Low-quality products run hotter, fail more|155|9|Air temperature [K]|Low-Medium|Ambient temp affects heat dissipation|156 157# **5. Project Structure**158 159```16004_predictive_maintenance/161├── data/162│ ├── raw/ai4i2020.csv163│ └── processed/features.csv164├── models/165│ ├── xgboost_model.pkl166│ └── isolation_forest.pkl167├── pipeline/168│ ├── 01_eda.py169│ ├── 02_feature_engineering.py170│ ├── 03_preprocessing.py171│ ├── 04_model_training.py172│ └── 05_evaluation.py173├── outputs/174│ ├── confusion_matrix.png175│ ├── roc_curve.png176│ ├── feature_importance.png177│ └── anomaly_scores.png178├── app.py179├── path_utils.py180└── README.md181```182 183**Pipeline File Descriptions:**184 185| File | Purpose |186| :- | :- |187| 01\_eda.py | Load data, plot distributions, failure rates, correlations, sub-label analysis |188| 02\_feature\_engineering.py | Create temp\_diff, power, tool\_wear\_torque, encode Type, drop leakage columns |189| 03\_preprocessing.py | Scale features, apply SMOTE on train set, save processed arrays |190| 04\_model\_training.py | Train Isolation Forest, Logistic Regression, SVM, XGBoost — save models |191| 05\_evaluation.py | Generate all metrics, confusion matrix, ROC curve, feature importance plots |192 193# **6. Expected Results Summary**194 195|**Metric**|**Baseline (Logistic Reg.)**|**Best Model (XGBoost + SMOTE)**|196| :- | :- | :- |197|Accuracy|> 96%|> 97%|198|Precision (Failure)|55 – 65%|75 – 85%|199|Recall (Failure)|60 – 70%|85 – 92%|200|F1-Score (Failure)|57 – 67%|80 – 88%|201|AUC-ROC|0.82 – 0.87|0.91 – 0.95|202 203# **7. Common Mistakes to Avoid**204- Using accuracy as the primary metric — with 96.5% no-failure, predicting all 'no failure' gives 96.5% accuracy but is completely useless205- Including sub-label columns (TWF, HDF, PWF, OSF, RNF) as features — they directly encode failure causes and cause severe data leakage206- Applying SMOTE before the train/test split — synthetic samples from test data leak into training207- Forgetting scale\_pos\_weight in XGBoost — set to ~27 (ratio of negative to positive) for imbalanced data208- Not engineering derived features (temp\_diff, power, tool\_wear\_torque) — raw features alone miss key failure physics209- Dropping Type column — product quality type has meaningful impact on failure rate210 211# **8. Recommended Tools & Libraries**212 213|**Library**|**Purpose**|214| :- | :- |215|pandas|Data loading, feature engineering|216|numpy|Numerical operations, power calculation|217|scikit-learn|Preprocessing, SVM, Isolation Forest, metrics|218|xgboost|Best classifier — handles imbalance with scale\_pos\_weight|219|imbalanced-learn|SMOTE for oversampling minority failure class|220|matplotlib / seaborn|EDA plots, confusion matrix heatmap, ROC curve|221|joblib|Save and load trained models|222 223# **9. Project Deliverables Checklist**224- pipeline/ folder with 5 modular .py files (EDA → feature engineering → preprocessing → training → evaluation)225- Trained XGBoost model + Isolation Forest saved as .pkl using joblib226- Classification Report + Confusion Matrix visualization227- ROC Curve comparing all models228- Feature Importance bar chart (top 9 features including engineered)229- Anomaly score distribution plot (Isolation Forest)230- README.md explaining failure modes and prediction threshold choice231- Streamlit app (app.py) for live failure risk prediction — user inputs sensor readings (temp, RPM, torque, tool wear, quality type), model returns failure probability with risk level (Low/Medium/High/Critical), top contributing factors, and recommended maintenance action232 233Predictive Maintenance | ML Project Instruction File | Classification + Anomaly Detection Project #4234 