2.1.3 · D1Data Preprocessing & Feature Engineering

Foundations — Outlier detection and treatment

2,973 words14 min readBack to topic

Before you can read a single formula in the parent note, you need a vocabulary of small pictures. This page builds every one of them from nothing. We never use a symbol before we draw it.


0. The raw material: data points on a line

Everything starts with numbers you collected. Ages, prices, temperatures — each measurement is one data point.

Picture it as beads dropped onto a number line. Most beads clump together; a few wander off. That wandering bead is the thing we are hunting.

Figure — Outlier detection and treatment
  • — one bead (look at the coral dot on the far right of the figure).
  • — how many beads total.
  • The number line — the stage on which "far" and "near" have meaning.

1. The middle: mean

To say a point is "far from the crowd", you first need a single number that marks where the crowd is.

Let's earn every symbol in that line:

  • (capital Greek "sigma") means add them all up. The below and above say "start at the first bead, stop at the last."
  • then shares the total equally among all beads.

2. The typical spread: standard deviation

Knowing the middle isn't enough. In one dataset the beads huddle tightly; in another they scatter widely. We need a number for "typical distance from the middle."

Reading it piece by piece, inside-out:

  • — this is the signed deviation: bead 's position minus the centre. WHY subtract? Subtraction measures displacement from the reference . But watch the sign: a bead to the right of the mean gives a positive number, a bead to the left gives a negative number. So by itself is not a distance — it is a signed gap that can point either way.
  • — square it. WHY square? This is precisely where we throw away the sign: squaring turns every signed gap into a positive quantity, so left-beads and right-beads no longer cancel each other out. As a bonus it punishes far beads much harder than near ones. (Only after squaring do we have something distance-like.)
  • then — average those squared gaps.
  • — undo the squaring so the answer is back in the original units (rupees, years), not squared units.
Figure — Outlier detection and treatment

The z-score: counting rulers

Now we can name the tool the parent note leans on. Once you have (the centre) and (the length of one ruler), the natural question is "how many rulers away is a point?"


3. Rank-based landmarks: percentiles, , , IQR

Because and can be fooled, we build a ruler that ignores how big the wild beads are and only cares about their position in line.

Figure — Outlier detection and treatment

4. From one dimension to many: vectors and

Real data has several features per row (sqft and bedrooms). One bead is no longer a point on a line — it's a point in space.

The mean is now a mean vector : the average bead position, one coordinate per feature.

Two new pieces of notation, both easy:

  • The little means transpose — flip a column of numbers on its side into a row so it can be multiplied. Because is the element-wise vector above, the product is just "square each coordinate gap and add them" — the same Pythagoras you already know, extended to features. (Squaring again removes the signs, exactly as in Section 2.)
Figure — Outlier detection and treatment

5. Correlation and the covariance matrix (a first look)

So far our beads were single numbers . Now each bead has several features. To talk about "how two features move together," let us give the two feature-columns names: write for the value of the first feature at bead , and for the value of the second feature at the same bead . So is exactly the first coordinate and the second coordinate of the vector from Section 4 — the same data, just sliced column-by-column instead of row-by-row. Each has its own mean, and .

You don't need to hand-compute here — you only need the picture: is the recipe for the tilt and stretch of that lavender ellipse. The diagonal sets how wide the ellipse is along each axis; the off-diagonal sets its tilt. The parent note uses ("Sigma inverse", the undo-operation) to squash the ellipse back into a circle so ordinary distance works again. Keep the ellipse picture; that's the payload.


6. Neighbourhoods and density (a first look)

The last family of methods (LOF) doesn't ask "far from the centre?" but "far from your neighbours?"


Prerequisite map

The diagram below is a flow map. Each box is one idea from this page; each arrow means "you need the box at the tail before the box at the head makes sense."

In words, in case the diagram does not render: Data points on a line is the root. From it, three branches grow. (1) The mean gives the centre, and the mean feeds the standard deviation (the spread), which feeds the z-score method. (2) The same data points, ranked, give percentiles and quartiles, which feed the IQR fences method. (3) Data points also become vectors in ; vectors give Euclidean distance, which — once covariance shapes the ellipse — feeds the Mahalanobis distance; separately vectors give neighbours and density, which feed the Local Outlier Factor. All four methods (z-score, IQR, Mahalanobis, LOF) pour into the parent topic, Outlier detection and treatment.

Data points on a line

Mean gives the centre

Percentiles and quartiles

Standard deviation is the spread

Z-score method

IQR fences method

Vectors in R-d

Euclidean distance

Covariance shapes the ellipse

Mahalanobis distance

Neighbours and density

Local Outlier Factor

Outlier detection and treatment


  • Scaling turns these rulers comparable across features → 2.1.02-Feature-scalingand-normalization
  • Missing values must be handled before you trust any mean or quartile → 2.1.01-Missing-data-handling
  • Outliers as the thing you want (not noise) → 3.3.02-Anomaly-detection
  • Penalising extreme model behaviour → 4.1.03-Regularization

Equipment checklist

Can you say each of these out loud before opening the parent note?

What does the subscript in mean?
A name-tag: is the -th data point in the dataset of points.
What is telling you to do?
Add up the expression for every point, from the first to the last.
In plain words, what is the mean ?
The balance point of all data — sum of values divided by count.
Why does one wild point drag ?
It carries weight in the average, tilting the see-saw toward itself.
What does standard deviation measure?
The typical (roughly average) distance of points from the mean.
What does the z-score tell you?
How many typical spreads () a point sits from the mean; a plain, unit-free number.
Is a distance?
No — it is a signed deviation; it is positive right of the mean, negative left. Squaring removes the sign to make it distance-like.
Why do we square the gaps inside ?
To strip the sign so left and right gaps don't cancel, and to punish far points more.
What does the symbol mean in "-th percentile"?
A chosen percentage ; the -th percentile is the value below which of sorted points fall.
What happens when is not a whole number?
You interpolate — slide proportionally between the two neighbouring sorted points.
What is the IQR and why is it robust?
The width of the middle 50% (); it depends on positions, not extreme values, so wild points can't stretch it.
What does mean?
The space of all lists of real numbers — one dimension per feature.
What does vector subtraction mean?
Subtract element-wise, one coordinate at a time; the result is the arrow from to .
What does the transpose do in ?
Flips a column into a row so the product becomes "square each coordinate gap and add" — Pythagoras in dimensions.
What shape does Euclidean distance treat as "equally far"?
A circle (or sphere) around the centre.
How do and relate to the vector ?
They are its coordinates sliced by column — is feature 1, is feature 2, at the same bead .
What does the entry hold?
The covariance of feature with feature ; the diagonal holds each feature's variance.
Why isn't a circle the right shape for correlated data?
Correlated features form a tilted ellipse; encodes that tilt (off-diagonal) and stretch (diagonal).
What does the number represent in nearest neighbours?
How many closest beads count as your local crowd — a knob you choose (often around 20).
What does Local Outlier Factor compare?
A point's local density against the density of its neighbours.