DS701 Session 3 — Mon Sep 14, 2026
State the four properties a function \(d(x, y)\) must satisfy to be called a metric. Then say, in one line, how a norm \(p(\cdot)\) gives you a metric for free.
Let \(\mathbf{u} = [1, 2, 0]\) and \(\mathbf{v} = [3, -1, 4]\). Compute (a) the inner product \(\mathbf{u}\cdot\mathbf{v}\), (b) \(\Vert\mathbf{u}\Vert_2\) and \(\Vert\mathbf{v}\Vert_2\), (c) the cosine of the angle between them, and (d) the \(\ell_1\) and \(\ell_\infty\) distances \(\Vert\mathbf{u}-\mathbf{v}\Vert_1\), \(\Vert\mathbf{u}-\mathbf{v}\Vert_\infty\). Then explain in one sentence why the cosine can be near zero while the vectors are far apart in every \(\ell_p\) distance.
Two situations from the lecture. Case 1: two long documents, almost identical, that differ in 10 words. Case 2: two 5-word documents with no words in common. Represent each document as a bit vector over the vocabulary. What is the Hamming distance in each case, why does that ranking feel wrong, and which measure from the lecture fixes it (give its formula)?
Everything today lives in \(\mathbb{R}^d\): a data object is a vector \(\mathbf{x} = [x_1, \dots, x_d]\), a dataset is an \(m \times d\) matrix (rows = objects, columns = features).
Inner product — elementwise multiply, then sum: \[\mathbf{u}\cdot\mathbf{v} = \mathbf{u}^T\mathbf{v} = \sum_i u_i v_i\]
Norm — a length. The \(\ell_2\) norm is the one you know: \[\Vert\mathbf{v}\Vert_2 = \sqrt{\mathbf{v}\cdot\mathbf{v}} = \sqrt{\textstyle\sum_i v_i^2}\]
Distance — the norm of the difference: \[d(\mathbf{u},\mathbf{v}) = \Vert\mathbf{u} - \mathbf{v}\Vert\]
Angle — inner product with the lengths divided out: \[\cos\theta = \frac{\mathbf{u}\cdot\mathbf{v}}{\Vert\mathbf{u}\Vert_2\,\Vert\mathbf{v}\Vert_2}\]
A metric \(d(x,y)\) satisfies

A metric measures dissimilarity: bigger \(=\) more different. On vectors we call it a distance. Symmetry and the triangle inequality are what let us reason about “neighborhoods” — nearest-neighbor search, clustering, and everything downstream leans on them.
Not everything we call a “distance” is one: \(1 - \cos(\mathbf{x},\mathbf{y})\) is not a metric. We will still use it, but we will say dissimilarity.
A norm \(p(\mathbf{v})\): \(p(a\mathbf{v}) = |a|\,p(\mathbf{v})\), \(\ p(\mathbf{u}+\mathbf{v}) \le p(\mathbf{u}) + p(\mathbf{v})\), \(\ p(\mathbf{v}) = 0 \iff \mathbf{v} = 0\).
Every norm gives a metric: \(d(\mathbf{x},\mathbf{y}) = p(\mathbf{x} - \mathbf{y})\).
\[ \Vert \mathbf{x} - \mathbf{y} \Vert_p = \Big(\sum_{i=1}^d |x_i - y_i|^p\Big)^{1/p}, \qquad p \ge 1 \]
| \(p\) | norm | distance |
|---|---|---|
| \(1\) | \(\sum_i \lvert x_i - y_i \rvert\) | Manhattan (Hamming on bits) |
| \(2\) | \(\sqrt{\sum_i (x_i - y_i)^2}\) | Euclidean |
| \(\infty\) | \(\max_i \lvert x_i - y_i \rvert\) | Chebyshev / maximum |

The unit “circles”: diamond \(\subset\) circle \(\subset\) square, because \(\Vert\cdot\Vert_1 \ge \Vert\cdot\Vert_2 \ge \Vert\cdot\Vert_\infty\).
The choice of \(p\) decides what gets emphasized.
\(\ell_1\) — Manhattan
\(\ell_2\) — Euclidean
\(\ell_\infty\) — Chebyshev
Same two points, three different answers: \(A=(3,4)\), \(B=(1,1)\) gives \(5\), \(\sqrt{13}\), \(3\). The ranking of neighbors can change with \(p\) — that is the point, not a nuisance.
Cosine similarity — the angle, lengths divided out: \[ \cos(\mathbf{x},\mathbf{y}) = \frac{\mathbf{x}^T\mathbf{y}}{\Vert\mathbf{x}\Vert\,\Vert\mathbf{y}\Vert} \]
Euclidean distance — the length of the gap: \[ \Vert\mathbf{x} - \mathbf{y}\Vert_2 \]
Word-count vectors: document B \(=\) document A pasted twice. Cosine says identical (\(1\)); Euclidean says far (\(\Vert\mathbf{A}\Vert\)). For text we usually want the proportions, not the length → cosine.
Rule of thumb: ask “do I care about magnitude?” If not, cosine (or normalize to unit length first, after which Euclidean and cosine rank neighbors identically).
The units of each feature are part of the model. Wine dataset, 13 chemical measurements:
| feature | typical range | std. dev. |
|---|---|---|
hue |
0.5 – 1.7 | 0.23 |
alcohol |
11 – 15 | 0.81 |
magnesium |
70 – 160 | 14 |
proline |
280 – 1680 | 314 |
Raw Euclidean distance between two wines \(\approx |\Delta\,\texttt{proline}|\): proline alone carries \(99.7\%\) of the total variance. Twelve features are invisible.
Standardize — per feature, subtract the mean and divide by the standard deviation — and the nearest neighbors change. In the activity you will measure exactly how much (spoiler: 1-NN accuracy on the wine type goes from ~0.65 to ~1.0).
Two separate decisions, both yours: which metric, and which units. Cosine does not rescue you from bad units — the direction is still dominated by the big coordinate.
Bit vector as a code (every position matters): Hamming \(= \ell_1 =\) number of bit flips.
Bit vector as a set (which items are present): Hamming counts the set difference, and that misleads — 10 differing words out of thousands beats 5 disjoint words.
Jaccard normalizes by how much there was to agree on: \[ J_{sim} = \frac{|x \cap y|}{|x \cup y|}, \qquad J_{dist} = 1 - J_{sim} \ \text{ (a metric)} \]

| data | first choice |
|---|---|
| real measurements, comparable units | Euclidean (\(\ell_1\) if outliers) — standardize first |
| word counts, varying lengths | cosine |
| item sets / baskets | Jaccard |
| aligned binary codes | Hamming |
scikit-learnGoal: build Euclidean, Manhattan, cosine and Jaccard distance matrices on real data with numpy, verify them against sklearn / scipy, watch nearest neighbors change with the metric and with feature scaling — then two stretch lessons: the fit / transform pattern that every sklearn object shares, and a 10-minute dynamic time warping teaser (why plain distances fail on time-shifted series).
numpy, pandas, matplotlib, scipy, scikit-learn only.The activity notebook goes live on the day of the lecture. Colab is optional — you can also open it on GitHub and run it locally.
Full lecture notes: Distances and Time Series
sklearn API pattern used in the stretch section: Scikit-Learn appendix