Toolvito

Works on this device

Cosine Similarity Calculator

Find how closely two lists of numbers point the same way, or how alike two texts are by the words they use. You get the cosine similarity, the angle, the cosine distance and the working.

Enter your values

For two lists of numbers of the same length, such as two rows of a table or two embeddings.

Your values

Numbers separated by commas, spaces or new lines. You can paste a column from a spreadsheet.

Your values are worked out in this browser. They are not sent to a server.

Your result will show here

Fill in the values, then choose Calculate.

How to use this calculator

  1. Choose two vectors, two texts, or several vectors.
  2. Type or paste the numbers, separated by commas, spaces or new lines. Both vectors need the same number of values. For texts, paste the two texts or open text files.
  3. Choose Calculate.

The formula

cosine similarity = (A · B) ÷ (|A| × |B|), where A · B = a₁b₁ + a₂b₂ + … is the dot product and |A| = √(a₁² + a₂² + …) is the length of A. The angle is arccos of the similarity, and the cosine distance is 1 − similarity.

  • 1 means the vectors point the same way, 0 means they are at a right angle, and −1 means they point in opposite directions.
  • With no negative values (such as word counts) the similarity lies between 0 and 1.

How texts are compared

  • Each text is split into words at spaces and line breaks. Punctuation at the two ends of a word is taken off, so "Hello," is "hello". "don't" and "well-known" stay one word. Upper and lower case are the same word. Each Chinese character and Japanese kana is one word.
  • Each text becomes a vector with one entry for every word that appears in either text: how often it appears, or 1 and 0 if you choose "present or not".
  • You can leave out common English words such as the, a, of and and. There is no stemming, so "cat" and "cats" are different words.

Worked example

A = (1, 2, 3) and B = (4, 5, 6). A · B = 4 + 10 + 18 = 32. |A| = √14 = 3.74166 and |B| = √77 = 8.77496. The similarity is 32 ÷ (3.74166 × 8.77496) = 0.974632, and the angle is 12.9332°.

What it does not measure

Cosine similarity looks only at direction, not at size: (1, 2, 3) and (10, 20, 30) have a similarity of 1. For texts it does not see word order or meaning: "dog bites man" and "man bites dog" have a similarity of 1, and two words with the same meaning count as different words.

For the dot product, the angle and other operations on vectors, see the Vector Calculator.

Limits you should know

  • Up to 100,000 values in a vector, and up to 50 vectors in the table mode.
  • A vector of only zeros has no direction, so its cosine is not defined.
  • The text mode compares the words themselves. It does not use TF-IDF weights or meaning (embeddings); for embeddings, paste the numbers into the vector mode.

Related tools