download

Dot product, cosine or L2: why do they pick different items?

Machine learning~12 minReviewed 29 Oct 2026Free · No sign-in

Question

A shop recommends products with embeddings. The shopper's vector is q = (2, 1). There are three products:

productvector
A(1, 1)
B(4, 2)
C(3, 5)

Rank the products three ways: by dot product with q (higher is closer), by cosine similarity (higher is closer) and by L2 distance (lower is closer). Do the three orders agree?

Then name the smallest change to the vectors that makes all three give the same order.

Show hints (3)
  1. "LENGTH OR DIRECTION"

    Dot product grows with a vector's length. Cosine only looks at the angle. L2 measures the straight-line gap between the two points.

  2. "SAME DIRECTION"

    B = (4, 2) is exactly 2 × q. Which score loves that? Which one says B is far away?

  3. "THE FIX"

    Scale every product vector to length 1. Then the dot product is the cosine times the length of q, and that length is the same for every product. What happens to L2?

Show checked explanation

They pick three different winners: dot product picks C, cosine picks B and L2 picks A. Dot product rewards long vectors, cosine ignores length, and L2 punishes points far from q. Scale every product vector to length 1, and all three give the same order: B, A, C. You don't need to normalize the query.

"SOLUTION · PYTHON"
import numpy as np

q = np.array([2.0, 1.0])  # the shopper
names = "ABC"
items = np.array([[1.0, 1.0], [4.0, 2.0], [3.0, 5.0]])  # three products


def orders(X):
    dot = X @ q
    cos = dot / (np.linalg.norm(X, axis=1) * np.linalg.norm(q))
    l2 = np.linalg.norm(X - q, axis=1)
    best_first = {"dot": -dot, "cosine": -cos, "L2": l2}
    return "  ".join(f"{m}: {''.join(names[i] for i in np.argsort(k))}"
                     for m, k in best_first.items())


print("raw items: ", orders(items))
unit_items = items / np.linalg.norm(items, axis=1, keepdims=True)
print("unit items:", orders(unit_items))
"OUTPUT"
raw items:  dot: CBA  cosine: BAC  L2: ABC
unit items: dot: BAC  cosine: BAC  L2: BAC

Code run on CPython 3.12 with NumPy 2.5 against the example above. Checked by an editor other than the author.

Walkthrough

Here are the raw scores for q = (2, 1):

productdotcosineL2
A (1, 1)30.9491.000
B (4, 2)101.0002.236
C (3, 5)110.8444.123

C wins on dot product because it is long. B points exactly the same way as q, so its cosine is 1. But B is twice as long as q, so L2 puts it 2.236 away. A is short and close to q, so L2 picks it.

One formula links them: q · x = |q| × |x| × cos. So a long product vector gets a boost on dot product.

Now divide each product vector by its length. Every |x| becomes 1, and two things happen:

  • The dot product becomes |q| × cos. |q| is the same for every product, so the order is the cosine order.
  • The squared L2 distance becomes |q|² + 1 - 2 × (q · x). The first two terms are the same for every product. So a bigger dot product means a smaller distance.

All three now give B, A, C, and you didn't touch q.

Which score is right? The one the model was trained with. A dot-product model may store something useful in the length, such as how popular an item is. Normalizing throws that away.

Common mistakes

  • Normalizing only the query. That divides every dot product by the same number, so the order stays C, B, A. Cosine needs each product's own length removed.
  • Switching the vector index from dot product to L2 or cosine to make it faster. On vectors that aren't normalized, that changes the results, not just the speed.
  • Normalizing by default because cosine "is better". If the length carries signal, you lose it. Compare recall on held-out data before and after.
  • Forgetting the zero vector. It has no direction, so cosine divides by zero. Filter such items out or handle them with a separate rule.

Follow-up questions

  • "Do you also need to normalize the query?" Not for the order. With unit product vectors, |q| multiplies every dot product by the same number and adds the same amount to every squared L2 distance. Normalize q when you need the scores themselves: real cosines from -1 to 1, a fixed cut-off, or scores you compare across queries.
  • "Your vector database only supports L2 distance. How do you search by cosine?" Normalize the product vectors to length 1 before you insert them. Then the nearest by L2 is the one with the highest cosine. If you normalize the query too, you can turn a distance d back into a cosine: cos = 1 - d² / 2.

What a strong answer covers

Computes the three orders and explains each winner by length versus direction. Uses q · x = |q| × |x| × cos to show that unit-length products make all three agree, and knows the query needs no normalizing for the order. Picks the score the model was trained with.

Open in interactive practice deck →

Original OfferVibe question. Topic idea from Inside the Machine Learning Interview (Peng Shao, 2023); text, examples and code are our own.
© 2026 OfferVibe · Text CC BY-NC-SA 4.0 · code MITFree to share and adapt with credit, not for commercial use. Code samples are free for any use.