Cosine Similarity
Measures the angle between two vectors; 1 means identical direction.
Formula
Variables
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
More in Machine Learning & AI
View allLinear Regression Model
BasicPredicts a continuous value as a weighted sum of input features plus a bias.
Gradient Descent Update
BasicIteratively moves parameters in the direction that most reduces the loss.
ReLU Activation
BasicRectified Linear Unit: outputs the input if positive, else zero.
Leaky ReLU
BasicA ReLU variant that lets a small gradient flow for negative inputs.
Tanh Activation
BasicSquashes input to the range (-1, 1); zero-centred activation.
Softmax
BasicTurns a vector of scores into a probability distribution that sums to 1.