Free preview
59 lessons
Essential Linear Algebra for ML
Free preview
Essential Linear Algebra for ML · 59 lessons
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 using magnitude and angle
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 and is defined as:
where and is the length or magnitude of the vectors and and 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.
The arc shows the smaller angle
Note: When using the formula , 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 , i.e. . The dot product depends only on the value of , 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.
Let and be vectors with and , respectively.
Suppose:
Calculate . Round your answer to three decimal places.
Solution
To compute the dot product, , we use the formula:
where 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 between them:
Let’s think through the geometry.
So the angle between the two vectors is:
Now, substitute into the formula:
Making sure our calculator is set in degrees, we have:
Practice questions
3 questions
Let and be vectors with and , respectively, and let the angle between them be .
Calculate . Round your answer to three decimal places.
Select the correct answer:
+ 2 more questions
Dot product using components
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 :
Then their dot product is:
That is, we simply multiply the components element-wise and add them up.
This formula generalises naturally to higher dimensions. For two vectors in :
the dot product is:
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.
Let and .
Calculate the dot product .
Solution
We use the formula for the dot product in :
Substituting the values:
Practice questions
3 questions
Let and .
Calculate .
Select the correct answer:
+ 2 more questions
Vector norms and unit vectors
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:
we previously defined its length as:
But from the definition of the dot product, we also have:
So we can rewrite the length as:
This formulation is useful because it expresses the length compactly using the dot product. For a vector in :
the length (or norm) is:
We often refer to this as the vector norm.
Info
We will often use double bars, , instead of absolute value bars , 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
We do this by dividing the vector by its own norm:
The little hat on top of 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 :
So has length and keeps the direction of - exactly what we want from a normalised vector.
Suppose we have a feature vector representing a data point in a machine learning problem:
Calculate the norm of the feature vector, and normalise it (convert it to a unit vector).
Solution
We know that in -dimensions,
Normalising the vector gives:
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:
Select the correct answer:
+ 2 more questions