Free preview

59 lessons

Essential Linear Algebra for ML

Free preview

Essential Linear Algebra for ML · 59 lessons

Master the linear algebra that powers modern ML

No surprise gaps

Actually remember it

Skip what you know

One subscription. All learning paths included.

Our content is best on a larger screen

Dot product and vector norm

Dot product using magnitude and angle

Explanation

So far, we've seen how to multiply a vector by a scalar to stretch or shrink it.

But can we multiply two vectors together?

It turns out there is a meaningful way to do this - though it's not the kind of multiplication we're used to.

The dot product (also called the scalar product) of two vectors v\vec{v} and w\vec{w} is defined as:

vw=vwcos(θ)\vec{v} \cdot \vec{w} = |\vec{v}| |\vec{w}| \cos(\theta)

where v|\vec{v}| and w|\vec{w}| is the length or magnitude of the vectors v|\vec{v}| and w|\vec{w}| and θ\theta is the angle (in degrees or radians) made between the two vectors when both of their "tails" are placed at the origin.

You might be wondering: why define multiplication this way? Why not just multiply vectors component by component, like we did for addition or scalar multiplication?

The answer is that the dot product captures something geometric - it measures how much two vectors "point in the same direction." The sign of the dot product indicates whether the vectors point in the same direction (positive), are perpendicular (zero), or point in opposite directions (negative).

Have a play around with the interactive visualisation below. Notice how the dot product changes based on the norms of the vectors and the angle between them.

1234−1−2−3−41234−1−2−3−4
v=[2.001.00]
w=[2.001.00]
θ=53.1
|v|=2.24,|w|=2.24
vw=3.00

The arc shows the smaller angle θ between v and w. This is the angle used in the dot product formula: vw=|v||w|cosθ


Note: When using the formula vw=vwcos(θ)\vec{v} \cdot \vec{w} = |\vec{v}||\vec{w}|\cos(\theta), it doesn’t matter whether we measure the smaller angle between the vectors or the larger reflex angle - the result will be the same. This is because the cosine function is symmetric around 180180^\circ, i.e. cos(θ)=cos(360θ)\cos(\theta) = \cos(360^\circ - \theta). The dot product depends only on the value of cos(θ)\cos(\theta), not on the specific way we measure the angle between the vectors.

ML Context

If two data points (represented as vectors) have a large, positive dot product, it means their features are similar and they point in roughly the same direction in “feature space” (the space where each axis represents a feature of the data). If the dot product is zero, the data points are unrelated (perpendicular), and if it’s negative, they are dissimilar or point in opposite directions. This way of measuring similarity is used throughout machine learning, from classification to clustering.

Example

Let v\vec{v} and w\vec{w} be vectors with v=7|\vec{v}| = 7 and w=3|\vec{w}| = \sqrt{3}, respectively.

Suppose:

  • v\vec{v} makes an angle of 2020^\circ with the positive xx-axis.
  • w\vec{w} sits at an angle of +30+30^\circ below the negative xx-axis.

Calculate vw\vec{v} \cdot \vec{w}. Round your answer to three decimal places.

Solution

To compute the dot product, vw\vec{v} \cdot \vec{w}, we use the formula:

vw=vwcos(θ)\vec{v} \cdot \vec{w} = |\vec{v}| |\vec{w}| \cos(\theta)

where θ\theta is the angle between the two vectors, measured when both are placed tail-to-tail at the origin.

Visualising the two vectors will help us calculate the angle θ\theta between them:

246−2−4−6246−2−4−6
v
w
20°
30°
θ

Let’s think through the geometry.

  • v\vec{v} is 2020^\circ above the positive xx-axis.
  • w\vec{w} is 3030^\circ below the negative xx-axis, which means it's at an angle of 180+30=210180^\circ + 30^\circ = 210^\circ from the positive xx-axis.

So the angle between the two vectors is:

θ=21020=190.\theta = 210^\circ - 20^\circ = 190^\circ.

Now, substitute into the formula:

vw=73cos(190)\vec{v} \cdot \vec{w} = 7\sqrt{3} \cos(190^\circ)

Making sure our calculator is set in degrees, we have:

vw=73cos(190)=11.940\vec{v} \cdot \vec{w} = 7\sqrt{3} \cdot \cos(190^\circ) = -11.940

Practice questions

3 questions

Let v\vec{v} and w\vec{w} be vectors with v=2|\vec{v}| = \sqrt{2} and w=3|\vec{w}| = 3, respectively, and let the angle between them be 7272^\circ.

Calculate vw\vec{v} \cdot \vec{w}. Round your answer to three decimal places.

Select the correct answer:

+ 2 more questions

Dot product using components

Explanation

There’s another, more practical way to compute the dot product between two vectors - by working directly with their components.

Suppose we have two vectors in R2\mathbb{R}^2:

v=[v1v2],w=[w1w2].\vec{v} = \begin{bmatrix} v_1 \\ v_2 \end{bmatrix}, \quad \vec{w} = \begin{bmatrix} w_1 \\ w_2 \end{bmatrix}.

Then their dot product is:

vw=v1w1+v2w2.\vec{v} \cdot \vec{w} = v_1w_1 + v_2w_2.

That is, we simply multiply the components element-wise and add them up.

This formula generalises naturally to higher dimensions. For two vectors in Rn\mathbb{R}^n:

v=[v1v2vn],w=[w1w2wn],\vec{v} = \begin{bmatrix} v_1 \\ v_2 \\ \vdots \\ v_n \end{bmatrix}, \quad \vec{w} = \begin{bmatrix} w_1 \\ w_2 \\ \vdots \\ w_n \end{bmatrix},

