May we use cookies so Meta, Google, LinkedIn and ChatGPT see your visit and we learn which ads work? PrivacyMay we use cookies to see which pages help, and let Meta, Google, LinkedIn and ChatGPT see your visit, so we know which of our ads work? That way we spend less on ads that do not work. We do not sell your data, and the site works the same if you decline. You can change your answer at any time under Cookie settings at the foot of the page. Privacy policy
Loading...
This idea carries a full system design question on its own. Each walks through the full answer.
AI Engineering · Data Engineering For Ml
Free preview: the first 2 parts of this 27-part lesson.
Already bought the course? Sign in to read the whole lesson.
this lesson is part of
Generative AI and Agentic AI Engineering
Unlock this lesson and the 208 other paid lessons
$35 once, by card or PayPal
See the Generative AI and Agentic AI courseread the rest of this lesson in
Generative AI and Agentic AI Engineering
Unlock this lesson and the 208 other paid lessons
$35 once, by card or PayPal
See the Generative AI and Agentic AI coursein Data Engineering For Ml
This lesson is part of the AI Engineering course. Unlock the full course, a separate one-time payment, for lifetime access.
One-time payment by card or PayPal
Get Lifetime AccessOne-time payment: UPI, card or netbanking
Get Lifetime AccessFoundation lessons of this course are free, no payment needed. Try them now
Picture a weather forecaster in a desert town. Every morning he says the same thing: no rain today. It rains on maybe two days a year. So he is right on about 363 days out of 365, more than 99 percent of the time. He has never once warned anyone to bring an umbrella.
Is he a good forecaster? By the count of right answers, he is excellent. By the one thing people need from him, he is useless. The rare day is the whole reason to have a forecaster.

Machine learning has the same trap. When one kind of case is rare, a model can score very well by never naming it. In this lesson I measure that on real card payments. A model that calls every payment legit scores 99.827 percent on the test set and catches none of the 148 frauds.
In real work the rare case is often the one that matters: fraud, a rare disease, a broken part on a factory line. This lesson is about how to tell when a score is lying to you, which number to read instead, and what the common fixes really do. It ends with the other half of the title: messy data, such as copied rows and gaps, which can quietly bend every score you read.
This is the eighth lesson of the chapter on data engineering for machine learning. Lesson 7, on data labeling, was about where labels come from. This lesson is about what happens when one label is far rarer than the other.
Two later lessons in this chapter measure parts of this story in depth, and I build on them here rather than repeat them. Lesson 12, rebalance or threshold, compared rebalancing the data with simply moving the model's cut-off. On average, a plain model with a well-chosen cut-off beat rebalanced models left at the default. Lesson 13, leakage before the split, measured how much each preparation step leaks when it runs before the data is split.
What this rebuild corrected. An earlier version of this lesson made claims its sources do not support. Each is fixed where it comes up, and the full record is in
results/ibl-factcheck.json.
- It opened with a fraud team's story. It was made up, and so were the confusion matrices and curves that followed it.
- It said fraud is "roughly 1 transaction in 1000" in a typical payments stream. I found no source. The real public data used here is 0.173 percent.
- It said ROC-AUC is "flatteringly high" on rare classes. More exactly, it does not move when only the rarity changes. That is a different problem, measured below.
- It said fitting an imputer on all rows turned a 0.95 score into 0.62 in production. Those numbers were invented, and lesson 13 measured that rescaling, a similar step, barely leaked.
- It said to start with class weights or SMOTE and tune the threshold last. Lesson 12 measured the opposite order working better.

The plan follows that chart from top to bottom. First the trap itself, then the two numbers people use in place of accuracy and how they behave as the rare class gets rarer. Then the split, the cut-off and rebalancing, each with a real measurement. Last, the messy data: copies, gaps and leaks.