Confusion Matrix for Fault Detection: Calculator & Formula

Share:
Industrial AI
Confusion Matrix for AI Fault Detection: How to Read TP, FP, FN, TN and Calculate Precision, Recall and F1 for Plant Alarms

Your AI model flags a pump bearing fault, but how often is it right and how many faults does it miss? We count TP, FP, FN and TN, work out precision, recall and F1, and use them to pick a sensible alarm threshold.

TP FP FN TN Precision and Recall F1 Calculator Imbalanced Data Threshold Tuning

A fault detection model that reports 93 % accuracy can still drown operators in false alarms. We read the four cells, calculate the metrics that matter and tune the alarm threshold for plant use.

Hello everyone, today we are going to learn how to read and calculate a confusion matrix for AI based fault detection and predictive maintenance alarms in process plants.
confusion matrix
Quick Answer

What is a confusion matrix in AI fault detection?

Confusion matrix is the 2 by 2 table that compares what an AI fault detector predicted with what really happened in the plant. Its four cells are true positives, false positives, false negatives and true negatives, and they give precision = TP / (TP + FP) and recall = TP / (TP + FN). Example: 40 caught faults, 60 false alarms and 10 missed faults give precision 40 % and recall 80 %.

What Is a Confusion Matrix in Fault Detection?

A confusion matrix is a table that counts how many plant faults an AI model caught, how many it missed and how many false alarms it raised. Check it before you let any predictive maintenance model raise a real alarm.

Key Facts
  • Cells: TP caught, FP false alarm, FN missed, TN normal.
  • Precision = TP / (TP + FP), share of alarms that are real.
  • Recall = TP / (TP + FN), share of faults that got an alarm.
  • With rare faults, accuracy flatters a useless model.

The model reads data from a process historian and decides, window by window, whether a fault is present. Maintenance records tell us what really happened, and the confusion matrix puts both side by side.

The same scoring works for a bearing model, a soft sensor quality alert or an AI root cause analysis tool, and we score all of them the same way.

Do You Know?

IBM shows how accuracy fools people: a model with one miss in 100 cases scores 99 % accuracy, yet that miss may matter most.

Advertisement

How to Read a Confusion Matrix?

Read a confusion matrix by matching the row (what actually happened) with the column (what the model predicted). The diagonal cells are correct decisions and the two off diagonal cells are the two kinds of mistakes.

True Positive (TP)

A real fault existed and the model raised an alarm.

Best for: Catch rate
Correct
False Positive (FP)

No fault, but the model alarmed anyway, a nuisance alarm.

Best for: Alarm trust
Error
False Negative (FN)

A real fault existed and the model stayed quiet, the dangerous error.

Best for: Missed risk
Error
True Negative (TN)

Healthy equipment and no alarm, usually the biggest cell.

Best for: Normal behaviour
Correct

Some tools put the actual class in columns instead of rows, so read the axis labels first. A swapped layout silently swaps your FP and FN.

Here precision means the share of alarms that are real. That differs from instrument precision, covered in accuracy vs precision of an instrument.

How Is a Confusion Matrix Built from Plant Data?

You build a confusion matrix by splitting history into fixed time windows, letting the model predict each window and then labelling each window from maintenance records. Every window lands in exactly one of the four cells.

1
Pick the window
Use one fixed length, such as one hour, for scoring and live alarms.
2
Collect signals
Pull vibration, bearing temperature and motor current from the historian.
3
Run the model
A score from 0 to 1 plus a threshold gives alarm or no alarm.
4
Label the truth
Mark fault only when a work order or inspection confirms it.
5
Count the cells
Compare prediction and truth for every window.
confusion matrix fault detection setup from sensors, historian, model and work orders
Diagram by Instrumentation Blog showing how sensor data, an ML score with a 0.70 alarm threshold and maintenance work orders combine into the four confusion matrix counts.

Our worked example in this article uses a pump bearing detector scored on 1000 one hour windows. Work orders confirm 50 fault windows and 950 normal windows, and with a threshold of 0.70 the model gives TP 40, FP 60, FN 10 and TN 890.

It does not matter whether the model is a simple rule or an anomaly detection model. Scoring only looks at what it predicted.

Quick Tip

Label the truth before you look at model output. If the engineer sees the alarm first, labels drift towards agreeing with the model.

How to Calculate Precision and Recall from a Confusion Matrix

Precision is TP divided by all alarms, and recall is TP divided by all real faults. With our counts that gives precision 40 / 100 = 40 % and recall 40 / 50 = 80 %.

Given: TP = 40, FP = 60, FN = 10, TN = 890
Precision = TP / (TP + FP)
= 40 / 100 = 40.0 %
Recall = TP / (TP + FN)
= 40 / 50 = 80.0 %
F1 = 2TP / (2TP + FP + FN)
= 80 / 150 = 53.3 %
Accuracy = (TP + TN) / Total
= 930 / 1000 = 93.0 %

