The cosine similarity calculator will teach you all there is to know about the cosine similarity measure, which is widely used in machine learning and other fields of data science. Read on to discover what the cosine similarity is, what the formula for the cosine similarity is, whether the cosine similarity can be negative, and how to calculate the cosine similarity in Python.
How to use the cosine similarity calculator
Here's how to use this cosine similarity calculator:
- Select the length of the two vectors you want to compare (from 2 up to 8 components).
- Enter your vectors a⃗ and b⃗ into the calculator, one element at a time. The vectors will always have the same length — attach zeroes if your vectors differ in length.
- The cosine similarity SC (and derivative values, like the angle between the vectors, θ, and the cosine distance, DC) are displayed below the vector inputs.
- The calculations for finding the cosine similarity are shown below the results so that you may understand your specific result.
What is the cosine similarity?
The cosine similarity measure indicates how similar two vectors are using the cosine of the angle between them. It gives no information on the comparative magnitudes of the vectors. Cosine similarity is widely used in data analysis and data science, particularly in the field of natural language processing, where documents are represented as vectors of word frequencies.
The cosine similarity formula
The cosine similarity between two N-dimensional vectors a⃗ and b⃗, denoted SC(a⃗, b⃗), is defined as the cosine of the angle θ between the two vectors:
SC(a⃗, b⃗) = cosθ
However, we don't always know the angle θ. A more helpful formula can be derived from the dot product. The dot product of two vectors is defined as:
a⃗⋅b⃗ = ‖a⃗‖ ‖b⃗‖ cosθ
where ‖a⃗‖ is the magnitude (length) of the vector a⃗. Rearranging this equation gives us the working formula for the cosine similarity:
SC(a⃗, b⃗) = cosθ = (a⃗⋅b⃗) / (‖a⃗‖ ‖b⃗‖)
where the dot product and the magnitudes expand to:
- a⃗⋅b⃗ = a1b1 + a2b2 + … + aNbN
- ‖a⃗‖ = √(a1² + a2² + … + aN²)
- ‖b⃗‖ = √(b1² + b2² + … + bN²)
Can the cosine similarity be negative? Yes! Because it is the cosine of an angle, SC always lies in the range [−1, 1]. A value of 1 means the vectors point in exactly the same direction, 0 means they are orthogonal (unrelated), and −1 means they point in exactly opposite directions.
How do I calculate the cosine similarity?
To calculate the cosine similarity by hand, follow these steps:
- Compute the dot product a⃗⋅b⃗ by multiplying matching components and summing them.
- Compute the magnitude of each vector as the square root of the sum of its squared components.
- Divide the dot product by the product of the two magnitudes.
- Optionally, take the arccosine of the result to obtain the angle θ, and compute the cosine distance DC = 1 − SC.
An example of the cosine similarity
Let's compute the cosine similarity of the 6-dimensional vectors a⃗ = [3, 8, 7, 5, 2, 9] and b⃗ = [10, 8, 6, 6, 4, 5]:
- a⃗⋅b⃗ = 3×10 + 8×8 + 7×6 + 5×6 + 2×4 + 9×5 = 219
- ‖a⃗‖ = √(9 + 64 + 49 + 25 + 4 + 81) = √232 ≈ 15.2315
- ‖b⃗‖ = √(100 + 64 + 36 + 36 + 16 + 25) = √277 ≈ 16.6433
- SC = 219 / (15.2315 × 16.6433) ≈ 0.8639
The angle between the vectors is θ = arccos(0.8639) ≈ 30.24°, and the cosine distance is DC = 1 − 0.8639 ≈ 0.1361. A similarity close to 1 tells us these two vectors point in nearly the same direction.
How to calculate the cosine similarity with Python
You can calculate the cosine similarity with just a few lines of Python using NumPy:
import numpy as np
a = np.array([3, 8, 7, 5, 2, 9])
b = np.array([10, 8, 6, 6, 4, 5])
cos_sim = np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
print(cos_sim) # 0.8638...
Alternatively, scikit-learn offers a ready-made helper:
from sklearn.metrics.pairwise import cosine_similarity
cosine_similarity(, )
What is the cosine distance?
The cosine distance is a complementary measure defined as:
DC(a⃗, b⃗) = 1 − SC(a⃗, b⃗)
It ranges from 0 (identical direction, maximum similarity) to 2 (opposite directions, minimum similarity). Note that the cosine distance is not a true metric — it does not satisfy the triangle inequality — but it is a convenient dissimilarity score in clustering and recommendation systems.
Units, measurement systems, and currencies
Cosine similarity is scale- and unit-invariant: multiplying a vector by any positive constant does not change its direction, so SC, the angle θ, and the distance DC are all dimensionless. The optional unit selector only labels your component values and the magnitudes ‖a⃗‖ and ‖b⃗‖. You may pick a measurement system or a currency:
- Metric (SI): millimeters (mm), centimeters (cm), meters (m), kilometers (km), grams (g), kilograms (kg).
- US / Imperial: inches (in), feet (ft), yards (yd), miles (mi), ounces (oz), pounds (lb).
- Currencies: US dollar (USD), Russian ruble (RUB), euro (EUR), pound (GBP), and 16 more world currencies — handy when your vectors hold prices, budgets, or spending profiles.
FAQs
- Can the cosine similarity be negative?
- Yes. It equals the cosine of the angle between the vectors, so it ranges from −1 (opposite directions) through 0 (orthogonal) to 1 (same direction). Negative values appear whenever the angle exceeds 90°.
- What does a cosine similarity of 0 mean?
- The two vectors are orthogonal (perpendicular), meaning θ = 90°. In data science this indicates the two items share no common direction — they are considered unrelated.
- What is the difference between cosine similarity and Euclidean distance?
- Cosine similarity compares only the direction of two vectors and ignores their magnitude, while Euclidean distance measures the straight-line gap between their tips, which depends on magnitude. That is why cosine similarity is preferred for high-dimensional, magnitude-varying data such as text.
- Can I use cosine similarity on vectors of different lengths?
- The two vectors must have the same number of components. If they differ, pad the shorter one with zeroes so both have the same length.
- What if a vector is the zero vector?
- The cosine similarity is undefined for the zero vector because its magnitude is 0 and the formula would divide by zero. Enter a non-zero vector for both a⃗ and b⃗.