Cosine Similarity Calculator

Cosine Similarity Calculator cos θ

Enter two vectors as comma or space separated numbers of the same dimension (2D like 1 0, 3D like 1 2 3, or higher). You get the cosine similarity, the angle between them, the dot product and both magnitudes — plus a live plane diagram for 2D vectors, the working and the NumPy code.

Examples:

This free cosine similarity calculator measures how closely two vectors point in the same direction, ignoring their length. Enter two vectors of the same dimension above — 2D, 3D, or higher — and you instantly get the cosine similarity, the angle between them, the dot product and both magnitudes, along with a plane diagram for 2D inputs, the full working, and the matching NumPy code. Cosine similarity is the workhorse metric behind semantic search, recommendation systems and embedding comparisons, and this tool computes it exactly the way those systems do.

What is cosine similarity?

Cosine similarity is a measure of how similar the directions of two vectors are. It is the cosine of the angle between them, computed from the dot product divided by the product of their magnitudes. The key idea is that cosine similarity looks only at orientation, not at size: two vectors that point the same way score 1 whether one is ten times longer than the other or not. This makes it ideal for comparing things like documents, user profiles or word embeddings, where the direction of a vector carries the meaning and its raw length is often just an artefact of scale.

Because it is a cosine, the value always lands between −1 and 1. A score of 1 means the vectors are perfectly aligned (angle of 0 degrees), 0 means they are perpendicular and share nothing in common, and −1 means they point in exactly opposite directions (angle of 180 degrees). Everything in between is a smooth gradient of similarity. That bounded, interpretable range is a big part of why the metric is so popular.

The metric in one line

Cosine similarity of a and b = (a dotted with b) divided by (length of a times length of b). It equals the cosine of the angle between the two vectors, so it always sits between −1 and 1.

How to use the cosine similarity calculator

  1. Type Vector a and Vector b as comma or space separated numbers. You can use two components (1 0), three (1 2 3), or as many as you like — embeddings often have hundreds.
  2. Make sure both vectors have the same dimension. Cosine similarity pairs up matching components, so a mismatch is undefined and the calculator will say so.
  3. Press Compute. The highlighted tile shows the cosine similarity, and the surrounding tiles give the angle in degrees, the dot product and both magnitudes.
  4. For 2D inputs, read the plane diagram: vector a is indigo and vector b is violet, drawn from a shared origin so you can see the angle between them directly.
  5. Use the Decimals selector to control precision and Copy result to grab a one-line summary. The example chips load classic cases: parallel, orthogonal and opposite vectors.

The result updates live as you type, so you can nudge a component and watch the similarity move.

A worked example

Take a = [1, 2, 3] and b = [2, 4, 6]. First the dot product: 1×2 + 2×4 + 3×6 = 2 + 8 + 18 = 28. Next the magnitudes: |a| = √(1 + 4 + 9) = √14 ≈ 3.742 and |b| = √(4 + 16 + 36) = √56 ≈ 7.483. Their product is √14 × √56 = √784 = 28. So the cosine similarity is 28 / 28 = 1, and the angle is acos(1) = 0°. That is exactly right: b is just a scaled by 2, so the two vectors point in precisely the same direction. This is the Parallel example chip, and it demonstrates the defining property of the metric — scaling a vector does not change its cosine similarity with another.

Contrast that with a = [1, 0] and b = [0, 1]. The dot product is 1×0 + 0×1 = 0, so the cosine similarity is 0 / (1 × 1) = 0 and the angle is acos(0) = 90°. The vectors are orthogonal — they share no direction at all.

The formula

The formula for cosine similarity between two vectors a and b is:

\[ \cos\theta = \frac{a\cdot b}{\lVert a\rVert\,\lVert b\rVert} = \frac{\sum_{i=1}^{n} a_i b_i}{\sqrt{\sum_{i=1}^{n} a_i^2}\;\sqrt{\sum_{i=1}^{n} b_i^2}} \]

Reading it piece by piece: the numerator a·b = Σ aibi is the dot product, which multiplies matching components and sums them. The denominator is the product of the two Euclidean norms (magnitudes), where |a| = √(Σ ai²). Dividing the dot product by the magnitudes cancels out the lengths and leaves pure direction — the cosine of the angle. To recover the angle itself, take the inverse cosine: θ = acos(cosθ). The calculator clamps the ratio to the range −1 to 1 before calling acos, which guards against tiny floating-point overshoots that would otherwise return an invalid result.

The range and how to interpret it

Cosine similarity always falls in the closed interval [−1, 1]. Here is how to read the value:

  • 1 — the vectors are identical in direction (0° apart). Maximum similarity. In a search engine this is the perfect match.
  • Between 0 and 1 — the vectors point in broadly the same direction; the closer to 1, the more aligned. Most real-world similar items land here, often in the 0.7 to 0.95 band for related embeddings.
  • 0 — the vectors are orthogonal (90° apart) and share no directional component. In text analysis this typically means two documents have no meaningful overlap.
  • Between −1 and 0 — the vectors point somewhat opposite; the components tend to disagree.
  • −1 — the vectors are exact opposites (180° apart). Maximum dissimilarity.

