Stop 03 · Linear Algebra & CalculusTopics · 4/9

Norms & Distance Measures

Part of the Machine Learning Roadmap

In short

KNN, k-means, recommendation and vector search all ask one question: which points are close? The answer depends entirely on how you measure distance, and picking the wrong ruler gives the wrong neighbours.

A norm measures the size of a vector: the L2 norm is ordinary straight-line length and the L1 norm adds absolute values. Distances like Euclidean, Manhattan and cosine similarity decide which points count as close in KNN, clustering and search.

The jaadu rides in an auto-rickshaw zig-zagging along a grid of city streets while a crow flies straight overhead to the same gate.

Think of it like this

Crow vs auto-rickshaw

From Connaught Place to India Gate, a crow flies straight across: that's Euclidean distance (L2). An auto has to follow the road grid, going so many blocks east and so many north: that's Manhattan distance (L1). Cosine similarity ignores how far you travel and only asks whether two people are heading in the same direction.

Watch it happen

Distance between a = (1, 2) and b = (4, 6)

dx = 3
0
dy = 4
1
L1 = 7
2
L2 = 5
3
cos = 0.99
4

1/4Subtract the points: the difference is 3 along x and 4 along y.

The big ideas

What you need to know

A norm is a vector's size

L2 norm is the usual length: square root of the sum of squares. L1 norm is the sum of absolute values.

Distance is norm of a difference

Euclidean distance between a and b is just the L2 norm of a minus b. Same idea for Manhattan with L1.

Cosine measures direction

Cosine similarity is a·b divided by the product of their lengths. 1 means same direction, 0 unrelated, -1 opposite.

Scale your features first

If salary is in rupees and age in years, salary swamps every distance. Standardise before using distance-based models.

See it in code

Walk through the code

python
1import numpy as np2from sklearn.metrics.pairwise import cosine_similarity3 4a = np.array([1.0, 2.0])5b = np.array([4.0, 6.0])6 7l2 = np.linalg.norm(a - b)          # 5.08l1 = np.linalg.norm(a - b, ord=1)   # 7.09cos = a @ b / (np.linalg.norm(a) * np.linalg.norm(b))10 11print(l2, l1, round(cos, 3))        # 5.0 7.0 0.99212print(cosine_similarity([a], [b]))  # [[0.99227788]]

1/4Two small vectors, like two customers described by two features.

The same two points, three ways of saying how close they are.

Save yourself the pain

Common mistakes

Oops

Running KNN or k-means on raw columns like income (₹) and age (years).

Do this instead

Scale with StandardScaler first so no single column dominates the distance just because its numbers are bigger.

Oops

Using Euclidean distance to compare documents of very different lengths.

Do this instead

Use cosine similarity for text and embeddings. It cares about which words appear, not how long the text is.

Your mission

Find the nearest flat

~20 min

Quick check

What is the L1 norm of the vector (3, -4)?

Go deeper

  1. 1Compute L1 and L2 norms of a vector by hand and with np.linalg.norm.
  2. 2Compare Euclidean distance and cosine similarity for two vectors pointing the same way but with different lengths.
  3. 3Note where norms appear again in Ridge (L2) and Lasso (L1) regularization.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge