02.03 Lesson 3 of 12

54 min

Dot 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 · b from 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

Exercise

Compare vector alignment

Write a small utility that reports a dot product and cosine similarity for two feature vectors.

  1. Calculate the dot product of [1, 2] and [2, 1] by hand.
  2. Verify the result with np.dot.
  3. Calculate cosine similarity for [1, 2] and [2, 1].
  4. Compare [1, 2] with [10, 20] and explain why their cosine similarity is 1.
  5. Handle a zero vector explicitly rather than returning a misleading number.
Done when

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

2 links
  1. 01
    Dot products and duality | Chapter 9 Video

    Watch the single chapter for the geometric interpretation of dot products and projections.

  2. 02
    numpy.dot Article

    Read the one-dimensional array example and confirm the returned scalar.

Check your understanding

Self-test

2 questions
  1. Question 1

    What is the dot product of [1, 2] and [3, 4]?

  2. Question 2

    Why can cosine similarity be more useful than a raw dot product when comparing direction?

Completion check

Move on when

You can calculate a dot product and cosine similarity, and explain what their values imply geometrically.

Quick find

Search Michi

Type to search every stage, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels