Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 62 ยท Getting Started

Length, Distance and Unit Vectors

Measure vectors with the L1, L2 and max norms, compute Euclidean distance, normalize to unit vectors and use np.linalg.norm correctly with examples.

How long is a vector? How far apart are two points? These sound like geometry questions, but they appear everywhere in data work: nearest-neighbor search, clustering, error measurement and regularization all boil down to "how big is this vector?" The tool that answers them is the norm.

This lesson defines the three most common norms, shows how to compute the distance between two vectors, and explains how to normalize a vector into a unit vector that carries only direction. It builds on Vectors and prepares the ground for the dot product.

The Euclidean length (L2 norm)

For a vector in the plane, the Pythagorean theorem gives its length. The vector (3, 4) is the hypotenuse of a right triangle with legs 3 and 4, so its length is sqrt(3^2 + 4^2) = sqrt(25) = 5. In any dimension:

TEXT
||v|| = sqrt(v1^2 + v2^2 + ... + vn^2)

The double bars ||v|| mean "the norm of v". This is the L2 norm or Euclidean norm, the everyday straight-line length.

Python Length by hand and with NumPy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

np.linalg.norm is the function to remember. It handles any dimension and is careful about numerical details, so prefer it to hand-written square roots.

Other norms: L1 and max

The Euclidean norm is not the only reasonable idea of size. Two others are very common.

Three ways to measure size

L2 norm Straight-line length. Square, add, root.
L1 norm Add the absolute values. Like walking city blocks.
Max norm The largest absolute component.

np.linalg.norm picks one with its ord argument.

1 / 2
  • L1 norm (Manhattan norm): |v1| + |v2| + ... + |vn|. A taxi in a city grid cannot cut diagonally, so the distance from (0, 0) to (3, 4) is 3 + 4 = 7 blocks.
  • Max norm (L-infinity): max(|v1|, ..., |vn|). It only cares about the single biggest component.

All three obey the same sensible rules: the norm of the zero vector is 0, every other vector has a positive norm, ||c * v|| = |c| * ||v||, and the triangle inequality ||u + v|| <= ||u|| + ||v|| holds (a detour is never shorter than the direct route).

Python L1, L2 and max norms

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Where are they used? The L2 norm measures ordinary error and distance. The L1 norm appears in sparse models and robust statistics because it penalizes many small values less harshly than squaring. The max norm answers worst-case questions such as "what is the biggest single deviation?".

Distance between two vectors

The distance between two points is the length of the vector that joins them. Subtract, then take the norm:

TEXT
distance(a, b) = ||a - b||

The order does not matter because ||a - b|| = ||b - a||. With the L2 norm this is the Euclidean distance; with L1 it is the Manhattan distance.

Python Distance between two points

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last example introduces axis=1. When you pass a matrix, np.linalg.norm(M, axis=1) returns one norm per row, so a single call measures the distance to every shop. This pattern is the core of nearest-neighbor search. Without axis, NumPy treats a matrix as a different object and returns a single "Frobenius" number, which is rarely what you want for row distances.

Unit vectors and normalizing

A unit vector has length exactly 1. Unit vectors are pure direction: they say where to point, not how far to go. To normalize any non-zero vector, divide it by its own length:

TEXT
unit(v) = v / ||v||

For v = (3, 4) the length is 5, so the unit vector is (0.6, 0.8). Check: 0.6^2 + 0.8^2 = 0.36 + 0.64 = 1.

Python Normalizing vectors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

keepdims=True keeps the lengths as a column of shape (3, 1) so that the division broadcasts row by row. Without it you would get a shape error or, worse, divide the wrong way.

The zero vector has no direction, so normalizing it divides by zero. Real code should check if length > 0 or use a tiny safety value such as np.maximum(length, 1e-12).

The standard unit vectors along the axes, (1, 0) and (0, 1) in the plane, are called e1 and e2. Every vector is a combination of them, which is the idea behind a basis.

Why normalizing matters

Suppose one feature of your data is measured in thousands and another in fractions. Distances will be dominated by the large one. Scaling vectors to unit length (or scaling each feature) removes such effects. Text-search systems normalize document vectors so that long documents do not win just for being long; the next lesson shows the exact formula, called cosine similarity.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the L2 length of the vector (6, 8)?

    sqrt(6^2 + 8^2) = sqrt(36 + 64) = sqrt(100) = 10. The L1 length would be 14.
  2. 2. How do you compute the Euclidean distance between arrays a and b in NumPy?

    Subtract to get the vector between the points, then take its norm. Summing the differences could cancel positive and negative parts.
  3. 3. Which is the unit vector in the direction of (0, 5)?

    Divide by the length 5 to get (0, 1). It has length 1 and still points straight up.
  4. 4. What does np.linalg.norm(M, axis=1) return for a matrix M with 3 rows?

    The axis argument selects the direction to reduce along; axis=1 collapses each row into its norm.
  5. 5. Why can the zero vector not be normalized?

    Normalizing divides by the length. A zero length means division by zero, and the zero vector has no direction anyway.

Summary

  • The norm ||v|| measures the size of a vector; np.linalg.norm(v) computes the Euclidean (L2) norm by default.
  • L1 adds absolute values, L2 is the straight-line length, and the max norm takes the biggest absolute component; choose with ord.
  • The distance between two vectors is ||a - b||, and axis=1 computes it for many rows at once.
  • A unit vector has length 1; get one by dividing a non-zero vector by its norm.
  • Use keepdims=True when normalizing rows of a matrix, and guard against the zero vector.

Next up: combine vectors into a single number with The Dot Product.