Advertisement

Cosine similarity calculator

Enter two vectors in matching component order. Their lengths must match, and neither may be the zero vector.

Calculator

Cosine similarity
0.70710678
Cosine distance, 1 - similarity
0.29289322
Included angle
45
Magnitude of A
1.00000000
Magnitude of B
1.41421356

Advertisement

How this calculator works

Cosine similarity measures alignment, independently of positive rescaling. It is 1 for equal directions, 0 for orthogonal vectors and -1 for opposite directions. Components may be negative, so the result is not a percentage. The computation normalises each vector before taking its dot product to reduce unnecessary overflow.

Formula

s = (A dot B)/(|A||B|); cosine distance = 1 - s; angle = arccos(s).

Worked example

For A = (1, 0) and B = (1, 1), the similarity is 1/sqrt(2), about 0.7071067812, and the angle is 45 degrees. Opposite unit vectors have similarity -1.

Advertisement

Frequently asked questions

Why reject a zero vector?

A zero vector has no direction, and the defining denominator is zero.

Does similarity imply equal magnitudes?

No. Vectors (1, 2) and (10, 20) have similarity 1 despite their different lengths.

Is cosine distance a metric?

In general 1 minus cosine similarity does not satisfy the triangle inequality. Angular distance is a different construction.

Sources

Advertisement