Cosine Similarity
Cosine Similarity
A measure of how similar two pieces of data or text are, based on how closely their directions align.
In Simple Terms
Cosine similarity compares vectors (lists of numbers) by checking how closely their directions align. Converting text or products into vectors is a separate job, usually handled by an embedding model — cosine similarity only compares the directions of the resulting vectors. It isn't affected by the size or length of the data — it only measures how closely their directions match. AI search and recommendation features use this method on vectorized text or products, finding ones with similar directions to surface related content or items.
Behind the Name
The name combines "cosine," the trigonometric function, with "similarity" — how alike two things are. Cosine values range from 1 down to -1 depending on the angle between two directions, and this measure borrows that scale to show how closely two vectors point in the same direction.
Take a Closer Look!
Cosine similarity is a way to express, as a number, how similar two pieces of data are once they've been converted into a list of numbers (a vector) — using the angle between the two vectors' directions. It isn't affected by the size of the data; it only evaluates how close the two directions are, based on that angle.
The result ranges from 1, when the angle is a perfect match, to 0 for a right angle, down to -1 when the directions are exactly opposite.
A helpful way to picture this is comparing two pieces of text by turning the words that appear in them into vectors. Since this looks at word overlap, the vectors end up pointing in similar directions — and the result lands close to 1 — whenever the texts use similar proportions of words, regardless of how long each text is.
Put simply, its defining feature is that it can judge how closely two things' directions match while ignoring differences in the sheer amount of data.
This measure is used across many systems, including natural language processing and data analysis.
For example, when searching for web pages with similar meaning, instead of just counting word occurrences, systems use "embedding vectors" — numerical representations of a text's meaning generated by an AI model — and measure the angle between them with cosine similarity to find related information.