liveBook · Manning

Cosine similarity

As explained in AI Recommender Systems

Cosine similarity is a measure of the angular similarity between two vectors, typically ranging from zero to one for nonnegative vectors.

Cosine similarity measures the angle between two vectors, ignoring their magnitude. Vectors pointing in the same direction score 1; the measure commonly ranges from 0 to 1 for nonnegative vectors. section 3.1.2

The calculation divides the dot product by both vector lengths. This makes cosine similarity useful when direction matters more than the amount of data represented by a vector. section 3.1.2

Recommendation systems apply cosine similarity in several retrieval pipelines:

  • Represent users and items in a sparse user-item matrix, transpose it so items become vectors, and compute item-item similarity. section 2.2.2
  • Use TF-IDF vectors built from titles and genres to retrieve content-similar items, including items without behavioral ratings. section 2.3.1
  • Compare embedding vectors to support similarity search in high-dimensional spaces. section 3.1.2

Movie retrieval

Given a seed movie, compute its similarity row, exclude the seed itself, sort scores in descending order, and return the top k movie identifiers with similarity values.

Similarity quality depends on representation. Sparse metadata can make many items appear identical—for example, movies sharing only the Drama genre—while richer descriptions provide more meaningful distinctions. section 2.3.1

For normalized embedding vectors, cosine similarity and dot product produce the same ranking. Using the cheaper dot product avoids repeated normalization when comparing millions of items. section 3.1.2

How AI Recommender Systems covers it

How deep it goes
Explains the angular formula, sparse and content-based implementations, top-k retrieval, metadata limitations, and normalized-vector optimization.
Examples
Uses movie watch histories, title-and-genre TF-IDF vectors, Drama-only matches, and embedding comparisons as concrete examples.
What it assumes
Assumes vectors, dot products, and matrix representations while demonstrating their use through movie ratings, metadata, and embeddings.
What you can do after
Build it from scratch with sparse matrices or content vectors, rank top-k candidates, and substitute dot products for normalized embeddings.
How it connects
Connects vector similarity with collaborative filtering, content-based recommendations, TF-IDF, embeddings, dot product, and similarity search.

Also explained in

Compare how 25 books explain cosine similarity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI Recommender Systems. The section links point to the parts of the book it draws on.