Mathematics for AI / Linear Algebra
Measuring how aligned two vectors are.
Reviewed by Yuvaraj
The dot product is the most important arithmetic operation in machine learning that you can still do by hand. Whenever a system judges how similar two things are, a search query against a document, a user against a product they might like, one word's meaning against another, a dot product is doing the work underneath. It takes two vectors and collapses them into a single number that captures both how much they point the same way and how large they are. Understand this one operation deeply and a surprising amount of ML "similarity" stops looking like magic.
Given two vectors of the same length, multiply matching components and add the results:
Ask about this lesson, or about anything in AI. Answers cite the lessons they draw on.
Finished this lesson?
Mark it complete to earn XP, keep your streak, and schedule a review.
That is literally all the machine does, a tight multiply-and-add loop over the components, which is why hardware (CPUs and GPUs) is built to run it billions of times per second.
The exact same number can be written using lengths and the angle between the two vectors:
Here is the length (norm) of . These two formulas are equal: the algebraic form is how you compute the dot product, and the geometric form is what it means.
Because lengths are never negative, the sign of the dot product is entirely determined by :
| Dot product | Geometry | What it means |
|---|---|---|
| Orthogonal: the vectors share no direction | ||
| angle under | They point broadly the same way | |
| angle over | They point in opposing directions |
This is why the raw dot product works as a similarity score: bigger and more positive means "more aligned."
Projection answers a related question: how much of lies along the direction of ? The scalar projection (the length of 's shadow on ) is
and the vector projection puts that shadow back as a vector pointing along :
Projection is the geometric engine behind least-squares regression, Gram–Schmidt orthogonalization, and dropping data onto principal components in PCA.
Solve the geometric form for the angle term and you get cosine similarity:
Dividing by both lengths cancels magnitude and leaves only direction, giving a score from to . This is the backbone of semantic search: an embedding model turns text into vectors whose direction encodes meaning, so two passages about the same topic point almost the same way and score near . Vector databases rank retrieved results by exactly this number.
Take and .
Since is positive and the cosine is close to (the angle is about ), the two vectors are fairly similar in direction.
import numpy as np
a = np.array([2, 3])
b = np.array([4, 1])
dot = a @ b # 11
cos_sim = dot / (np.linalg.norm(a) * np.linalg.norm(b))
print(dot, round(float(cos_sim), 3)) # 11 0.74
Common mistakes
a * b multiplies component-by-component and returns a vector, while the dot product a @ b returns a single scalar.cosine_similarity, the practical API used across real ML retrieval pipelines.