How to use it
- Paste vector A and vector B, for example
1, 2, 3and4, 5, 6. - Read the cosine similarity at the top; the distance, angle and the dot product and lengths are below.
How it works
- Dot product: A·B = Σ aᵢbᵢ.
- Lengths: ‖A‖ = √(Σ aᵢ²), ‖B‖ = √(Σ bᵢ²).
- Similarity: cos θ = A·B / (‖A‖ ‖B‖); distance = 1 − cos θ.
- The angle is computed from the unit vectors in a form that stays accurate near 0° and 180°.
Worked example
A = (1, 2, 3), B = (4, 5, 6). A·B = 4 + 10 + 18 = 32, ‖A‖ = √14 = 3.74166, ‖B‖ = √77 = 8.77496. The similarity is 32 / √1078 = 0.974632, the cosine distance is 0.0253682 and the angle is 12.9332° (0.225726 rad). The vectors point in nearly the same direction even though B is more than twice as long.
Tips and common mistakes
- Keep the components in the same order in both vectors; similarity compares position by position.
- Centring the data first (subtracting each vector’s mean) turns cosine similarity into the Pearson correlation.
- For very sparse vectors, most zeros contribute nothing; only positions where both are non-zero add to the dot product.