02.03 Lesson 3 of 12
54 minDot Products & Similarity
Understand the dot product as both a weighted sum and a measure of alignment between two vectors.
By the end of this lesson
You will be able to
- Calculate
a · bfrom corresponding components. - Interpret positive, zero and negative dot products geometrically.
- Connect a dot product to a weighted sum.
- Explain cosine similarity and why it removes magnitude.
The dot product combines corresponding components
For two vectors of equal length, a · b = Σᵢ aᵢbᵢ. Multiply matching components and add the products. If w contains model weights and x contains features, w · x is a weighted sum. The weights determine how strongly each feature contributes to the score.
x = np.array([2.0, 5.0, 1.0])
w = np.array([0.5, 0.1, 3.0])
score = np.dot(x, w)
print(score) # 4.5 Alignment gives the geometric interpretation
The dot product also equals ||a|| ||b|| cos(θ). For nonzero vectors, a positive result indicates an acute angle, zero indicates perpendicular directions and a negative result indicates an obtuse angle. Cosine similarity divides the dot product by both magnitudes, so it compares orientation without letting vector length dominate. It is undefined when either vector has zero magnitude.
def cosine_similarity(a, b):
denominator = np.linalg.norm(a) * np.linalg.norm(b)
if denominator == 0:
raise ValueError("Cosine similarity needs nonzero vectors")
return np.dot(a, b) / denominator Apply what you learned
ExerciseCompare vector alignment
Write a small utility that reports a dot product and cosine similarity for two feature vectors.
- Calculate the dot product of
[1, 2]and[2, 1]by hand. - Verify the result with
np.dot. - Calculate cosine similarity for
[1, 2]and[2, 1]. - Compare
[1, 2]with[10, 20]and explain why their cosine similarity is 1. - Handle a zero vector explicitly rather than returning a misleading number.
Your code matches the hand calculation, returns a cosine similarity of 1 for vectors pointing in the same direction and gives a clear error for a zero vector.
Free references
Study resources
- 01 Dot products and duality | Chapter 9 Video
Watch the single chapter for the geometric interpretation of dot products and projections.
- 02
Check your understanding
Self-test
-
Question 1
What is the dot product of
[1, 2]and[3, 4]?1 × 3 + 2 × 4 = 11. -
Question 2
Why can cosine similarity be more useful than a raw dot product when comparing direction?
It divides by both vector magnitudes, so the comparison depends on angle rather than overall scale.
Completion check
Move on when
You can calculate a dot product and cosine similarity, and explain what their values imply geometrically.