Launch your tech mastery with us—your coding journey starts now!
Course Content
Introduction to Machine Learning
At its core, Machine Learning (ML) is a branch of artificial intelligence that focuses on building systems that learn or improve performance based on the data they consume.
0/5
Machine Learning

The Simple Definition

An outlier is a data point that is radically different from all the other data points in your dataset. It is the extreme “odd one out.”

If most of your data sits comfortably in a cluster, the outlier is the one dot sitting miles away by itself.

Why Are Outliers a Problem? (The Elon Musk Effect)

To understand why outliers ruin Machine Learning models, let’s look at a real-life example.

Imagine you are sitting in a coffee shop with nine other regular people. The average net worth of the room is roughly $50,000. Suddenly, Elon Musk walks in. Because his net worth is over $200 billion, the new “average” net worth of the room instantly shoots up to $18 billion per person!

If a Supervised Learning algorithm looks at that room, it will learn a completely false rule: “Ah, people who drink coffee are billionaires.” Outliers pull the model’s mathematical line away from reality, causing it to make terrible predictions on normal, everyday data.

How Do We Find Them? (Connecting to Visualization)

Before you can handle an outlier, you have to find it. You can’t usually spot them just by staring at a Pandas DataFrame. Instead, we use the tools from our previous lessons: Matplotlib and Seaborn!

  • Scatter Plots: If you plot your data on a simple X/Y graph, the normal data will form a dense cloud. An outlier will be the single dot floating way off in the corner.
  • Box Plots: This is a special chart specifically designed by statisticians to hunt outliers. It draws a box around your normal data and puts little dots outside the box to warn you: “Hey, these points are mathematically weird!”

How to Handle Outliers (Step-by-Step Options)

Once you find an outlier, what do you do with it? Just like with missing values, you have a few practical choices depending on the situation.

Option 1: Drop Them (Delete the Row)

If you are 100% sure the outlier is a mistake or a glitch, you simply delete the row using Pandas.

  • Example: You are analyzing a dataset of human ages, and one person is listed as being 350 years old. That is a typo. Delete it so it doesn’t confuse your algorithm.

Option 2: Cap Them (Squeeze Them In)

Sometimes, the data is real, but it is just too extreme for your model to handle fairly. Instead of deleting it, you create a “ceiling” and a “floor.”

  • Example: You are predicting the price of normal family cars. One row is a multi-million-dollar Ferrari. Instead of deleting it, you “cap” your maximum price at $100,000. The model treats the Ferrari as a $100,000 car, keeping the math stable while still keeping the row of data.

Option 3: Keep Them (The Outlier IS the Target!)

Sometimes, you absolutely cannot touch the outlier because the outlier is exactly what you are trying to predict!

  • Example: In Fraud Detection, normal credit card purchases are the cluster, and the $5,000 TV bought halfway across the world is the outlier. If you delete or cap the outlier, your bank’s fraud model will literally have nothing to learn from!

Practical Use Cases

Every data scientist has to wrangle outliers. Here is how it looks in the real world:

  • Real Estate Algorithms: Zillow drops massive mansions from its training data when trying to learn how to price normal, three-bedroom suburban homes, because the mansions skew the neighborhood average.
  • IoT (Internet of Things) Sensors: A factory temperature sensor usually reads 70°F. Suddenly it spikes to 900°F for exactly one second, then goes back to 70°F. The system identifies this as a sensor glitch (an outlier) and drops it to avoid triggering a false fire alarm.
  • Medical Research: If a drug lowers blood pressure for 99% of people, but causes a massive spike in 1% of people, researchers keep the outliers. Those extreme reactions are vital for discovering side effects.

Summary

Outliers are extreme, unusual data points that sit far away from the rest of your data. If left alone, they can act like a magnet, pulling your Machine Learning model’s logic entirely off course. By using visualizations to find them, we can use Pandas to either Drop them (if they are errors), Cap them (if they are just a bit too extreme), or Keep them (if the extreme event is exactly what we want our AI to study).