What are vector norms and distance metrics?
A norm is a function that assigns a length to a vector. The L2 norm is ordinary straight-line length, √(x₁² + x₂² + …); the L1 norm adds up absolute values; and the L∞ norm takes the largest absolute value. Every norm defines a distance: the distance between a and b is the norm of a − b.
Machine learning uses norms and distances wherever it compares or limits vectors. k-nearest neighbors and k-means rely on distances between data points, retrieval systems rank embeddings by cosine similarity, and L1 and L2 penalties keep model weights small. Loss functions are norms too: mean squared error is based on L2 and mean absolute error on L1.
Key ideas
- A norm measures the length of a vector, and the distance between two vectors is the norm of their difference.
- L2 is straight-line length, L1 sums absolute values, and L∞ takes the largest absolute value.
- For any vector, L∞ ≤ L2 ≤ L1.
- The shape of the unit ball changes with p, from a diamond at p = 1 to a circle at p = 2 to a square as p → ∞.
- Cosine similarity compares direction only and ignores length.
- L1 regularization produces sparse weights, while L2 shrinks all weights without zeroing them.
Short takes on this topic
Common questions
Should I use cosine similarity or Euclidean distance?
Use cosine when only direction matters, as with text embeddings whose lengths vary for reasons unrelated to meaning. For unit-length vectors the two agree in ranking, because |a − b|² = 2 − 2·cos(θ). Euclidean distance is the better choice when magnitude carries information, such as physical measurements.
Why do distances become less useful in high dimensions?
As the number of dimensions grows, the distances from one point to all others tend to bunch together, so the nearest and farthest neighbors are almost equally far. Methods that rely on distance, such as k-NN, then lose contrast. Dimensionality reduction, feature selection, or learned embeddings usually help.
What is the Mahalanobis distance?
It is a distance that accounts for the scale and correlation of the data: d(x, y) = √((x − y)ᵀ Σ⁻¹ (x − y)), where Σ is the covariance matrix. If the features are uncorrelated with unit variance, it reduces to Euclidean distance. It is used in outlier detection and in Gaussian models.
Do features need to be scaled before computing distances?
Usually yes. A feature measured in thousands, such as income, will dominate one measured in single digits, such as rooms per house, in any L1 or L2 distance. Standardizing each feature to zero mean and unit variance gives each one an equal starting influence.
Related explainers
- Linear Algebra — Vectors, matrices, and the language of data
- Regularization — Dropout, L1, and L2 — keeping models honest
- K-Nearest Neighbors — Classification by proximity and distance metrics
- Embeddings — Turning words and categories into vectors
- Curse of Dimensionality — Why high-dimensional spaces break intuition and algorithms
- Feature Scaling — Why and how to normalize features for faster convergence
References
- Deisenroth, Faisal & Ong, "Mathematics for Machine Learning", ch. 3 Analytic Geometry
- Goodfellow, Bengio & Courville, "Deep Learning", section 2.5 Norms
- James, Witten, Hastie & Tibshirani, "An Introduction to Statistical Learning", ch. 6 Linear Model Selection and Regularization
- Tibshirani (1996), "Regression Shrinkage and Selection via the Lasso"
Last updated