Skip to content
Glacius
Linear algebraConcept reference

Cosine similarity

Cosine similarity of two nonzero real vectors is their dot product divided by the product of their Euclidean lengths.

On this page 8 sections
  1. Overview
  2. For u=(5,0) and v=(3,4), the dot product is 15
  3. A closer look
  4. Check your understanding
  5. Check your understanding
  6. Key takeaway
  7. Sources & further reading
  8. Concept connections

01For u=(5,0) and v=(3,4), the dot product is 15#

For u=(5,0)u=(5,0) and v=(3,4)v=(3,4), the dot product is 15. Doubling vv doubles that score to 30, while the angle stays the same.

Two equal-scale panels compare u=(5,0) with v=(3,4), and the same u with 2v=(6,8). Both pairs make the same angle. The dot products are 15 and 30, but both cosine similarities are 0.6.Two equal-scale panels compare u=(5,0) with v=(3,4), and the same u with 2v=(6,8). Both pairs make the same angle. The dot products are 15 and 30, but both cosine similarities are 0.6.
Figure 1Two equal-scale panels compare u=(5,0) with v=(3,4), and the same u with 2v=(6,8). Both pairs make the same angle. The dot products are 15 and 30, but both cosine similarities are 0.6.
Link to this figure ↗Download SVGDownload PNG

Cosine similarity divides out both lengths. For u=(5,0)u=(5,0) and v=(3,4)v=(3,4), each length is 5, giving 15/25=0.615/25=0.6.

sim(u,v)=uvuv\operatorname{sim}(u,v)=\frac{u\cdot v}{\|u\|\,\|v\|}
Check your reasoning

For u=(0,5)u=(0,5) and v=(4,3)v=(4,3), find cosine similarity.

Show answer and explanation
0.6

The dot is 15 and the lengths are 5, so 15/25=0.615/25=0.6.

02A closer look#

The score is 1 for the same direction, 0 for perpendicular vectors, and −1 for opposite directions. Positive scaling leaves the score unchanged because it scales the dot product and the length together.

Check your reasoning

Cosine is requested for (0,0)(0,0) and (1,0)(1,0). What happens?

  1. AIt equals 0.
  2. BIt equals 1.
  3. CIt is undefined.
Show answer and explanation
It is undefined.

The zero vector makes the denominator zero.

03Check your understanding#

Check your reasoning

Cosine search uses q=(1,0)q=(1,0). Which scores higher: A=(3,4)A=(3,4) or B=(4,3)B=(4,3)?

  1. AA
  2. BB
  3. CThey tie.
Show answer and explanation
B

Both lengths are 5. Scores are 3/53/5 and 4/54/5.

04Check your understanding#

Check your reasoning

A learner divides dot product 8 by lengths 2+42+4. What should replace that sum?

  1. A242\cdot4
  2. B424-2
  3. C4/24/2
Show answer and explanation
242\cdot4

Cosine uses the product of the two lengths.

Key takeaway

Divide the dot product by both lengths; zero vectors are excluded.

  • Compute directional similarity for two nonzero vectors.

Sources & further reading

  1. [1]
  2. [2]

Reference this concept

Link to this page, a section, or an individual figure.

Glacius. “Cosine similarity.” Math behind ML. /learn/la-cosine