One important caveat: when your data is non-negative — as with raw word counts or TF-IDF vectors, where every component is zero or positive — cosine similarity can never be negative. It ranges only from 0 to 1, because two vectors in the positive orthant can be at most 90 degrees apart. Negative similarities only appear when vectors are allowed to have negative components, which is common for learned embeddings.

Cosine similarity vs Euclidean distance

Both cosine similarity and Euclidean distance measure how close two vectors are, but they answer different questions. Euclidean distance measures the straight-line gap between the tips of the vectors — it cares about magnitude. Cosine similarity measures the angle between them — it ignores magnitude entirely. Consider the document [1, 1] and a document twice as long with the same word proportions, [2, 2]. Their Euclidean distance is √2 ≈ 1.41, suggesting they are somewhat different, but their cosine similarity is exactly 1, correctly reporting that they are about the same topic. When the length of a vector is a nuisance rather than a signal — a long document is not more relevant just because it has more words — cosine similarity is usually the better choice.

There is a tidy relationship between the two when vectors are normalised to unit length: minimising Euclidean distance is then equivalent to maximising cosine similarity. That is why many vector databases store L2-normalised embeddings and let you use either metric interchangeably. If you are not normalising, though, the two can rank results quite differently, and picking the right one matters.

Cosine similarity in Python with NumPy

In practice you compute cosine similarity in code, and NumPy makes it a one-liner:

import numpy as np
a = np.array([1, 2, 3])
b = np.array([2, 4, 6])
cos = np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
print(cos) # 1.0
# angle in degrees:
print(np.degrees(np.arccos(np.clip(cos, -1, 1)))) # 0.0

The core expression np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b)) is the formula translated directly into code, and it returns the same value this calculator does. The np.clip(cos, -1, 1) inside the angle line plays the same protective role as the clamp inside this tool — without it, floating-point rounding can push the cosine a hair past 1 and make arccos return NaN. For many pairs at once, scikit-learn offers sklearn.metrics.pairwise.cosine_similarity, which computes a full matrix of similarities efficiently.

Where cosine similarity shows up in machine learning

Cosine similarity is everywhere in modern ML because so much of it runs on embeddings — dense vectors that encode the meaning of words, sentences, images, products or users. Comparing two embeddings almost always means taking their cosine similarity.

In semantic search and retrieval, a query is embedded into a vector and the system returns the stored documents whose embeddings have the highest cosine similarity to it; this is the retrieval step behind RAG (retrieval-augmented generation) pipelines. In recommendation systems, items or users are represented as vectors and cosine similarity finds the nearest neighbours — the movies most like the one you just watched. In natural language processing, cosine similarity between word vectors reveals analogies and clusters of related terms, and it is the standard way to score how close two sentences are in meaning. It also appears inside attention mechanisms: scaled dot-product attention compares queries and keys with a dot product that, once the vectors are normalised, behaves just like cosine similarity to decide how much each token should attend to every other. Whenever a model needs to ask "how alike are these two things?", cosine similarity is usually the answer.

Common mistakes to avoid
Forgetting to divide by the magnitudes — that gives you the raw dot product, not cosine similarity, and the value is no longer bounded to [−1, 1]. Comparing vectors of different dimensions, which is undefined. Feeding in a zero vector, which has no direction and makes the metric undefined (the calculator flags this). And expecting negative scores from non-negative data such as word counts, where cosine similarity can only range from 0 to 1.

Frequently asked questions

What is a good cosine similarity score?

It depends on the data, but higher is more similar. A value of 1 is a perfect directional match and 0 means no shared direction. For text or embedding comparisons, scores above roughly 0.7 to 0.8 usually indicate strong similarity, while values near 0 indicate unrelated items. Always calibrate against examples from your own dataset rather than a fixed universal threshold.

Can cosine similarity be negative?

Yes, if the vectors have negative components. A negative score means the vectors point in partly opposite directions, and −1 means they are exact opposites. However, when every component is non-negative — as with raw counts or TF-IDF — cosine similarity ranges only from 0 to 1 and can never be negative.

What is the difference between cosine similarity and cosine distance?

Cosine distance is simply 1 − cosine similarity. Similarity of 1 corresponds to a distance of 0, and orthogonal vectors have a similarity of 0 and a distance of 1. Distance is convenient when you want smaller numbers to mean closer, as in clustering and nearest-neighbour search.

Do the vectors need to be the same length?

They must have the same number of components (the same dimension), because the dot product pairs them up one by one. Their magnitudes, however, do not need to match at all — ignoring magnitude is the whole point of cosine similarity.

Does this calculator match NumPy?

Yes. The cosine similarity, dot product and magnitudes use the same definitions as numpy.dot and numpy.linalg.norm, so np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b)) returns the same value shown here.

Related calculators

Explore the full linear algebra calculators hub, or dig deeper with the dot product calculator and the vector calculator.