In plant language, eight of ten bearing faults got an alarm, but six of every ten alarms sent a technician to a healthy pump. That confusion matrix result will wear out operator patience fast.

confusion matrix solved example with precision recall F1 and accuracy bars
Diagram by Instrumentation Blog showing the solved pump bearing example with TP 40, FP 60, FN 10 and TN 890, plus precision, recall, F1 and accuracy results.

Enter your own confusion matrix counts from a site trial below. The defaults are the pump example.

Precision, Recall and F1 Calculator
Model performance
Precision 40.0 % | Recall 80.0 % | F1 53.3 % | Accuracy 93.0 %

Compare two models only on the same windows and labels. Our guide to Python in industrial automation shows scripts that turn historian exports into counts.

Advertisement

Why Is Accuracy Misleading for Imbalanced Data?

Accuracy is misleading because faults are rare, so a model that never raises an alarm still scores very high. In our example a dummy model that always says normal gets 950 / 1000 = 95 % accuracy, which beats the real model at 93 %, yet it catches zero faults.

MetricFormulaPump exampleWhat it tells the plant
Accuracy(TP + TN) / Total93.0 %Hides missed faults when faults are rare
PrecisionTP / (TP + FP)40.0 %How far operators can trust an alarm
RecallTP / (TP + FN)80.0 %Share of real faults that got caught
F1 score2TP / (2TP + FP + FN)53.3 %One number that balances both
SpecificityTN / (TN + FP)93.7 %Share of healthy windows left alone
False alarm rateFP / (FP + TN)6.3 %Nuisance load on healthy equipment

Davis and Goadrich of the University of Wisconsin showed in their ICML 2006 paper that precision and recall give a more informative picture than ROC curves on highly skewed data. Plant fault data is exactly that, with healthy hours far outnumbering fault hours.

Imbalance also hurts training, since a model can learn to favour the normal class. Our post on vibration analysis with AI covers how fault examples are gathered.

Myth: A model with 95 % accuracy is ready for the control room.
Fact: With 5 % faults, a model that never alarms also gets 95 %.
Myth: One confusion matrix from the test set is enough forever.
Fact: Process changes, sensor drift and new operating modes shift the counts, so recount on fresh data every month.
Myth: Higher recall is always better.
Fact: Pushing recall up usually pushes false alarms up too.

What Is a Good F1 Score for Fault Detection?

There is no single good F1 score, because the right target depends on how costly a missed fault is compared with a false alarm. F1 is useful for comparing two models on the same data, not as a pass mark on its own.

F1 is the harmonic mean of precision and recall, so it stays low if either is low. IBM notes that some researchers criticise F1 for weighting both equally.

For a trip that shuts down a unit, a false positive is expensive, so aim for precision. For an advisory alarm on a critical compressor, a miss is worse, so lean towards recall.

Tune for high precision when
  • Each alarm triggers a costly action such as a shutdown
  • Operators already face too many alarms per shift
  • The fault develops slowly and gives several chances
Tune for high recall when
  • A missed fault can cause fire, injury or a long outage
  • The equipment has no other protection
  • A check visit is cheap compared with a failure

How to Choose the Alarm Threshold Using a Confusion Matrix

Choose the threshold by recounting the confusion matrix at a few threshold values and picking the one with the lowest total cost of misses and false alarms. Raising the threshold cuts false alarms but also misses more faults.

In our pump example, moving the threshold from 0.70 to 0.85 gave TP 35, FP 20, FN 15 and TN 930. Precision rose to 35 / 55 = 63.6 %, recall fell to 70 %, and F1 rose to 70 / 105 = 66.7 %.

Assumed cost: false alarm Rs 2,000, missed fault Rs 5,00,000
Threshold 0.70: 60 FP and 10 FN
Cost = 60 × 2,000 + 10 × 5,00,000
= Rs 51,20,000
Threshold 0.85: 20 FP and 15 FN
Cost = 20 × 2,000 + 15 × 5,00,000
= Rs 75,40,000

So the higher F1 setting costs more here, because each extra miss outweighs many saved visits. Put your own costs into the confusion matrix, since the answer flips if a false alarm means a shutdown.

A time delay often beats a higher threshold. Like delay timers in ISA 18.2 alarm management, making the score stay high for two or three windows removes one off spikes.

flowchart for tuning AI fault detection alarm threshold with precision and recall
Diagram by Instrumentation Blog showing a flowchart for tuning the alarm threshold of an AI fault detector using precision and recall targets, with a monthly recount loop.
Do You Know?

Yokogawa summarises ISA 18.2 guidance as about 150 alarms per day being very likely acceptable and about 300 per day being the maximum manageable for an operator. A chatty AI model can eat that budget on its own.

Advertisement

Troubleshooting a Fault Detection Model

Most model problems show up as a pattern in the confusion matrix, so ask which cell is out of line. The table gives the usual cause and fix for each pattern.

