Why accuracy lies here
- Lesson 29
- 30 min
- Module 7: Evaluate with few labels
Where we are
Our comparison table counts the planted anomalies that each detector finds. Omar asks for one number, and Tomás proposes accuracy. In this lesson, we see why accuracy says almost nothing when frauds are rare, and we use precision at k, recall and the time to detection instead. A smoke alarm that never rings shows the problem.
Lesson 29 of 38 · Anomaly Detection: Find What Is UnusualWhy accuracy lies here
1. What "unusual" means
2. Simple baselines that are hard to beat
3. Statistical process control and distribution drift
4. Distance, density and isolation
5. Anomalies in time
6. Deep and embedding methods
7. Evaluate with few labels
8. Learn from the reviewers
9. Run a detector in production
What you will be able to do
Measure a fraud detector by what the review team sees and misses each day. When frauds are rare, the usual score, accuracy, is about the same for a useless detector and a perfect one.
NotebookDo this lesson in a notebook
The notebook has every code cell, task and check of this lesson. It computes accuracy for a detector that does nothing, precision at k for five values of k, the precision–recall curve and the time to detection. Its setup cells install pandas and scikit-learn and download the course data (about 7 MB), so it needs no local setup. About 30 minutes.
Colab and Kaggle open your own copy. They need a free Google or Kaggle account. Stuck? See the solution notebook.
Before you startWhat to know and to have ready
What you need to know
- Precision, recall and the precision–recall curve for a classifier, from The precision–recall trade-off in Machine Learning: From Problem to Reliable Model. This lesson uses the same ideas for anomaly scores, and does not repeat them in depth.
- The planted anomalies and the course’s split (learn on June to August 2025, score September to November 2025), from Meet the Larkfield data.
- Isolation Forest on the behaviour features, from Isolation Forest and Features decide more than the algorithm.
What you need on your computer
Nothing: the notebook runs in Colab or Kaggle. To run it on your own computer, use Python 3.12 in Jupyter. The setup cell installs the packages that it needs.
The notebook was tested with Python 3.12.13 in Jupyter on macOS, with pandas 3.1.1 and scikit-learn 1.9.1. Colab, Kaggle and Windows were not tested.
A smoke alarm that never rings
Section titled “A smoke alarm that never rings”Think of a smoke alarm in a kitchen. On almost every day of the year there is no fire. Now imagine a broken alarm that never rings. Each evening, you ask it: “Was there a fire today?” It says “no”, and it is right. It is right on 364 days of the year, so it is right 99.7% of the time.
On the one day with a fire, it is wrong, and that is the only day that matters. Nobody would call this alarm “99.7% good”. We judge an alarm by two other questions. When it rings, is there really a fire? And when there is a fire, does it ring, and in time?
A fraud detector is a smoke alarm for payments. Almost every payment is honest, and the few frauds are the only payments that cost money. So the question “how often is the detector right?” has the same trap as the broken alarm.
The problem: Tomás proposes accuracy
Section titled “The problem: Tomás proposes accuracy”Omar, the product manager, wants one number for each detector in the comparison table. “Which one is best? Give me a single score.” Tomás, the AI engineer, proposes the most common score in machine learning. Accuracy is the share of right answers among all answers: right alerts plus right “no alert” answers, divided by all payments.
A tiny example: 1,000 payments and one fraud
Section titled “A tiny example: 1,000 payments and one fraud”Here are 1,000 payments, and one of them is a fraud. This is a constructed example. The “detector” is a line of code that never alerts:
is_fraud = np.zeros(1000, dtype=bool)is_fraud[417] = True # 1 fraud in 1,000 paymentssays_nothing = np.zeros(1000, dtype=bool) # a "detector" that never alertsaccuracy = (says_nothing == is_fraud).mean()print(f"accuracy of 'nothing is fraud': {accuracy:.3f}; frauds found: {int((says_nothing & is_fraud).sum())} of 1")The output:
accuracy of 'nothing is fraud': 0.999; frauds found: 0 of 1The detector does nothing, and its accuracy is 99.9%. It is right about the 999 honest payments, and accuracy counts each of those as much as the one fraud. So accuracy is high because the fraud is rare, not because the detector is good.
On Larkfield’s payments
Section titled “On Larkfield’s payments”Larkfield’s evaluation months have 169,803 payments, and 2,817 of them are planted frauds. That is 1.66%, about 31 a day. Frauds are far more common here than in a real shop, so that each day has enough cases to learn from. The dataset card says so. Even with this many frauds, “nothing is fraud” is right on 98.34% of the payments.
Priya, Larkfield’s data scientist, computes accuracy for the detectors of the comparison table. Each one sends its 50 highest scores of each day to Sofia’s team, because the team can review 50 alerts a day. The function accuracy_at_budget() in the notebook marks those 50 a day as “fraud” and every other payment as “honest”:
print(f"nothing is fraud: accuracy {(~planted).mean():.4f}")print(f"Isolation Forest, 50/day: accuracy {accuracy_at_budget(score):.4f}")print(f"perfect detector, 50/day: accuracy {accuracy_at_budget(planted + 0.0):.4f}")The output:
nothing is fraud: accuracy 0.9834Isolation Forest, 50/day: accuracy 0.9834perfect detector, 50/day: accuracy 0.9857A perfect detector is a test detector that knows the answer key, so it shows the best result that any detector can reach. All three accuracies are within a quarter of a point of each other. The two charts put accuracy beside a second score, precision at 50 a day: the share of the 50 daily alerts that are real frauds. You meet it properly below.
Text description of the chart
A column chart with four columns on an axis from 0 to 100%. “Nothing is fraud” has an accuracy of 98.3%. MAD per customer has 95.9%. Isolation Forest has 98.3%. The perfect detector, which knows the answers, has 98.6%. All four columns reach almost the top of the chart, and the eye cannot see a difference between them.
Text description of the chart
A column chart with the same four detectors on an axis from 0 to 100%. “Nothing is fraud” sends no alerts, so it has no column. MAD per customer has 4.8%: fewer than 1 alert in 20 is real. Isolation Forest has 50.0%: half of its alerts are real frauds. The perfect detector has 54.2%. Here the columns are very different, and Isolation Forest is close to the perfect detector.
Accuracy even puts MAD per customer (95.92%) below the detector that does nothing. MAD’s false alerts are wrong answers, and doing nothing makes no false alerts at all. By accuracy, the best plan for Larkfield is to switch the detectors off.
Why even the perfect detector is not 100% accurate
Section titled “Why even the perfect detector is not 100% accurate”The perfect detector knows every answer, so why does it reach only 98.57%? Because it must also obey the budget of 50 alerts a day. On a day with 70 planted frauds, it can send only 50, and the other 20 count as wrong. On a day with 20 planted frauds, it still sends 50 alerts, so 30 of them are honest payments. On many days, fewer than 50 frauds exist. For this reason, its precision at 50 a day is 0.542, not 1. Keep this ceiling in mind: no detector can beat it at this budget.
The idea: measure what the team sees
Section titled “The idea: measure what the team sees”Back to the smoke alarm. We judged it with two questions: when it rings, is there a fire? When there is a fire, does it ring in time? For a detector, the team’s daily work gives the same questions. Each day, the detector gives every payment a score, and the team reads the alerts with the highest scores. So we measure those alerts, and the frauds that are not among them.
Text description of the diagram
The diagram starts with one day of payments: about 1,870 payments, with about 31 planted frauds. An arrow leads to the detector, which gives each payment a score. A second arrow leads to “the top k”: the k highest scores, which are the alerts that the team reads. Three boxes come from the top k. Precision at k is the share of real frauds among the k alerts, and it answers “is the team’s time well used?”. Recall is the share of all planted frauds that the alerts find, and it answers “what do we miss?”. Time to detection is the minutes from the first payment of a fraud to the first alert, and it answers “did we stop it in time?”. A fourth box with a dashed border comes straight from all the payments: accuracy, the right answers among all payments. Almost all payments are honest, so every detector gets about 98%.
Precision at k
Section titled “Precision at k”Think of a search engine. You type a question, and you read the first page: 10 results. You do not care about result number 5,000. You care how many of those 10 results are useful. A review team works the same way. It reads the top k alerts each day, so we score those k alerts.
Precision at k is the share of real frauds among the k alerts that the team reads. The “k” is the number of alerts a day. At Larkfield, k is 50, so the course often writes “precision at the budget”. Recall is the share of all planted frauds that those alerts find. Recall needs a list of all the frauds. Here the planted frauds give us that list, so we say recall of known cases: it counts only the frauds that we know about.
Here is a tiny worked example. One day has 12 frauds. The detector’s top 10 alerts contain 7 real frauds and 3 honest payments:
| Measure | Calculation | Value |
|---|---|---|
| Precision at 10 | 7 real alerts ÷ 10 alerts | 0.70 |
| Recall | 7 frauds found ÷ 12 frauds | 0.58 |
| Frauds missed | 12 − 7 | 5 |
| False alerts | 10 − 7 | 3 |
Precision says how well the team’s time is used. Recall says how much fraud gets through. The honest payments that got no alert do not appear in either number, so the honest majority cannot hide the result.
Precision at k on Larkfield’s payments
Section titled “Precision at k on Larkfield’s payments”In the notebook, Priya scores Isolation Forest for five values of k:
rows = []for k in (10, 25, 50, 100, 200): r = compare.score_payments(evaluation, score, "Isolation Forest", f"top {k} a day", budget=k) rows.append({"alerts a day (k)": k, "precision at k": r["precision at budget"], "recall": r["recall"]})pd.DataFrame(rows)The output:
alerts a day (k) precision at k recall0 10 0.802 0.2591 25 0.691 0.5582 50 0.500 0.8083 100 0.292 0.9424 200 0.152 0.980Read one row at a time. At 10 alerts a day, 8 alerts in 10 are real, but the team finds only a quarter of the frauds. At 200 alerts a day, it finds 98% of them, but only 15% of the alerts are real. More alerts find more fraud, and each alert is less often real. Where to stop depends on the team’s time, which is the topic of the next lesson.
The precision–recall curve
Section titled “The precision–recall curve”The machine learning course drew the precision–recall curve (PR curve) for a classifier. Each threshold gives one point, with its recall across and its precision up. The same curve works for anomaly scores. Here, each point is one daily budget, from 5 to 200 alerts a day. So the curve shows every k at once.
- Isolation Forest
- Two rules, ranked
- MAD per customer
- Perfect (dashed)
Text description of the chart
A line chart with recall from 0 to 1 across and precision from 0 to 1 up. Each detector has one point for each of 11 daily budgets, from 5 to 200 alerts a day. Isolation Forest starts at the top left (5 a day: precision 0.866, recall 0.140) and goes down to the right. At 50 a day, marked on the chart, it has precision 0.500 and recall 0.808. At 200 a day it ends at precision 0.152 and recall 0.980. The two rules, ranked, stay between precision 0.13 and 0.45 and reach a recall of 0.863. MAD per customer stays near the bottom: precision below 0.07, recall at most 0.241. The perfect detector is a dashed line just above Isolation Forest: it starts at precision 0.927 and reaches recall 1.0 at 150 alerts a day.
A curve near the top right is better: high precision and high recall at the same time. Isolation Forest is close to the perfect detector. MAD per customer stays near the bottom, so no budget makes it useful for these frauds.
Why does the perfect detector’s curve fall to the right? On many days there are fewer frauds than the budget. When the team reads 200 alerts on a day with 31 frauds, at most 31 of them can be real. So precision must fall as the budget grows, even with perfect knowledge.
The machine learning course also summarised the curve in one number, the average precision. In the notebook, the chart cell computes it with one threshold for all 91 days together, not 50 a day. Isolation Forest gets 0.901. Random scores would get the share of planted payments, 0.0166. This one number is useful to compare detectors quickly. But it mixes every budget, and Sofia’s team works at one budget only. So the course reports precision at the budget.
Time to detection
Section titled “Time to detection”A stolen card does not pay once. The fraudster uses it again and again until somebody stops it. So for fraud, when the first alert comes also matters. Time to detection is the time from the first payment of a fraud to the first alert about it. The course calls all the payments of one fraud an episode: one stolen card, one card test or one account takeover.
episodes = compare.planted_payments().set_index("payment_id")alerts = compare.daily_alerts(evaluation, score, 50)a = alerts[alerts.payment_id.isin(episodes.index)].copy()a["episode"], a["kind"] = a.payment_id.map(episodes.episode), a.payment_id.map(episodes.kind)first = a.groupby("episode").agg(kind=("kind", "first"), start=("ts", "min"), first_alert=("ts", lambda s: s[a.loc[s.index, "reviewed"]].min()))first["minutes"] = (first.first_alert - first.start).dt.total_seconds() / 60first.groupby("kind").agg(episodes=("minutes", "size"), found=("minutes", "count"), median_minutes=("minutes", "median"))The output:
episodes found median_minuteskindaccount_takeover 111 53 0.000card_testing 26 26 0.817stolen_card 169 167 0.000A median of 0 minutes means that, for at least half of the episodes found, the very first payment was already in the top 50. That is true for 147 of the 306 episodes. Card tests are found less than a minute after they start. But the column found tells a second story. Isolation Forest finds almost every stolen card and every card test, and fewer than half of the account takeovers.
Text description of the chart
A column chart with three columns on an axis from 0 to 100%. Stolen card: 98.8% of the 169 episodes found. Card testing: 100% of the 26 episodes found. Account takeover: 47.7% of the 111 episodes found. The account-takeover column is about half the height of the other two.
An account takeover uses the real customer’s account, often on the customer’s own device. So it looks more normal than a stranger’s card in a new country. One average recall would hide this weak spot: always look at recall by kind.
For time series, the comparison table of Module 5 already measures time to detection in minutes. On the ticket API’s slow memory leak, the latency threshold alerted after 3,342 minutes, more than two days. The seasonal baseline alerted after 506 minutes. Both “found” the leak, so recall alone would call them equal.
Which measure, when
Section titled “Which measure, when”| Question | Measure | Use it when | Do not use it when |
|---|---|---|---|
| Is the team’s time well used? | Precision at k (at the budget) | A team reviews a fixed number of alerts | Nobody reviews the alerts |
| What gets through? | Recall of known cases, by kind | You have planted or confirmed cases | The known cases are only one kind |
| Do we act in time? | Time to detection | Each minute costs money (fraud, outages) | The harm happens all at once |
| Which detector ranks better overall? | PR curve, average precision | You compare detectors before you choose a budget | You report to the team that works at one budget |
| How often is it right? | Accuracy | The two classes are about the same size | Anomalies are rare: almost always here |
These measures cost little: you need the alerts and a list of known cases. The list is the hard part. Planted cases exist only in test data like Larkfield’s. Later in this module, you estimate precision without a full answer key.
Guided practice: the measures in the notebook
Section titled “Guided practice: the measures in the notebook”- Open the notebook from the box Do this lesson in a notebook at the top of this page.
- Run the setup cells. The data cell ends with the
Ready:line. - In section 1, run the tiny example. You see
accuracy of 'nothing is fraud': 0.999. - In section 2, run the cell that loads the payments and fits Isolation Forest. You see
2,817 planted (1.66%). - Write your prediction for the three accuracies. Then run the next cell. You see
0.9834,0.9834and0.9857. - In section 3, run the precision at k cell. At k = 50, you see a precision of
0.500and a recall of0.808. - In section 4, run the PR curve cell. The chart title shows the average precision,
0.901. - In section 5, run the time to detection cell. You see
53of111account takeovers found.
Your turn
Section titled “Your turn”The notebook’s section 6 has two tasks. First, compute the accuracy of “nothing is fraud” on the evaluation payments, rounded to 4 decimals, as accuracy_nothing. Second, compute Isolation Forest’s precision at k = 100 alerts a day as precision_at_100. Before you run the second one, predict it from the table above.
Show the answers
accuracy_nothing = round(float((~planted).mean()), 4)precision_at_100 = compare.score_payments(evaluation, score, "Isolation Forest", "top 100", budget=100)["precision at budget"]accuracy_nothing is 0.9834, and precision_at_100 is 0.292. At 100 alerts a day, fewer than 3 alerts in 10 are real. The accuracy of “nothing is fraud” is the share of honest payments, 1 − 0.0166.
Common mistake
Choosing the detector by accuracy
Symptom: Tomás ranks the detectors of the comparison table by accuracy at 50 alerts a day: kNN distance 0.9837, Isolation Forest 0.9834, “nothing is fraud” 0.9834, the autoencoder 0.9799, the two rules 0.9759, MAD per customer 0.9592. The ranking looks precise, and nothing raises an error. But doing nothing ties with Isolation Forest and beats three real detectors.
Cause: with 1.66% frauds, accuracy is decided by the honest payments. A detector that sends 50 alerts makes up to 50 wrong answers a day, and doing nothing makes about 31. So accuracy punishes alerts, even good ones.
Fix: rank the detectors by precision at the budget, with recall by kind and the time to detection beside it. For the same detectors, precision at 50 a day goes from 0.505 (kNN distance) to 0.048 (MAD per customer), and doing nothing has no alerts at all.
Consequence in AI work: a team that reports accuracy for rare events can ship a detector that catches nothing, and the dashboard still shows 98%. The same trap exists for any rare event: equipment failures, rare diseases, security incidents.
Knowledge check
Section titled “Knowledge check”Check your understanding
Omar reads that a new detector is 99.2% accurate on payments where 0.8% are fraud. What do you tell him, and which numbers do you ask for?
Show the answer
A detector that never alerts is also 99.2% accurate here, because 99.2% of the payments are honest. So 99.2% says nothing about the frauds. Ask for three numbers. First, precision at the team’s budget: the share of the daily alerts that are real. Second, recall of the known frauds, by kind. Third, the time to detection. At Larkfield, accuracy is 0.9834 for doing nothing and for Isolation Forest. But precision at 50 a day is 0 for one and 0.500 for the other.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
Predict and explain: the team doubles its budget from 50 to 100 alerts a day. What happens to Isolation Forest's precision at k and its recall?
Show the answer
Recall goes up and precision goes down. The extra 50 alerts a day have lower scores, so fewer of them are real. Measured: precision falls from 0.500 to 0.292, and recall rises from 0.808 to 0.942. The PR curve shows the same: a step to the right and down.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
Isolation Forest finds 167 of 169 stolen cards, all 26 card tests and 53 of 111 account takeovers, with a median of 0 minutes for the ones it finds. What does one overall recall hide, and why does the time to detection matter?
Show the answer
One overall recall mixes the kinds, so it hides that fewer than half of the account takeovers are found. Account takeovers look more normal, because they use the customer’s own account and often the customer’s device. The time to detection matters because a fraud keeps paying until somebody stops it. An alert after the last payment finds the fraud but saves no money.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Recap
- When frauds are rare, accuracy is about the same for every detector: 0.9834 for doing nothing and for Isolation Forest, 0.9857 for a perfect detector. By accuracy, doing nothing beats MAD per customer.
- Precision at k is the share of real frauds among the k alerts that the team reads each day. Recall of known cases is the share of the known frauds that they find. At 50 a day, Isolation Forest has 0.500 and 0.808.
- More alerts a day raise recall and lower precision. The PR curve shows every budget at once; even the perfect detector’s precision falls when the budget is larger than the number of frauds.
- Time to detection says if the alert comes in time. Recall by kind shows the weak spot: Isolation Forest finds only 53 of 111 account takeovers.
You can now measure a detector at any number of alerts a day. But which number should Larkfield choose? Next, you choose the threshold from Sofia’s budget and compute what each choice costs per week.
Next: The alert budget