← Back to portfolio

Manufacturing Defect Analysis

Python · Pandas · Scikit-learn · Random Forest · Data: Predicting Manufacturing Defects by Rabie El Kharoua, via Kaggle — CC BY 4.0

Brief

A 3,240-row production QC dataset — 16 process metrics per batch (cost, supplier quality, delivery, maintenance, downtime, inventory, safety, energy and additive-manufacturing fields) alongside two defect signals: a continuous DefectRate and a binary DefectStatus pass/fail flag. The question: which of those 16 metrics actually predicts whether a batch gets flagged defective, and how much of that could be caught before shipment using a model built on process data alone.

Production runs
3,240
17 fields each
Flagged defective
84.0%
2,723 of 3,240
Model accuracy
95.1%
held-out 20% split
Maintenance gap
+5.6 hrs
defective vs. passed batches

Cleaning

QA pass across all 17 fields: df.isna().sum() returned zero missing values anywhere, df.duplicated() found zero duplicate rows, and every column loaded as numeric already (12 float64, 5 int64) — no categorical encoding needed before modelling. Every field's observed min/max also matched the ranges in the dataset's own documentation (e.g. quality score 60–100%, supplier quality 80–100%, maintenance hours 0–24/week), so nothing looked mis-scaled or out of bounds. Per that same documentation this is a synthetic dataset generated for educational use, not a live factory feed — worth keeping in mind for how much weight to put on the "why," not just the "what."

Data profile

Descriptive statistics for all 16 process fields. The standard deviations run close to what you'd expect from a uniform spread over each stated range rather than a bell curve — the same pattern visible in Fig 1 below, consistent across the board.

FieldMinMeanMedianMaxStd dev
ProductionVolume100548.5549.0999262.4
ProductionCost ($)5,00012,42312,40519,9934,308
SupplierQuality (%)80.089.889.7100.05.8
DeliveryDelay (days)02.63.051.7
DefectRate0.52.72.75.01.3
QualityScore (%)60.080.180.3100.011.6
MaintenanceHours011.512.0236.9
DowntimePercentage (%)0.02.52.55.01.4
InventoryTurnover2.06.06.010.02.3
StockoutRate0.000.050.050.100.03
WorkerProductivity (%)80.090.090.1100.05.7
SafetyIncidents04.65.092.9
EnergyConsumption (kWh)1,0012,9882,9974,9971,153
EnergyEfficiency0.100.300.300.500.12
AdditiveProcessTime (hrs)1.05.55.410.02.6
AdditiveMaterialCost ($)100300300500116

Exploring the data

The four fields most relevant to quality — defect rate, quality score, maintenance hours and production volume — each spread fairly evenly across their stated range rather than clustering anywhere. The outcome itself doesn't: 84% of batches (2,723 of 3,240) are flagged defective against 16% (517) passed. That skew is by design, not an artifact — the dataset's documentation notes defect cases were deliberately oversampled since they're rare in real production, with non-defect cases added afterward for balance, and it flags the result as imbalanced and worth correcting before modelling. The Random Forest below uses class weighting for that reason rather than training on the raw 84/16 split.

Histograms of defect rate, quality score, maintenance hours and production volume, all roughly uniform
Fig 1. — Defect rate, quality score, maintenance hours and production volume all spread evenly across their range.
Bar chart showing 517 batches passed QC against 2,723 flagged defective
Fig 2. — 517 batches passed QC against 2,723 flagged defective — an 84/16 split.

What correlates with a defect flag

Most of the 16 process metrics show next to no relationship with whether a batch got flagged: supplier quality, downtime, worker productivity, energy efficiency, safety incidents and delivery delay all sit under |r| = 0.05 — functionally noise. Only four stand out: maintenance hours (r = 0.30), defect rate (r = 0.25, expected since it's the same signal in continuous form), production volume (r = 0.13) and quality score (r = -0.20).

Horizontal bar chart of each feature's correlation with DefectStatus, led by MaintenanceHours
Fig 3. — Correlation with the defect flag, sorted. Twelve of sixteen features are effectively noise.

Predicting defects with a model

A Random Forest trained on all 16 fields reached 95.1% accuracy on a held-out 20% test split (648 batches) — 95% precision and 99% recall on "Defective," but only 73% recall on "Passed QC," reflecting the class imbalance: the model is far more confident flagging a bad batch than clearing a good one.

Bar chart of Random Forest feature importances, led by MaintenanceHours, DefectRate, QualityScore and ProductionVolume
Fig 4. — Feature importance mirrors the correlation ranking almost exactly: the same four fields account for roughly three-quarters of the model's total importance.

The maintenance hours pattern

The single strongest signal runs the opposite way you'd expect. Batches that passed QC logged a median 6 maintenance hours; batches flagged defective logged a median 13 — more than double.

Box plot comparing maintenance hours for passed vs defective batches, with defective batches skewing much higher
Fig 5. — Maintenance hours by outcome: defective batches skew noticeably higher, not lower.

More maintenance is coinciding with more defects, not fewer — which reads like reactive servicing recorded around already-troubled batches rather than preventive upkeep paying off. Worth testing directly rather than assumed, by separating scheduled from reactive maintenance in the underlying data.

Where this points