the dot product is:

vw=v1w1+v2w2++vnwn.\vec{v} \cdot \vec{w} = v_1w_1 + v_2w_2 + \dots + v_nw_n.

This is sometimes referred to as the inner product, especially in more abstract or general mathematical settings. In regular (Euclidean) space, the dot product and inner product are the same thing.

Example

Let v=[132]\vec{v} = \begin{bmatrix} 1 \\ 3 \\ -2 \end{bmatrix} and w=[222]\vec{w} = \begin{bmatrix} -2 \\ 2 \\ -2 \end{bmatrix}.

Calculate the dot product vw\vec{v} \cdot \vec{w}.

Solution

We use the formula for the dot product in R3\mathbb{R}^3:

vw=v1w1+v2w2+v3w3\vec{v} \cdot \vec{w} = v_1w_1 + v_2w_2 + v_3w_3

Substituting the values:

vw=(1)(2)+(3)(2)+(2)(2)=2+6+4=8.\begin{align*} \vec{v} \cdot \vec{w} &= (1)(-2) + (3)(2) + (-2)(-2) \\ &= -2 + 6 + 4 \\ &= 8. \end{align*}

Practice questions

3 questions

Let v=[112]\vec{v} = \begin{bmatrix} 1 \\ -1 \\ 2 \end{bmatrix} and w=[332]\vec{w} = \begin{bmatrix} -3 \\ -3 \\ -2 \end{bmatrix}.

Calculate vw\vec{v} \cdot \vec{w}.

Select the correct answer:

+ 2 more questions

Vector norms and unit vectors

Explanation

Earlier, we used the Pythagorean theorem to define the length (also called the magnitude or norm) of a vector in terms of its components.

Now that we’ve introduced the dot product, we can express the same idea in a more compact and general way.

Let’s start in two dimensions. For a vector:

v=[v1v2],\vec{v} = \begin{bmatrix} v_1 \\ v_2 \end{bmatrix},

we previously defined its length as:

v=v12+v22.|\vec{v}| = \sqrt{v_1^2 + v_2^2}.

But from the definition of the dot product, we also have:

vv=v12+v22.\vec{v} \cdot \vec{v} = v_1^2 + v_2^2.

So we can rewrite the length as:

v=vv.|\vec{v}| = \sqrt{\vec{v} \cdot \vec{v}}.

This formulation is useful because it expresses the length compactly using the dot product. For a vector in Rn\mathbb{R}^n:

v=[v1v2vn],\vec{v} = \begin{bmatrix} v_1 \\ v_2 \\ \vdots \\ v_n \end{bmatrix},

the length (or norm) is:

v=vv=v12+v22++vn2.||\vec{v}|| = \sqrt{\vec{v} \cdot \vec{v}} = \sqrt{v_1^2 + v_2^2 + \dots + v_n^2}.

We often refer to this as the vector norm.

Notation

Info

We will often use double bars, ||\cdot||, instead of absolute value bars |\cdot|, to match the standard notation for vector norms in higher dimensions. But nothing fundamental has changed - we’re still measuring how far the vector reaches from the origin.

Recall that normalising a vector means converting it into a unit vector - a vector that points in the same direction as the original vector but has length 1.1.

We do this by dividing the vector by its own norm:

v^=vv.\hat{\vec{v}} = \frac{\vec{v}}{||\vec{v}||}.

The little hat on top of v\vec{v} is standard notation for a unit vector - it tells us that the vector has been normalised.

To check that this really gives a unit vector, we take the norm of the normalised vector and confirm that it equals 11:

v^=vv=1vv=1.||\hat{\vec{v}}|| = \left\|\frac{\vec{v}}{||\vec{v}||} \right\| = \frac{1}{||\vec{v}||} \cdot ||\vec{v}|| = 1.

So v^\hat{\vec{v}} has length 11 and keeps the direction of v\vec{v} - exactly what we want from a normalised vector.

Example

Suppose we have a feature vector representing a data point in a machine learning problem:

v=[312]\vec{v} = \begin{bmatrix} -3 \\ -1 \\ 2 \end{bmatrix}

Calculate the norm of the feature vector, and normalise it (convert it to a unit vector).

Solution

We know that in 33-dimensions,

v=vv=v12+v22+v32=(3)2+(1)2+(2)2=9+1+4=14.\begin{align*} \|\vec{v}\| &= \sqrt{\vec{v} \cdot \vec{v}} \\ &= \sqrt{v_1^2 + v_2^2 + v_3^2} \\ &= \sqrt{(-3)^2 + (-1)^2 + (2)^2} \\ &= \sqrt{9 + 1 + 4} \\ &= \sqrt{14}. \end{align*}

Normalising the vector gives:

v^=vv=114[312].\begin{align*} \hat{\vec{v}} &= \frac{\vec{v}}{\|\vec{v}\|} \\ &= \frac{1}{\sqrt{14}} \begin{bmatrix} -3 \\ -1 \\ 2 \end{bmatrix}. \end{align*}

Converting vectors to unit length ensures that only the direction (the pattern of features) matters, not the overall magnitude. This means our comparisons are not affected by the scale or units of measurement used for each feature.

Practice questions

3 questions

Calculate the norm of the vector:

v=[14211].\vec{v} = \begin{bmatrix} 1 \\ 4 \\ 2 \\ -1 \\ -1 \end{bmatrix}.

Select the correct answer:

+ 2 more questions