NelworksNelworks

ML & AI / Season 1 / EP08

THE TYRANNY OF THE MAJORITY (IMBALANCED RANDOM FORESTS)

Shez

The bank is secure, Kurumi! My AI is active!

Kurumi

Secure?

Shez

Look at that score! I trained a Random Forest on the transaction logs. It's practically perfect.

Kurumi

99.9% accuracy on fraud detection. Okay. Let's test it.

Shez

Wait... hold on...

Shez

It... it didn't flag you.

Kurumi

I just robbed you, and your model held the door open for me.

Shez

But the accuracy! It's 99.9%! How can it be wrong if the math says it's right?

Kurumi

Silly, its because you are suffering from **Class Imbalance**.

Kurumi

How many transactions happen a day?

Shez

One million.

Kurumi

999,000 are legit. 1,000 are fraud.

Kurumi

If I write a script that says `return "Legit"` for *everything*...

Shez

It would be right 999,000 times... and wrong 1,000 times.

Shez

...99.9% accuracy.

Shez

Nooo. My model isn't smart. It's just lazy. It figured out that "Safe" is always a safe bet.

Kurumi

It's **KPI Cosplay**. You optimized for a metric that hides crime.

Shez

But why did the Random Forest do this? It's an ensemble! It's supposed to be robust!

Kurumi

Random Forest uses **Bagging** (Bootstrap Aggregating). Each tree gets a random subset of the data.

Kurumi

Since Fraud is so rare (0.1%), most of these trees didn't even *see* a fraud case in their training bag.

Kurumi

They have no concept of theft. They only know honest citizens.

Shez

"I saw a crime! I saw it!"

Kurumi

Maybe 1 or 2 trees actually got the fraud data.

Shez

Transaction Safe?

Shez

YES! YES! YES! (Deafening roar).

Kurumi

This is **Majoritarian Rule**. The forest takes a majority vote.

Kurumi

98 votes for Safe. 2 votes for Fraud.

Kurumi

Result: Safe.

Shez

I-It's the Tyranny of the Majority! The trees are suppressing the truth!

Kurumi

The algorithm is designed to minimize error. To the algorithm, the 2 Fraud trees look like **Noise**.

Kurumi

And it gets deeper. Look at how the trees split. **Gini Impurity**.

Shez

The measure of disorder?

Kurumi

The Tree wants to separate these. But it's hard to isolate 10 tiny balls.

Kurumi

The Tree says, "Great job! I purified the big bucket!"

Kurumi

It doesn't care that the Red balls are still mixed in. It reduced the *average* impurity.

Shez

So the Fraud gets treated as an accepted impurity?

Kurumi

Exactly. The model thinks, "It's just a few bad apples, not worth a dedicated branch."

Shez

I need to fix this! I need to copy-paste the fraud rows and duplicate them 100 times!

Kurumi

**Oversampling** (copy-pasting) can lead to overfitting. The model just memorizes those specific frauds after *double counting voters*.

Shez

What about **SMOTE**? Generating synthetic frauds?

Kurumi

SMOTE helps. It creates new examples *between* existing frauds.

Kurumi

But be careful. If you SMOTE *before* you split your Test Set, you leak data. You must split first, then SMOTE only the Training set.

Kurumi

But there is a more elegant way. We don't need more data. We need to rig the election.

Shez

Class weight?

Kurumi

We tell the model: "Being wrong about a Fraud is **50 times worse** than being wrong about a Legit transaction."

Kurumi

We give the minority voters a louder voice.

Kurumi

Now, one "No" from a Fraud tree cancels out 50 "Yes" from the Legit trees.

Shez

FRAUD DETECTED!

Kurumi

This modifies the Gini calculation. It forces the tree to make splits that isolate the Red balls, even if it's "inefficient" for the Blue ones.

Shez

Nooo! My Accuracy dropped! It's down to 92%!

Kurumi

But look at your Recall. It went from 0% to 85%.

Shez

That means... I'm catching 85% of the thieves?

Kurumi

Yes. But you are also flagging some innocent people as fraud (False Positives). That's why accuracy dropped.

Shez

Is that okay?

Kurumi

Would you rather annoy a customer with a "Did you buy this?" text, or let a hacker drain their account?

Shez

Annoy the customer. Definitely.

Kurumi

Finally, stop using the default `0.5` threshold.

Shez

The probability cutoff?

Kurumi

Random Forest outputs a probability (e.g., 0.3 chance of fraud).

Kurumi

In a balanced world, 0.5 makes sense. In a fraud world, even a **0.1** (10%) chance is scary.

Shez

It works! It sees you!

Kurumi

Finally. Now you can brag about your model. Not about the Accuracy, but about the **F1-Score**.

Kurumi

Now give me back the gold bar you pocketed from the start.

Shez

...How did you know?

Kurumi

My Recall is 100%.