Part 3: Machine learning Lesson 10 of 20 in this part
Anomalies, patterns and density
What you will learn. Use Mahalanobis distance to judge how unusual a point is given how the features move together, use a kernel density estimate to see where data is crowded, and find shopping-basket rules with support, confidence and lift.
Before this lesson: Unsupervised learning: groups, compression and anomalies. Already know these? Carry on.
In plain words
Three tools help you find what is unusual and what goes together. First, Mahalanobis distance measures how far a point is from the centre of the data, but it takes into account how features move together. Suppose height and weight usually rise together. A person who is tall and light is more unusual than one who is tall and heavy, even if both are the same straight-line distance from the average. Second, a kernel density estimate draws a smooth picture of where data is crowded by placing a small bell-shaped bump over every data value and adding the bumps up. Third, association rules such as "people who buy bread often buy milk" are found by counting baskets, using three numbers: support, confidence and lift.
Straight-line distance
- Counts every direction the same
- Ignores how features move together
Mahalanobis distance
- Stretches along the usual pattern
- Large when a point breaks the pattern
Mahalanobis distance asks whether a point fits the usual pattern, not only how far it is.
- InputPut a small bell-shaped bump on every data valueThe width is a setting called bandwidth
- ProcessAdd all the bumps togetherCrowded places pile up
- OutputRead the height at any pointHigh is crowded, low is rare
Where bumps pile up, the estimate is high. Where data is thin, it is low.
- InputSupportHow often the items appear together in all baskets
- ProcessConfidenceOf baskets with the first item, how many also have the second
- OutputLiftHow much more often than chance the two go together
A rule is worth a look when confidence is high and lift is clearly above one.
Real-world example
A card company watches two features of each purchase: the amount, and the distance from the cardholder's home. Large amounts and long distances usually go together on holiday trips, so a large amount far from home is normal. A large amount close to home, or a small amount very far away, breaks the usual pattern. Straight-line distance cannot tell these apart, but Mahalanobis distance flags the odd ones. Separately, a supermarket counts baskets and finds that people who buy bread also buy milk more often than chance, and uses the rule to place the two close together.
How it works
- Mahalanobis. Find the average of each feature and the covariance table (how each pair of features moves together). Invert that table. For a new point, the squared distance is its difference from the average, times the inverted table, times the same difference again.
- Density. For any value, add up the height of a small bell curve centred on every data value. Divide by the number of values.
- Association rules. Count baskets. Support of a rule is the share of all baskets that contain both items. Confidence is the share of baskets with the first item that also have the second. Lift is the confidence divided by how common the second item is on its own.
Live simulation
Anomaly detection ranks points by how unusual they are, then a flag rate decides how many to act on.
The question. How do we find the unusual points in data when nobody has labelled them?
What to watch
- Each point's score is its distance to its 5th nearest neighbour. A point far from everything gets a high score.
- You choose what share of points to flag. Reveal the real anomalies to see how many were caught and how many flags were false alarms.
- Raise the share and watch false alarms grow faster than catches.
This simulation needs JavaScript, which is switched off or not loaded. The lists above say what it shows, and the example programs below print the same kind of numbers.
Try this
- Catch all 4 real anomalies with at most 2 false alarms.
What this leaves out. 84 made-up points in two dimensions, with 4 planted anomalies we know about. In real work nobody knows the true anomalies, so the flag rate is chosen from the cost of a miss against the cost of a false alarm. Other methods (isolation forest, density) behave differently on real data.
Python program that prints the same headline numbers: examples/sim-core/core.py.
Python example
Python: example run The example programme behind this lesson's quoted output ran on 2026-11-19 (Python 3.12.10). 1 code block in the lesson passed a syntax check, 1 also ran on their own.
Three small demonstrations in one file: Mahalanobis distance for two points at nearly the same straight-line distance, a kernel density estimate on twelve values, and the three numbers of a rule on eight baskets.
"""Three pattern tools: Mahalanobis distance, kernel density, and association rules (support, confidence, lift).
Run: python patterns.py (needs numpy)
"""
import numpy as np
# 1. Mahalanobis distance: how unusual is a point, given how the data usually moves together?
i = np.arange(30)
x = 2.0 * np.sin(0.9 * i)
y = 0.8 * x + 0.3 * np.cos(2.1 * i) # y follows x, with a little wobble
P = np.column_stack([x, y])
mu = P.mean(axis=0)
cov = np.cov(P, rowvar=False)
inv = np.linalg.inv(cov)
for q in ((1.5, 1.2), (1.5, -1.2)): # same distance from the centre, different direction
v = np.array(q) - mu
print("point (%.1f, %.1f): straight-line distance %.3f Mahalanobis distance %.3f" % (q[0], q[1], np.sqrt(v @ v), np.sqrt(v @ inv @ v)))
# 2. Kernel density: add a small bell curve over every data value
vals = np.array([1.0, 1.2, 1.5, 1.9, 2.1, 2.4, 6.0, 6.3, 6.5, 6.8, 7.1, 12.0])
def density(at, h=0.5):
return float(np.mean(np.exp(-0.5 * ((at - vals) / h) ** 2) / (h * np.sqrt(2 * np.pi))))
print("density at 2.0: %.3f at 4.5: %.3f at 12.0: %.3f" % (density(2.0), density(4.5), density(12.0)))
# 3. Association rules on eight shopping baskets
baskets = [{"bread", "milk"}, {"bread", "eggs"}, {"bread", "milk", "eggs"}, {"milk"},
{"bread", "milk"}, {"eggs"}, {"bread", "milk", "tea"}, {"tea", "milk"}]
def share(items):
return sum(items <= b for b in baskets) / len(baskets)
s_both, s_bread, s_milk = share({"bread", "milk"}), share({"bread"}), share({"milk"})
print("bread and milk: support %.3f confidence(bread -> milk) %.3f lift %.3f" % (s_both, s_both / s_bread, (s_both / s_bread) / s_milk))point (1.5, 1.2): straight-line distance 1.847 Mahalanobis distance 1.017
point (1.5, -1.2): straight-line distance 1.907 Mahalanobis distance 11.145
density at 2.0: 0.246 at 4.5: 0.001 at 12.0: 0.066
bread and milk: support 0.500 confidence(bread -> milk) 0.800 lift 1.067The two points are almost the same straight-line distance from the centre (1.847 and 1.907), but their Mahalanobis distances are 1.017 and 11.145. The second point goes against the pattern in which y follows x, so it is far more unusual. The density is high at 2.0 (0.246), where several values sit together, and almost zero at 4.5 (0.001), where there is a gap. A single value at 12.0 has a small bump of its own (0.066). In the baskets, the rule bread to milk has support 0.500, confidence 0.800 and lift 1.067.
.NET (C#) example
C#: compiled and run The C# sample compiled on 2026-11-19 with .NET SDK 10.0.400 (no extra packages). It also ran to the end without error.
Everything here is written from scratch in C#, with no packages: the 2 by 2 inverse is worked out by hand, the density is a simple average of bell curves, and the rule numbers are counts. It prints the same numbers as the Python run. ML.NET also offers PCA-based anomaly detection for the same purpose.
// Same numbers as patterns.py, written from scratch with no packages.
// 1. Mahalanobis distance
int m = 30;
double[] x = Enumerable.Range(0, m).Select(i => 2.0 * Math.Sin(0.9 * i)).ToArray();
double[] y = Enumerable.Range(0, m).Select(i => 0.8 * x[i] + 0.3 * Math.Cos(2.1 * i)).ToArray(); // y follows x, with a little wobble
double mx = x.Average(), my = y.Average();
double sxx = x.Sum(v => (v - mx) * (v - mx)) / (m - 1);
double syy = y.Sum(v => (v - my) * (v - my)) / (m - 1);
double sxy = Enumerable.Range(0, m).Sum(i => (x[i] - mx) * (y[i] - my)) / (m - 1);
double det = sxx * syy - sxy * sxy; // inverse of a 2x2 table by hand
foreach (var (qx, qy) in new[] { (1.5, 1.2), (1.5, -1.2) }) // same distance from the centre, different direction
{
double vx = qx - mx, vy = qy - my;
double straight = Math.Sqrt(vx * vx + vy * vy);
double mahal = Math.Sqrt((syy * vx * vx - 2 * sxy * vx * vy + sxx * vy * vy) / det);
Console.WriteLine($"point ({qx:F1}, {qy:F1}): straight-line distance {straight:F3} Mahalanobis distance {mahal:F3}");
}
// 2. Kernel density: add a small bell curve over every data value
double[] vals = { 1.0, 1.2, 1.5, 1.9, 2.1, 2.4, 6.0, 6.3, 6.5, 6.8, 7.1, 12.0 };
double Density(double at, double h = 0.5) =>
vals.Average(v => Math.Exp(-0.5 * Math.Pow((at - v) / h, 2)) / (h * Math.Sqrt(2 * Math.PI)));
Console.WriteLine($"density at 2.0: {Density(2.0):F3} at 4.5: {Density(4.5):F3} at 12.0: {Density(12.0):F3}");
// 3. Association rules on eight shopping baskets
var baskets = new List<HashSet<string>> {
new() { "bread", "milk" }, new() { "bread", "eggs" }, new() { "bread", "milk", "eggs" }, new() { "milk" },
new() { "bread", "milk" }, new() { "eggs" }, new() { "bread", "milk", "tea" }, new() { "tea", "milk" } };
double Share(params string[] items) => baskets.Count(b => items.All(b.Contains)) / (double)baskets.Count;
double both = Share("bread", "milk"), bread = Share("bread"), milk = Share("milk");
Console.WriteLine($"bread and milk: support {both:F3} confidence(bread -> milk) {both / bread:F3} lift {both / bread / milk:F3}");Maths behind it
- Covariance and the inverse of a 2 by 2 table (linear algebra, statistics)
- The bell curve (normal density) used as a kernel (probability)
- Counting shares: support, confidence and lift (probability)
Common mistakes
- Using straight-line distance for anomalies when features move together.
- Using a density bandwidth that is far too small (every value its own bump) or too large (everything blurred).
- Calling a rule "interesting" because its confidence is high, without checking lift.
- Treating a basket rule as a cause.
- Computing Mahalanobis distance with too few points. The covariance table needs plenty of data.
Quick check
Two points are the same straight-line distance from the average. When would they have very different Mahalanobis distances?
Show answer
When the features move together. A point along the usual pattern is close in Mahalanobis terms, and a point against the pattern is far.
What does a lift of exactly 1 mean for an association rule?
Show answer
No link. The two items appear together exactly as often as chance would predict.
What does the bandwidth control in a kernel density estimate?
Show answer
The width of each bell-shaped bump. Small bumps give a spiky picture that follows every value. Large bumps give a blurry picture.
Out of 10 baskets, 6 have bread and 3 have both bread and milk. What is the confidence of "bread then milk"?
Show answer
3 ÷ 6 = 0.5. Half of the baskets with bread also have milk.
Completion
Evidence of completion. A small table showing two points at the same straight-line distance but different Mahalanobis distances, and the support, confidence and lift of one rule on a dataset you choose.
Saved in this browser only. Reading and exercise are ticked separately because they are different achievements. A tick records your own work; it is not a test result. See all progress.