Free Cosine Similarity Calculator

Vector Dimension

Vector A

Vector B

Result

Enter vector components
to calculate cosine similarity

Cosine similarity is a metric that measures how similar two vectors are by calculating the cosine of the angle between them. It is widely used in machine learning, natural language processing, and information retrieval because it focuses on direction rather than magnitude. This cosine similarity calculator lets you quickly evaluate the similarity, the angle, and the cosine distance for any pair of vectors.

How to Use This Cosine Similarity Tool

  1. Choose the vector dimension (length) you wish to compare.
  2. Enter the components of vector a and vector b into the corresponding fields. The calculator automatically enforces equal length; if your vectors are of different lengths, simply add zeros to the shorter one.
  3. The results — cosine similarity SCS_C, angle θ\theta (in degrees or radians), and cosine distance DCD_C — are displayed instantly.
  4. A detailed computation log shows each step, from the dot product to the final division, so you can verify the calculation manually.

What Is Cosine Similarity?

Cosine similarity measures the similarity between two non-zero vectors by evaluating the cosine of the angle between them. Mathematically, for vectors a⃗\vec{a} and b⃗\vec{b}:

SC(a⃗,b⃗)=cos⁡(θ)=a⃗⋅b⃗∥a⃗∥∥b⃗∥S_C(\vec{a},\vec{b}) = \cos(\theta) = \frac{\vec{a} \cdot \vec{b}}{\|\vec{a}\| \|\vec{b}\|}

The result always lies in the interval [−1,1][-1, 1]:

  • SC=1S_C = 1 means the vectors are perfectly aligned (θ=0∘\theta = 0^\circ).
  • SC=0S_C = 0 means they are orthogonal (θ=90∘\theta = 90^\circ).
  • SC=−1S_C = -1 means they are exactly opposite (θ=180∘\theta = 180^\circ).

Because it does not consider the magnitude, two vectors can have a similarity of 1 even if their lengths differ dramatically. This property makes cosine similarity especially useful for comparing documents or images where the scale of features may vary but their relative importance remains the same.

Deriving the Cosine Similarity Formula

The formula above can be expressed directly from the dot product. For two vectors a⃗\vec{a} and b⃗\vec{b} with nn components:

a⃗⋅b⃗=∑i=1naibi\vec{a} \cdot \vec{b} = \sum_{i=1}^{n} a_i b_i ∥a⃗∥=∑i=1nai2,∥b⃗∥=∑i=1nbi2\|\vec{a}\| = \sqrt{\sum_{i=1}^{n} a_i^2}, \qquad \|\vec{b}\| = \sqrt{\sum_{i=1}^{n} b_i^2}

Thus:

SC(a⃗,b⃗)=∑i=1naibi∑i=1nai2∑i=1nbi2S_C(\vec{a},\vec{b}) = \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}}

This is the same formula used by the calculator to compute the cosine similarity and the associated angle between vectors.

Example: Computing Cosine Similarity

Let’s work through a concrete case. Consider:

a⃗=[1,5],b⃗=[−1,3]\vec{a} = [1, 5],\quad \vec{b} = [-1, 3]
  • Dot product: 1⋅(−1)+5⋅3=−1+15=141\cdot(-1) + 5\cdot3 = -1 + 15 = 14.
  • Magnitude of a⃗\vec{a}: 12+52=26≈5.099\sqrt{1^2 + 5^2} = \sqrt{26} \approx 5.099.
  • Magnitude of b⃗\vec{b}: (−1)2+32=10≈3.162\sqrt{(-1)^2 + 3^2} = \sqrt{10} \approx 3.162.
  • Cosine similarity: SC=145.099×3.162≈0.868S_C = \frac{14}{5.099 \times 3.162} \approx 0.868.

The positive value confirms that the vectors point in roughly the same direction (angle ≈29.8∘\approx 29.8^\circ).

Implementing Cosine Similarity in Python

In data science workflows, Python with NumPy is often used to compute cosine similarity. The implementation uses the dot product and vector norms:

import numpy as np

def cosine_similarity(a, b):
    dot_product = np.dot(a, b)
    norm_a = np.linalg.norm(a)
    norm_b = np.linalg.norm(b)
    return dot_product / (norm_a * norm_b)

This function can be used with any pair of equal‑length arrays and is identical to the calculation carried out by the tool.

Cosine Distance: How Different Are the Vectors?

The cosine distance is defined as the complement of the cosine similarity:

DC(a⃗,b⃗)=1−SC(a⃗,b⃗)D_C(\vec{a},\vec{b}) = 1 - S_C(\vec{a},\vec{b})

It ranges from 0 (identical direction) to 2 (opposite direction). While intuitive, cosine distance is not a true distance metric because it fails the triangle inequality. For some triples of vectors, DC(a⃗,c⃗)D_C(\vec{a},\vec{c}) can be larger than DC(a⃗,b⃗)+DC(b⃗,c⃗)D_C(\vec{a},\vec{b}) + D_C(\vec{b},\vec{c}), so it should be used with care in certain geometric algorithms.

Important Notes

  • Zero vectors: Cosine similarity is undefined for a vector that consists entirely of zeros, because its magnitude is zero, making the denominator in the formula zero.
  • Negative similarity: A negative value indicates that the angle between the vectors exceeds 90∘90^\circ, implying they are more dissimilar than similar. The minimum value −1-1 corresponds to perfectly opposite orientations.
  • Magnitude independence: Remember that the similarity only reflects directional alignment. Two vectors with very different lengths but the same direction will still receive a score of 1.

FAQ

1. What is cosine similarity?

Cosine similarity measures the similarity between two vectors by computing the cosine of the angle between them. It ranges from -1 to 1, where 1 means identical direction, 0 means orthogonality, and -1 means opposite direction. It disregards magnitude, making it useful for comparing documents or features where scale is irrelevant.

2. Can cosine similarity be negative?

Yes, cosine similarity can be negative when the angle between the vectors is greater than 90 degrees. A negative value indicates that the vectors are more dissimilar than similar, with -1 representing exactly opposite directions.

3. How do you calculate cosine similarity?

Calculate the dot product of the two vectors, divide it by the product of their magnitudes (norms). The formula is S_C = (a·b) / (||a|| ||b||). The calculator and the Python implementation (using NumPy) follow this exact process.

4. What is the difference between cosine similarity and cosine distance?

Cosine distance is the complement of cosine similarity: D_C = 1 - S_C. It ranges from 0 (same direction) to 2 (opposite direction). However, cosine distance is not a true distance metric because it does not satisfy the triangle inequality.

How to Use

  1. Select the dimension of your vectors (2 to 10) using the dropdown.
  2. Enter the components for Vector A and Vector B into the corresponding input fields.
  3. The cosine similarity, cosine distance, and angle between the vectors are calculated instantly with a step-by-step breakdown.