A vector is a list of numbers, like (3, 4). You can picture it as an arrow from the origin: 3 steps right and 4 steps up. In code it is simply a list.
Adding and scaling
- Add component by component: (3, 4) + (4, 3) = (7, 7). Arrow picture: put one arrow after the other.
- Scale by a number: 2 × (3, 4) = (6, 8), the same direction, twice as long.
Length
By Pythagoras, the length of (3, 4) is √(3² + 4²) = 5. In any number of dimensions, take the square root of the sum of squares.
The dot product
Multiply matching components and add: (3, 4) · (4, 3) = 12 + 12 = 24. The dot product connects to the angle θ between the arrows:
u · v = |u| × |v| × cos θ
so cos θ = (u · v) ÷ (|u| × |v|).
from math import sqrt, acos, degrees
u = [3, 4]
v = [4, 3]
print("u + v =", [a + b for a, b in zip(u, v)])
print("2u =", [2 * a for a in u])
dot = sum(a * b for a, b in zip(u, v))
length_u = sqrt(sum(a * a for a in u))
length_v = sqrt(sum(b * b for b in v))
print("dot =", dot, " |u| =", length_u, " |v| =", length_v)
print("angle =", round(degrees(acos(dot / (length_u * length_v))), 2), "degrees")- Dot product 0: the vectors are perpendicular.
- Positive: they point roughly the same way. Negative: roughly opposite ways.
Cosine similarity
Search engines and recommendation systems turn documents, songs or users into long vectors and compare them with cos θ: near 1 means "very similar", near 0 means "unrelated".
Common mistakes
- Multiplying two vectors component by component and calling it the dot product. The dot product is one number: you must add the products.
Quick check
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.