Outliers
Errors, rare events or the signal itself: how to find outliers and when to treat them.
Lesson 9 of 6610 minBeginner
You will learn to
- Detect outliers with IQR fences and z-scores
- Separate data errors from real extreme values
- Choose between clipping, winsorizing, transforming and keeping
Before this lesson
Intuition
Imagine you ask ten colleagues how many minutes their commute takes. Nine say between 4 and 9 minutes, and one says 40. That one answer pulls the average up, and it tells you something: perhaps that colleague lives far away, perhaps they typed the wrong number, perhaps they included a stop at the pharmacy. You cannot know from the number alone.
An outlier is a value far from the others. The word describes a position in the data, not a cause. Three causes are common:
- An error. A wrong unit, a typo, or a placeholder such as 9999 or -1 used for "unknown".
- A rare but real event. A very large purchase, a heat wave, a machine failure.
- The signal. In fraud or fault detection, the extreme values are exactly what you want to find.
The right treatment depends on the cause, so the first step is always to look at the rows, not to apply a rule. Models also differ: linear models and distance-based models are pulled hard by a single extreme value, while tree-based models mostly just split around it.
Picture it
Nine values sit close together. The value 40 is far outside the fences, so it is flagged. Note what the fences are built from: quartiles, which a single extreme value barely moves.
The math
Two standard detection rules.
IQR fences. Let and be the first and third quartiles (the values below which 25% and 75% of the data fall), and . A value is an outlier when
The factor 1.5 is a convention, not a law of nature. A larger factor, such as 3, flags only extreme cases.
Z-score. With mean and standard deviation ,
and values with are often flagged. This rule assumes the data is roughly bell-shaped, and it uses the mean and standard deviation, which are themselves moved by outliers.
Three common treatments:
- Clip to the fences: replace any value below the lower fence by the lower fence, and any value above the upper fence by the upper fence.
- Winsorize at a chosen percentile: replace values below the -th percentile and above the -th percentile by those percentiles. With , the lowest and highest 5% are pulled in.
- Transform the variable, for example with a logarithm, so that large values are compressed (see Transformations).
Worked example
Take ten commute times in minutes: 4, 5, 5, 6, 6, 7, 7, 8, 9, 40. (We computed every number below with NumPy's default quantile method.)
- Quartiles. , , so .
- Fences. Lower: . Upper: . Only 40 is outside.
- Z-score. The mean is 9.7 and the sample standard deviation is 10.75, so the z-score of 40 is . That is below 3: the z-score rule misses the outlier, because the outlier inflated the standard deviation.
- Effect of clipping. After clipping 40 to 11.5, the mean falls from 9.7 to 6.85. Without that value at all, the mean of the other nine is 6.33, and the median is 6.5 before and after.
So the mean moved by almost 3 minutes because of one row; the median did not move. That is why robust statistics are the safer way to detect extremes.
Now the decision. If 40 was typed in the wrong unit (say 4.0 hours), fix it. If it is a real long commute and you expect more of them, keep it, and prefer a model or scaling method that tolerates it. If you only want the model not to be dominated by it, clip or winsorize.
How mlflow does it
- Profile. In the data profile, numeric columns show an Outliers (IQR) statistic, the count of values outside the 1.5 x IQR fences. When more than 5% of a column's rows are outliers, the profile raises an outlier warning and the Quality tab scores it under Outliers.
- Winsorization step. In Pipeline Studio, the Extreme values group of the Choose... list on a number column has Cap the extremes (winsorization) and Clip outliers. Its limits parameter (default 0.05, from 0 to 0.5) is the share clipped at each end.
- Outlier Treatment step. The same category has Outlier Treatment, which clips values to the 1.5 x IQR fence. Clipping is the only method; it never removes rows.
- Fitted on training rows. The quantiles are learned from the training rows of each fold and then applied to the rows being scored, so the step does not leak.
- Row removal. The quick train wizard has a Remove outliers toggle that drops rows flagged by an isolation forest before training. It is off by default, with the note that outliers are sometimes signal.
Common mistakes
- Deleting outliers because they are inconvenient. The rows may be exactly the cases the model will meet in production. Check their cause first, and remove only what is truly an error.
- Using z-scores on small or skewed samples. The outlier distorts the mean and standard deviation, so it can hide itself. Prefer IQR fences or the median absolute deviation.
- Treating on the whole dataset before splitting. Quantiles computed on all rows use information from the test rows. Put the step in the pipeline so it is fitted on training rows only.
- Clipping the target by habit. Changing the target changes what you are predicting. Treat target outliers separately and say so in your report.
- Ignoring the model. Tree ensembles are fairly tolerant of extreme values; linear models, k-nearest neighbours and anything that scales by the standard deviation are not. Decide with the algorithm in mind.
Try it in mlflow
Add a winsorization step
- Open Pipelines and pick a pipeline.
- In the Columns part, find the numeric column with extreme values, press Choose..., and under Extreme values pick Cap the extremes.
- Leave the limits at 0.05, or change them, and train.
- Compare the run with a copy of the pipeline that has no outlier step.