liveBook · Manning

Cosine distance

As explained in Build an Advanced RAG Application (From Scratch)

Cosine distance measures vector dissimilarity based on their angular separation, commonly defined as one minus cosine similarity.

Cosine distance measures vector dissimilarity through angular separation; cosine similarity is commonly expressed as one minus this distance. section 3.4 section 4.0

In semantic search, encode a query and documents as embeddings, calculate cosine distance for each pair, and identify the smallest distance as the closest match. section 3.4

Ranking encoded sentences

Sentence Transformers encodes six example sentences into vectors with shape (6, 384). SciPy computes each cosine distance; converting scores with 1 - distance yields similarity. For the query “Machine learning,” the matching sentence scores 0.6723 and ranks first. section 3.4

Figure 3.4 A graphical demonstration of cosine similarity using two dimensions ("pet" and "furry"). Three words are drawn as vectors into the coordinate system. θ 1 denotes the angle between bird and dog, θ 2 depicts the angle between dog and cat, respectively. The smaller angle θ 2 illustrates a higher similarity of dogs with cats compared to birds.’

A two-dimensional vector diagram relates smaller angular separation to higher cosine similarity. section 3.4

How Build an Advanced RAG Application (From Scratch) covers it

How deep it goes
Explains angular intuition and demonstrates SciPy computation, but defers detailed metric mathematics and large-scale indexing.
Examples
Uses cat, dog, bird, and machine-learning sentences plus a two-dimensional pet-and-furry vector diagram.
What it assumes
Assumes basic vectors and embeddings, while demonstrating encoder loading and embedding generation with Sentence Transformers.
What you can do after
Apply it via SciPy to rank encoded query-document pairs and select the nearest semantic match.
How it connects
Connects embeddings, semantic search, Sentence Transformers, cosine similarity, Euclidean distance, bi-encoders, and FAISS.

Also explained in

Compare how 9 books explain cosine distance →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build an Advanced RAG Application (From Scratch). The section links point to the parts of the book it draws on.