Cosine similarity calculator for two vectors, embeddings or texts

Paste two vectors, a list of embeddings or two texts. Get cosine similarity, angle, dot product, euclidean distance and norms, or a ranked list.

How it works

  1. Paste vector A and vector B as JSON arrays, or switch to texts
  2. Read the similarity, angle, distance and norms
  3. Paste a list of vectors to get a matrix, or a list plus one vector to rank it

The formula

To calculate cosine similarity, divide the dot product of two vectors by the product of their lengths: cos(A, B) = (A · B) / (|A| × |B|). The dot product multiplies matching numbers and adds them up, and the length of a vector is the square root of the sum of its squared numbers. The result is between -1 and 1: 1 means the vectors point the same way, 0 means they are at right angles, and -1 means they point in opposite directions. Cosine distance is 1 minus the similarity, and the angle is the arccosine of the similarity, shown in degrees. Because both vectors are divided by their length, scaling one of them does not change the result: [1, 2, 3] and [2, 4, 6] have a similarity of 1. This holds for very small and very large numbers too, such as [1e-200] against itself or [1e160] against itself, which both give 1; the dot product or distance can then show as too large, but the cosine does not.

A worked example

Take A = [1, 2, 3] and B = [4, 5, 6]. The dot product is 1×4 + 2×5 + 3×6 = 32. The length of A is the square root of 14, about 3.741657, and the length of B is the square root of 77, about 8.774964. So the cosine similarity is 32 / (3.741657 × 8.774964), which is about 0.974632, an angle of about 12.93 degrees. The euclidean distance, the straight-line gap between the two points, is the square root of 27, about 5.196152. When both vectors already have length 1, the dot product equals the cosine similarity, which is why systems that normalize their embeddings can use a plain dot product.

Lists, ranking and the vector formats it reads

A vector can be a JSON array, numbers separated by commas, spaces or semicolons, or the embedding field of an API response: an object with an embedding array, or a data list of objects that each have one. Paste two vectors for a single result. Paste a list of vectors in the first box and leave the second empty to get a matrix of every pair, up to 20 vectors. Paste a list in one box and one vector in the other to rank the list by cosine similarity, highest first, up to 1,000 vectors and 20,000 numbers each. The vectors in a comparison must have the same length, and a vector of all zeros has no direction, so its cosine is shown as undefined.

Text mode and what it is not

Text mode compares which words two texts share. Words are lower-cased letters and digits, and each text becomes a vector with one number per distinct word: its count, or its TF-IDF weight with idf = ln((1 + N) / (1 + df)) + 1, where N is the number of texts and df the number of texts containing the word. This is word overlap, not an embedding. It cannot tell that car and automobile are close, and two texts with no word in common score 0 however alike their meaning is. Use it to compare wording, and use real embeddings from a model to compare meaning. Paste one text per line in the first box and leave the second empty to get a matrix of lines.

Frequently asked questions

What is the formula for cosine similarity?

cos(A, B) = (A · B) / (|A| × |B|). For A = [1, 2, 3] and B = [4, 5, 6] the dot product is 32 and the lengths are 3.741657 and 8.774964, so the similarity is 0.974632.

What is a good cosine similarity score?

The scale runs from -1 to 1, and what counts as high depends on the model that made the vectors. Compare scores from the same model against each other, for example by ranking a list with one query vector, rather than against a fixed cut-off.

What is the difference between cosine similarity and euclidean distance?

Cosine similarity compares direction and ignores length, so [1, 2, 3] and [2, 4, 6] score 1. Euclidean distance is the straight-line gap between the points, which for those two vectors is 3.741657. Both are shown.

Why does it say the vectors have different lengths?

Cosine similarity needs the same number of dimensions in both vectors, such as 1,536 for each. Embeddings from different models, or truncated copies, usually differ in length and cannot be compared.

Why is the cosine similarity undefined?

A vector of all zeros, such as [0, 0, 0], has length 0, and dividing by 0 is undefined. Check where the vector came from. The dot product and euclidean distance are still shown.

Are my vectors or texts uploaded?

No. They are processed in your browser, in this tab, and no server receives them. Your mode, weighting and number of decimals are saved in this browser, the vectors and texts are not.