Cosine Similarity
Measures similarity between two vectors by the angle between them.
Formula
Variables
Example
Identical direction gives 1, orthogonal gives 0
How it works
Cosine similarity compares two vectors by looking at the angle between them rather than their raw size. It comes from the dot product formula, which equals the product of the vector lengths times the cosine of the angle between them, so dividing by the magnitudes isolates how aligned the vectors are. A value near 1 means the vectors point in a very similar direction, 0 means they are unrelated in direction, and negative values mean they point in opposite directions.
When to use it
Use cosine similarity when you care about pattern or direction more than magnitude, such as comparing text embeddings, document vectors, user preferences, or feature vectors in recommendation systems. It is especially useful when two items can have very different lengths but still represent similar content or behavior.
Worked example, step by step
Let A = (2, 3, 1) and B = (4, 1, 2) Step 1: Compute the dot product: A · B = (2)(4) + (3)(1) + (1)(2) = 8 + 3 + 2 = 13 Step 2: Compute the magnitude of A: ||A|| = sqrt(2^2 + 3^2 + 1^2) = sqrt(4 + 9 + 1) = sqrt(14) Step 3: Compute the magnitude of B: ||B|| = sqrt(4^2 + 1^2 + 2^2) = sqrt(16 + 1 + 4) = sqrt(21) Step 4: Plug into the formula: cos θ = (A · B) / (||A|| ||B||) = 13 / (sqrt(14) * sqrt(21)) Step 5: Simplify: cos θ = 13 / sqrt(294) ≈ 13 / 17.146 = 0.758 Final answer: cosine similarity ≈ 0.758
Common mistakes to avoid
- Using cosine similarity as a distance metric without converting it, since larger values mean more similar, not farther apart
- Forgetting to normalize by vector lengths, which makes the result depend on scale instead of direction
- Assuming a zero vector can be compared normally, even though its magnitude is 0 and the formula becomes undefined
Did You Know?
Cosine similarity ignores magnitude, making it ideal for text embeddings.
Share this formula
More in Data Science & ML
View allMean Squared Error (MSE)
BasicAverage of squared differences between predicted and actual values.
Root Mean Squared Error (RMSE)
BasicSquare root of MSE, in the same units as the target variable.
Mean Absolute Error (MAE)
BasicAverage of absolute differences between predictions and actual values.
Coefficient of Determination (R²)
IntermediateProportion of variance in the target explained by the model.
Classification Accuracy
BasicFraction of predictions that are correct.
Precision
IntermediateOf all positive predictions, the fraction that were correct.