SymptomMatrix signLikely causeFix
Alarms every start upFP high in transientsTrained on steady running onlyAdd a running state filter
Faults missed at low loadFN high in one modeFew low load fault examplesScore per operating mode
Precision dropped suddenlyFP jump after a dateSensor replaced or recalibratedCheck instrument change records
Counts look too goodNear perfect on testSame days in training and testSplit by date, not at random

A slow transmitter span drift can look exactly like a developing fault to the model. Before blaming the algorithm, check calibration history for the tags feeding it.

Overfitting is the other trap, where the model memorises training days. This explainer on overfitting and underfitting shows the classic sign of a test score far above the live score.

Quick Tip

Log every false alarm with the operator comment. After a month that log explains the FP cell better than any dashboard.

Video and Reference Paper

The StatQuest video below explains the confusion matrix with a medical example. Swap patients for pumps and the logic is identical.

For why precision and recall suit skewed data, read the Davis and Goadrich paper. It also shows why ROC and precision recall curves can rank models differently.

PDF
The Relationship Between Precision Recall and ROC Curves
Davis and Goadrich, University of Wisconsin, ICML 2006

Checklist Before You Trust a Fault Detection Model

Go through this checklist before a model is allowed to raise alarms on a DCS or SCADA screen.

  • Axis labels checked for actual and predicted
  • Truth labels come from work orders, not alarms
  • Test dates differ from training dates
  • Precision and recall reported, not accuracy alone
  • Threshold set with real plant costs
  • Monthly recount has a named owner

Model alarms should follow your SCADA alarm management philosophy, with a priority and an owner. An AI alarm with no operator action belongs in a maintenance report.

For background, read our predictive vs preventive maintenance guide. Noisy signals hurt detection too, and our SNR calculator helps you check them.

Confusion Matrix FAQ

What is a confusion matrix in machine learning with a simple example?

It is a table of correct and wrong predictions split into TP, FP, FN and TN. For example, a pump model with 40 caught faults, 60 false alarms, 10 misses and 890 correct normal windows fills the four cells.

Each cell is a count of windows, and all four add up to the total, which is 1000 here.

What is the difference between precision and recall with an example?

Precision is the share of alarms that were real faults, while recall is the share of real faults that got an alarm. With TP 40, FP 60 and FN 10, precision is 40 % and recall is 80 %.

So the model catches most faults but raises many false alarms.

How to calculate F1 score from a confusion matrix step by step?

Use F1 = 2TP / (2TP + FP + FN), which equals the harmonic mean of precision and recall. With TP 40, FP 60 and FN 10 you get 80 / 150, which is 53.3 %.

Always check that precision and recall alone also make sense for your plant.

Why is accuracy not a good metric for imbalanced fault data?

Faults are rare, so a model that never alarms still scores high accuracy. With 50 faults in 1000 windows, an always normal model reaches 95 % accuracy and zero recall.

That is why precision, recall and F1 are the numbers to report for fault detection.

What is the most common mistake when reading a confusion matrix?

The most common mistake is reading a table with swapped axes, which swaps false positives and false negatives. Some tools put actual values in rows and others put them in columns.

Another frequent mistake is testing on the same days used for training, which makes the counts look far better than live results.

Which threshold is best for a predictive maintenance alarm model?

The best threshold gives the lowest total cost of missed faults plus false alarms, not simply the highest F1. Recount the four cells at a few thresholds, such as 0.70 and 0.85.

Then multiply misses and false alarms by your plant costs and pick the cheaper setting.

Advertisement

Related Articles

External References

What We Learn Today

  • A confusion matrix splits every scored time window into true positive, false positive, false negative or true negative, comparing model alarms with confirmed maintenance findings.
  • Precision shows how far operators can trust an alarm, recall shows how many real faults get caught, and F1 balances the two in a single number.
  • Choose the alarm threshold by recounting the four cells at several settings and comparing the real plant cost of missed faults against false alarm visits.
I hope you like above blog. There is no cost associated in sharing the article in your social media. Thanks for reading!! Happy Learning!!
Sunayana Gadepatil, author at Instrumentation Blog
Author · instrumentationblog.in
Ms. Sunayana Gadepatil is an instrumentation professional, technical writer, and the author behind Instrumentation Blog. With a strong interest in industrial instrumentation, process measurement, and automation, she specializes in simplifying complex technical concepts into clear, practical, and easy to understand insights. Through her articles, Ms. Sunayana shares valuable knowledge on flow, pressure, level, temperature, control systems, and industrial automation for engineers, students, technicians, and industry professionals.
Technically reviewed on

Leave a Reply

Your email address will not be published. Required fields are marked *

Support free learning

Help keep every guide, calculator and tool 100% free

Hosting, software and hours of research go into every article. Even a small contribution helps us publish more in-depth guides for students, technicians and engineers.

  • No paywalls
  • No sign up
  • Free for everyone