Dot Product of Vectors
The dot product of two vectors — what it computes, how to write it (np.dot, @, sum of products), and what it tells you about similarity.
The dot product is the building block of all matrix multiplication: every cell of A @ B is one dot product. On its own, it turns two lists of numbers into a single number — a total, a weighted score, or a measure of how similar two players are.
Definition
The dot product of two vectors
aandbof the same lengthnis the single numbera · b = a[0]*b[0] + a[1]*b[1] + ... + a[n-1]*b[n-1]— multiply matching positions, then add everything up.
- Vector — a 1D array, such as one player's stats.
- Same length — every value in
amust have a partner inb. - Single number (scalar) — unlike
a * b, the result is not an array. - Geometric meaning —
a · b = |a| × |b| × cos(θ), where θ is the angle between the vectors. If the dot product is 0, the vectors are perpendicular (orthogonal) — they share nothing in common.
Syntax
python
np.dot(a, b) # classic
a @ b # modern operator
np.sum(a * b) # the definition, written out
np.vdot(a, b) # flattens inputs first (rarely needed)| Function | Parameters | Returns |
|---|---|---|
np.dot(a, b) | two 1D arrays of equal length | a scalar |
a @ b | two 1D arrays of equal length | a scalar |
np.sum(a * b) | same | a scalar (element-wise multiply, then sum) |
np.outer(a, b) | two 1D arrays (any lengths) | a 2D table of every product a[i]*b[j] — not a dot product |
Example — three ways, one number
python
import numpy as np
runs = np.array([72, 12, 45])
weights = np.array([1, 25, 10])
print(np.dot(runs, weights))
print(runs @ weights)
print(np.sum(runs * weights))
print(72*1 + 12*25 + 45*10)text
822
822
822
822How it works: 72×1 + 12×25 + 45×10 = 72 + 300 + 450 = 822. Element-wise runs * weights would give the array [72 300 450]; the dot product adds it up.
Example — a vector dotted with itself, and outer product
python
v = np.array([1, 2, 3])
print(v @ v, np.dot(v, v))
print(np.outer(v, v))text
14 14
[[1 2 3]
[2 4 6]
[3 6 9]]v · v = 1 + 4 + 9 = 14 — the sum of squares. Its square root is the vector's length (the norm, next-but-one topic). np.outer is a different thing: every combination of products, returned as a matrix.
Example — orthogonal vectors and similarity
python
a = np.array([1, 0])
b = np.array([0, 1])
c = np.array([2, 0])
print(np.dot(a, b), np.dot(a, c))
u = np.array([3, 4]); w = np.array([4, 3])
cos = np.dot(u, w) / (np.linalg.norm(u) * np.linalg.norm(w))
print(round(float(cos), 3))text
0 2
0.96a and b point in perpendicular directions → dot product 0. a and c point the same way → positive. Dividing the dot product by both lengths gives the cosine similarity (from −1 to 1); 0.96 means u and w point in almost the same direction. Recommendation systems and search engines use exactly this to find "similar" items.
Comparison — a * b vs a @ b vs np.outer(a, b)
| Expression | Lengths | Result | For [1,2,3] and [1,2,3] |
|---|---|---|---|
a * b | must match | array, same length | [1 4 9] |
a @ b / np.dot(a, b) | must match | one number | 14 |
np.outer(a, b) | any | 2D array (len a, len b) | 3 × 3 table |
Common mistakes
- ❌ Different lengths:
np.dot([1, 2, 3], [1, 2])→ValueError: shapes (3,) and (2,) not aligned: 3 (dim 0) != 2 (dim 0). ✅ Vectors must be the same length. - ❌ Expecting
a * bto return a single number. ✅ Usea @ bornp.dot(a, b). - ❌ Transposing a 1D array to make a "column" (
a.Tdoes nothing for 1D). ✅ 1D vectors need no transposing for a dot product; usea.reshape(-1, 1)if you truly need a column.
Interview tip
Q: What does a dot product of zero mean? A: The two vectors are orthogonal (perpendicular). In data terms the features have no linear overlap — e.g. cosine similarity 0 means the two items are unrelated.
Key points to remember
- The dot product multiplies matching positions of two equal-length vectors and sums them into one number.
- np.dot(a, b), a @ b and np.sum(a * b) all give the same dot product for 1D arrays.
- a * b returns an array; a @ b returns a single number.
- v @ v is the sum of squares of v; its square root is the vector's length (norm).
- A dot product of 0 means the vectors are orthogonal (perpendicular).
- Cosine similarity = (a @ b) / (|a| |b|) measures how similar two vectors' directions are.
- np.outer(a, b) is not a dot product — it builds a table of every pairwise product.
Try it yourself
Dot product three ways
Cosine similarity between players
Practice: total bill with a dot product
Revision Flashcards
Tap a card to flip it and see the answer.
