Predictive Maintenance: Predicting Machine Failure from Sensor Data with Python

Predictive Maintenance: Predicting Machine Failure from Sensor Data with Python

This updated predictive-maintenance tutorial uses the 10,000-row AI4I 2020 benchmark to predict whether a machine fails. Failures account for only 3.4% of rows, so average precision and a prevalence baseline are more informative than accuracy alone.

Machine failure prevalence and torque overview

Leakage-safe features and pipeline

The model excludes identifiers, Failure Type, Target, and Machine failure from its features. Keeping failure-derived columns would reveal the answer. The remaining sensor values are standardized and machine type is one-hot encoded inside a training-only pipeline.

model = Pipeline([
    ('preprocessor', preprocessor),
    ('classifier', LogisticRegression(
        class_weight='balanced', max_iter=2000, random_state=42
    )),
])

Verified holdout results

On the stratified holdout, balanced accuracy was 0.990, F1 was 0.990, ROC AUC was 0.982, and average precision was 0.981. The prevalence-only baseline average precision was 0.034.

Machine failure ROC and precision-recall curves

Machine failure confusion matrix

Permutation importance

The strongest holdout permutation feature was PWF, measured by decrease in average precision. Correlated sensors can divide importance, and the ranking does not prove a failure mechanism.

Machine failure permutation importance

Limitations

AI4I is synthetic. These results do not establish performance on a production line, another machine family, or a future operating regime. The random holdout does not test drift or maintenance interventions. Balanced weights and a 0.5 threshold create one false-alarm trade-off; deployment needs cost-based threshold selection, temporal validation, probability calibration, and monitoring.

Florian Follonier

Florian Follonier · Cloud Solution Architect at Microsoft

Florian Follonier (PhD) is a Cloud Solution Architect at Microsoft based in Zurich and the author of relataly.com, writing hands-on tutorials on machine learning, Python, RAG, and AI agents.

3 Commentsarchived from the original site

  • Alex Berns
    Hi Florian, I liked your article, however I don't see where you have actually done the prediction testing with your actual test data. From my understanding the cross validation function automatically splits your data into train and validations portions so you can do the hyperparameter tuning. Once you are happy with the results of the crossvalidation you can finally check it one more time with your test data. regards Alex
  • Alex Berns
    Hey Florian, sorry, I see that you have done the testing further above, my bad. I believe that your model is still kinda condition based. I don't think your model is telling the user ahead of time when a potential failure will occur. Regards Alex
  • shravya
    could you suggest any ideas on areospace industry problems