The Dot Product
Intuition
The dot product measures how much two vectors point in the same direction. Large and positive: they agree; zero: perpendicular; negative: they point away from each other. A linear model's prediction is exactly "how well does the input agree with the weights".
Precise explanation
For :
Geometrically , where is the angle between them. It is only defined when both vectors have the same length .
Properties: symmetric (), linear in each argument, and .
total = 0
for a, b in zip(u, v):
total += a * b
Worked example
, : . — perpendicular axes.
Figure (original): u·v = ‖u‖‖v‖cos θ — positive here because θ < 90°.
Common mistakes
- Using
zipwithout checking lengths:zipsilently truncates to the shorter vector. - Confusing the dot product (a number) with element-wise multiplication (a vector).
- Starting the accumulator from
u[0]*v[0]and then looping over all elements again.
Why this matters
Dot products are the core operation of linear regression, neural network layers, cosine similarity in NLP retrieval and attention scores in transformers.
References
- Mathematics for Machine Learning — Marc Peter Deisenroth, A. Aldo Faisal, Cheng Soon Ong (Cambridge University Press) (
S009)