Linear algebra, 10 min read

Dot product

One number that tells you how much two vectors point the same way.

  • Key idea
  • Definition
  • Intuition
  • Example
  • Watch out

The dot product takes two vectors and returns one number. It is easy to compute, but above all it can be read: the number tells you how much the vectors agree.

Try it by hand

Drag the tips of the vectors and watch three things: the sign of the number, the angle and the yellow shadow.

Playground: dot productdrag both tips
u · v = 6.00angle = 53°point the same way

The yellow segment is the shadow of v on the line of u.

Similarity of vectors
import numpy as np

u = np.array([3, 1])
v = np.array([1, 3])

dot = u @ v
cos = dot / (np.linalg.norm(u) * np.linalg.norm(v))
angle = np.degrees(np.arccos(cos))

print("dot product:", dot)
print("cosine similarity:", round(cos, 3))
print("angle in degrees:", round(angle, 1))

Why we need this in ML

Every neuron in a neural network does exactly this: a dot product of the inputs with the weights. Is the result positive? Then the input resembles what the neuron is looking for. When we get to neural networks, we will come back to this picture.

The dot product of two nonzero vectors came out as 0. What does that mean?