An HR manager is reviewing the salaries in a small company. Nineteen people earn between 30,000 and 60,000 rupees a month, and one earns 12 lakh. If the manager reports the average salary, the number will describe nobody. One person has pulled it far above what everyone else earns. That single value is an outlier, and it can mislead anything that depends on the average, including a machine learning model.
An outlier is a data point that sits far from the rest of the data. Some are mistakes, such as a typing error that turns 45 into 450. Others are completely real and extremely important, such as a fraudulent transaction or a failing machine. Treating them all the same way is the most common error beginners make, and it is the one that costs the most.
In this tutorial, we explain what outliers are, where they come from and how they affect a model. We then detect them in four ways: with a scatter plot, a box plot, the z score and the interquartile range. A tested Python program shows the numbers on real data, including a result that will surprise you: removing outliers can throw away the very cases you care about. We end with how to decide whether to keep, fix or remove an outlier, common mistakes and a short FAQ.
An outlier is an observation that is very different from the rest of the values in a dataset. It may be unusually high, unusually low, or just an odd combination of values.
Outliers distort measures that use every value. The mean and the standard deviation are the two most affected. In the breast cancer data used below, the mean tumour area is 654.9, but the median is 551.1. The gap of about 100 is caused by a long tail of very large tumours. The mean has been pulled up, and the median has not.
Models are affected too. Linear regression is sensitive, because it minimises squared errors, and one extreme point has an outsized pull. In the program below, I generated 20 points around a line with a slope of about 3. With all points normal, the fitted slope was 2.94. After changing just the last point to a bad value, the slope dropped to 2.06. One point out of 20 changed the answer by almost a third.
Multivariate outliers are harder to spot, because no single column looks wrong. They need methods that look at several columns at once.
There are two families of methods. Visual methods let you see the data. Statistical methods put a number on how unusual a point is. Use both, since a chart often explains what a number cannot.
| Method | Good for | Watch out for |
|---|---|---|
| Scatter plot | Seeing two variables at once | Hard with many variables |
| Box plot | Seeing one variable and its spread | Shows one variable only |
| Z score | Roughly bell shaped data | The mean and standard deviation are themselves pulled by outliers |
| IQR rule | Skewed data, any shape | Flags many points in long tailed data |
| Isolation forest | Many variables at once | Needs a guess for the share of outliers |
A scatter plot puts one variable on each axis. Points that sit far from the main cloud are suspects. It is the quickest way to spot an outlier that involves two variables. The code below draws a scatter plot, then a box plot, using the breast cancer dataset that comes with scikit learn, which has 569 patients and 30 measurements each.

A box plot shows one variable. The box covers the middle half of the values, from the first to the third quartile. The line inside is the median. The whiskers reach to the furthest values within 1.5 times the box length, and any dot beyond a whisker is drawn as a possible outlier.

The program in the next block runs the numbers for all the methods below. Here is each method in words first.
The z score tells you how many standard deviations a value is from the mean. You subtract the mean from the value and divide by the standard deviation. A common rule flags any point with a z score above 3 or below minus 3.

Two cautions apply. The rule of 3 works best when the data is roughly bell shaped. And the mean and standard deviation are themselves pulled by outliers, so extreme points can hide each other. A z score only rescales data. It does not tell you what distribution the data follows, which the original page wrongly suggests.
The interquartile range, or IQR, is the distance between the third quartile, Q3, and the first quartile, Q1. A point is flagged as an outlier if it is more than 1.5 times the IQR below Q1, or more than 1.5 times the IQR above Q3. Because it uses quartiles, it does not depend on the mean, and it works for skewed data. The IQR is calculated for each column, not each row.
An isolation forest is built to find multivariate outliers. It builds many random trees. Points that are unusual get separated from the rest in very few splits, so they are easy to isolate. It looks at all columns together, which is why it can catch odd combinations that no single column reveals.
This program runs all four methods on the mean tumour area, then on all 30 columns, shows what removal costs, and tests the effect on a regression line.
print(“Isolation forest flags:”, int((forest.predict(df) == -1).sum()), “rows”)
This is the most important result. Applied to all 30 columns, the z score rule would drop 74 rows, and the IQR rule would drop 171, which is 30 percent of the whole dataset. Worse, 66.7 percent of the rows the IQR rule would remove are malignant cases, while malignant cases are only 37.3 percent of the data. The unusual rows are unusual because they are the serious cases. A cleaning step that removes them would make a cancer model blind to cancer.
The original page says outliers should be excluded. This result shows why that rule is dangerous.
One bad point in 20 pulled the ordinary linear regression slope from 2.94 to 2.06. A Huber regressor, which is built to give extreme points less weight, got 2.87 on the same bad data, close to the truth. When you cannot decide whether to remove a point, a method that is resistant to outliers is often better than deleting it.
With the contamination setting at 0.05, the isolation forest flagged 29 of 569 rows, which is about 5 percent. That number is set by you, not discovered by the model, so treat it as a shortlist to inspect, not a verdict.
Detection is only half the job. Before you change anything, ask why the point is unusual.
| If the outlier is | Then | Example |
|---|---|---|
| A clear error | Fix it if you can, or remove it | Age of 450, caused by a typing error |
| A real but extreme value | Keep it. Consider a robust method or a transformation | A very large customer |
| A rare event you want to find | Keep it. It is the signal | Fraud, a failing machine, a malignant tumour |
| From a different population | Model the groups separately | Retail and wholesale orders in one table |
| Not sure | Compare results with and without it, and report both | Any unclear case |
Options besides removal include capping extreme values at a limit, which is called winsorizing, applying a log transformation to a long tail, and using models that are naturally resistant to outliers, such as tree based models or the Huber regressor.
Outliers are not just dirty data to be scrubbed away. Sometimes they are mistakes, and sometimes they are the story. Learn the four ways to spot them, check each against the physical reality of your data, and compare your results with and without them before you delete anything. Run the program above on your own dataset, and see how many rows each rule would remove.