Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

3.2. Norms

Drag the plot above to look at all five vectors from different angles. You should notice that all of the linear combinations of w\color{#d81b60}\vec w and r\color{#004d40}\vec r lie on the same plane! We’ll develop more precise terminology to describe this idea, once again, in Chapter 4.1.


3.2. Norms

Earlier in this section, we defined the norm of a vector v\vec v as:

v=v12+v22++vn2=i=1nvi2\lVert \vec v \rVert = \sqrt{v_1^2 + v_2^2 + \cdots + v_n^2} = \sqrt{\sum_{i=1}^n v_i^2}

Now that we know how to add and scale vectors, we should think about how the norm behaves under these operations.

Properties of the Norm

Properties 1 and 2 are intuitive enough:

  1. Property 1 states that it’s impossible for a vector to have a negative norm. To calculate the norm a vector, we sum the squares of each of the vector’s components. As long as each component viv_i is a real number, then vi20v_i^2 \geq 0, and so i=1nvi20\sum_{i=1}^n v_i^2 \geq 0. The square root of a non-negative number is always non-negative, so the norm of a vector is always non-negative. The only case in which i=1nvi2=0\sum_{i=1}^n v_i^2 = 0 is when each vi=0v_i = 0, so the only vector with a norm of 0 is the zero vector.

  2. Property 2 states that scaling a vector by a scalar scales its norm by the absolute value of the scalar. For instance, it’s saying that both 2v2 \vec v and 2v-2 \vec v should be double the length of v\vec v. See, at this point, if you can prove why this is the case.

We now revisit norms in detail, including geometric intuition and alternative norms.

The Triangle Inequality

Property 3 is a bit more interesting. As a reminder, it states that:

u+vu+v\lVert \vec u + \vec v \rVert \leq \lVert \vec u \rVert + \lVert \vec v \rVert

This is a famous inequality, generally known as the triangle inequality, and it comes up all the time in proofs. Intuitively, it says that the length of a sum of vectors cannot be greater than the sum of the lengths of the individual vectors – or, more philosophically, a sum cannot be more than its parts. It’s called the triangle inequality because it’s a generalization of the fact that in a triangle, the sum of the lengths of any two sides is greater than the length of the third side.

Above:

u=32+12=10\lVert {\color{orange}\vec u} \rVert = \sqrt{3^2 + 1^2} = \sqrt{10}
v=42+(6)2=52\lVert {\color{#3d81f6}\vec v} \rVert = \sqrt{4^2 + (-6)^2} = \sqrt{52}
u+v=72+(5)2=74\lVert {\color{orange}\vec u} + {\color{#3d81f6}\vec v} \rVert = \sqrt{7^2 + (-5)^2} = \sqrt{74}

And indeed, 748.6\sqrt{74} \approx 8.6 is less than 10+5210.4\sqrt{10} + \sqrt{52} \approx 10.4.

To prove that the triangle inequality holds in general, for any two vectors u,vRn\vec u, \vec v \in \mathbb{R}^n, we’ll need to wait until Chapter 3.3. We don’t currently have any way to expand the norm u+v\lVert \vec u + \vec v \rVert – but we’ll develop the tools to do so soon. Just keep it in mind for now.

Unit Vectors and the Norm Ball

It’s common to use unit vectors to describe directions. I’ll use the same example as in Activity 1, when this idea was first introduced. Consider the vector x=[125]\vec x = \begin{bmatrix} 12 \\ 5 \end{bmatrix}. Its norm is x=122+52=169=13\lVert \vec x \rVert = \sqrt{12^2 + 5^2} = \sqrt{169} = \boxed{13}. (You might remember the (5,12,13)(5, 12, 13) Pythagorean triple from high school algebra– but that’s not important.)

There are plenty of vectors that point in the same direction as x\vec x – any vector cxc \vec x for c>0c > 0 does. (If c<0c < 0, then the vector cxc \vec x points in the opposite direction of x\vec x.)

But among all those, the only one with a norm of 1 is 113x\frac{1}{\boxed{13}} \vec x. Property 2 of the norm tells us this.

Image produced in Jupyter

In general, if v\vec v is any vector, then:

vv\frac{\vec v}{\lVert \vec v \rVert}

is a unit vector in the same direction as v\vec v. Sometimes, we say that vv\frac{\vec v}{\lVert \vec v \rVert} is a normalized version of v\vec v.

Here’s where things get interesting. Let’s visualize a few vectors and their normalized versions:

u=[31]    uu=[310110]v=[66]    vv=[1212]w=[75]    ww=[774574]x=[125]    xx=[1213513]y=[16]    yy=[137637]\begin{aligned} \vec u &= \begin{bmatrix} 3 \\ 1 \end{bmatrix} \implies \frac{\vec u}{\lVert \vec u \rVert} = \begin{bmatrix} \frac{3}{\sqrt{10}} \\ \frac{1}{\sqrt{10}} \end{bmatrix} \\ \vec v &= \begin{bmatrix} -6 \\ -6 \end{bmatrix} \implies \frac{\vec v}{\lVert \vec v \rVert} = \begin{bmatrix} \frac{-1}{\sqrt{2}} \\ \frac{-1}{\sqrt{2}} \end{bmatrix} \\ \vec w &= \begin{bmatrix} 7 \\ -5 \end{bmatrix} \implies \frac{\vec w}{\lVert \vec w \rVert} = \begin{bmatrix} \frac{7}{\sqrt{74}} \\ \frac{-5}{\sqrt{74}} \end{bmatrix} \\ \vec x &= \begin{bmatrix} -12 \\ 5 \end{bmatrix} \implies \frac{\vec x}{\lVert \vec x \rVert} = \begin{bmatrix} \frac{-12}{13} \\ \frac{5}{13} \end{bmatrix} \\ \vec y &= \begin{bmatrix} -1 \\ -6 \end{bmatrix} \implies \frac{\vec y}{\lVert \vec y \rVert} = \begin{bmatrix} \frac{-1}{\sqrt{37}} \\ \frac{-6}{\sqrt{37}} \end{bmatrix} \\ \end{aligned}
Image produced in Jupyter

What do these vectors all have in common, other than being unit vectors? They all lie on a circle of radius 1, centered at (0,0)(0, 0)!

Image produced in Jupyter

The circle shown above is called the norm ball of radius 1 in R2\mathbb{R}^2. It shows the set of all vectors vR2\vec v \in \mathbb{R}^2 such that v=1\lVert \vec v \rVert = 1. Using set notation, we might say:

{v:v=1,vR2}\{\vec v : \lVert \vec v \rVert = 1, \vec v \in \mathbb{R}^2\}

That this looks like a circle is no coincidence. The condition v=1\lVert \vec v \rVert = 1 is equivalent to v12+v22=1\sqrt{v_1^2 + v_2^2} = 1. Squaring both sides, we get v12+v22=1v_1^2 + v_2^2 = 1. This is the equation of a circle with radius 1 centered at the origin.

In R3\mathbb{R}^3, the norm ball of radius 1 is a sphere, and in general, in Rn\mathbb{R}^n, the norm ball of radius 1 is an nn-dimensional sphere.

Other Norms

So far, we’ve only discussed one “norm” of a vector, sometimes called the L2L_{\color{orange}2} norm or Euclidean norm. In general, if vRn\vec v \in \mathbb{R}^n is one vector, v=[v1v2vn]\vec v = \begin{bmatrix} v_1 \\ v_2 \\ \vdots \\ v_n \end{bmatrix}, then its norm is:

v=v12+v22++vn2=i=1nvi2\lVert \vec v \rVert = \sqrt{v_1^{\color{orange}2} + v_2^{\color{orange}2} + \cdots + v_n^{\color{orange}2}} = \sqrt{\sum_{i=1}^n v_i^{\color{orange}2}}

This is, by far, the most common and most relevant norm, and in many linear algebra classes, it’s the only norm you’ll see. But in machine learning, a few other norms are relevant, too, so I’ll briefly discuss them here.

  • The L1L_1 or Manhattan norm of v\vec v is:

    v1=v1+v2++vn=i=1nvi\lVert \vec v \rVert_1 = |v_1| + |v_2| + \cdots + |v_n| = \sum_{i=1}^n |v_i|

    It’s called the Manhattan norm because it’s the distance you would travel if you walked from the origin to v\vec v in a grid of streets, where you can only move horizontally or vertically.

  • The LL_\infty or maximum norm of v\vec v is:

    v=maxivi\lVert \vec v \rVert_\infty = \max_{i} |v_i|

    This is largest absolute value of any component of v\vec v.

  • For any p1p \geq 1, the LpL_p norm of v\vec v is:

    vp=(i=1nvip)1p\lVert \vec v \rVert_p = \left( \sum_{i=1}^n |v_i|^p \right)^{\frac{1}{p}}

    Note that when p=2p = 2, this is the same as the L2L_2 norm. For other values of pp, this is a generalization. Something to think about: why is there an absolute value in the definition?

All of these norms measure the length of a vector, but in different ways. This might ring a bell: we saw very similar tradeoffs between squared and absolute losses in Chapter 1.

Believe it or not, all three of these norms satisfy the same “Three Properties” we discussed earlier.

Back to x=[125]\vec x = \begin{bmatrix} 12 \\ 5 \end{bmatrix}. What are the L2L_2, L1L_1, and LL_\infty norms of x\vec x?

Image produced in Jupyter

Here:

  • x2=122+52=144+25=169=13\lVert \vec x \rVert_2 = \sqrt{12^2 + 5^2} = \sqrt{144 + 25} = \sqrt{169} = 13

  • x1=12+5=12+5=17\lVert \vec x \rVert_1 = |12| + |5| = 12 + 5 = 17

  • x=max(12,5)=12\lVert \vec x \rVert_\infty = \max(|12|, |5|) = 12

Let’s revisit the idea of a norm ball. Using the standard L2L_2 norm, the norm ball in R2\mathbb{R}^2 is a circle. What does the norm ball look like for the L1L_1 and LL_\infty norms? Or LpL_p with an arbitrary pp?

Loading...

The L1L_1 norm ball looks like a diamond. Any vector with an L1L_1 norm of 1 will lie on the boundary of the ball. The LL_\infty norm ball looks like a square, and the L1.3L_{1.3} norm ball looks like a diamond with rounded corners.

This is not the last you’ll see of these norm balls – in particular, in future machine learning courses, you’ll see them again in the context of regularization, which is a technique for preventing overfitting in our models.

np.linalg.norm and Vectorization

It’s been a while since we’ve experimented with numpy. A few things:

  • As we’ve seen, arrays can be added element-wise by default.

  • Arrays can also be multiplied by scalars out-of-the-box, meaning that linear combinations of arrays (vectors) are easy to compute. The above two facts mean that array operations are vectorized: they are applied to each element of the array in parallel, without needing to use a for-loop.

  • To compute the (L2L_2) norm of an array (vector), we can use np.linalg.norm.

Suppose you didn’t know about np.linalg.norm. There’s another way to compute the norm of an array (vector), that doesn’t involve a for-loop. Follow the activity to discover it.

In general, we’ll want to avoid Python for-loops in our code when there are numpy-native alternatives, as these numpy functions are optimized to use C (the programming language) under the hood for speed and memory efficiency.