Machine Learning & AI
Basic

Cosine Similarity

Measures the angle between two vectors; 1 means identical direction.

Formula

cos⁡θ=a⋅b∥a∥ ∥b∥\cos\theta = \dfrac{\mathbf{a}\cdot\mathbf{b}}{\lVert\mathbf{a}\rVert\,\lVert\mathbf{b}\rVert}

Variables

\mathbf{a},\mathbf{b}Vectors
\cdotDot product

Example

Identical vectors: cos = 1

How it works

Cosine similarity compares two vectors by checking how much they point in the same direction, ignoring their overall size. It does this by taking the dot product, which grows when matching components line up, and then dividing by the lengths of both vectors so the result depends on direction rather than magnitude. The value is 1 for perfectly aligned vectors, 0 for perpendicular vectors, and -1 for vectors pointing in opposite directions.

When to use it

Use cosine similarity when you care about orientation more than size, such as comparing documents represented as word-frequency vectors, matching user preferences, or measuring similarity between feature vectors in machine learning. It is especially useful when the scale of the vectors may vary a lot but the pattern of values matters.

Worked example, step by step

Let a = (2, 3) and b = (4, 1). Step 1: Find the dot product: a · b = (2)(4) + (3)(1) = 8 + 3 = 11. Step 2: Find the magnitudes: ||a|| = √(2^2 + 3^2) = √13, and ||b|| = √(4^2 + 1^2) = √17. Step 3: Substitute into the formula: cos θ = 11 / (√13 × √17). Step 4: Multiply the denominators: √13 × √17 = √221. Step 5: So cos θ = 11 / √221 ≈ 11 / 14.87 ≈ 0.74.

Common mistakes to avoid

  • Forgetting to divide by both vector magnitudes, which makes the result depend on scale
  • Using cosine similarity on a zero vector, where the denominator becomes 0
  • Confusing cosine similarity with Euclidean distance, which measures straight-line distance instead of directional alignment

Did You Know?

Cosine similarity powers semantic search — comparing text embeddings by direction, not length.

Share this formula

Reviewed & maintained by the FormulaWon editorial teamLast reviewed September 2026