liveBook · Manning

Cosine similarity

Cosine similarity measures the directional alignment of two nonzero vectors as the cosine of the angle between them, equal to their dot product divided by the product of their magnitudes.

How 25 Manning books explain it

Natural Language Processing in Action, Second Edition

Cosine similarity measures the angular similarity between two nonzero vectors by dividing their dot product by the product of their magnitudes.

How it is used
Uses cosine similarity to rank text matches and compare embeddings across sentences.
How deep it goes
Derives the normalized dot product, interprets its range, and implements pairwise comparisons.
Examples
Compares an algorithmic-bias question with corpus sentences and plots similarities between embedded passages.
What you can do after
Build it from scratch in Python and use normalized embeddings to calculate a pairwise similarity matrix.

Read how Natural Language Processing in Action, Second Edition explains cosine similarity →

Rearchitecting LLMs

Cosine similarity measures the cosine of the angle between two vectors, reflecting their directional alignment independently of magnitude.

How it is used
Ranks transformer blocks for task-specific depth pruning by comparing input and output activations.
How deep it goes
Explains directional interpretation and implements masked token-level scoring, aggregation, and block selection.
Examples
Compares three sentence embeddings and ranks Qwen3-0.6B blocks using WinoGrande and WikiText calibration.
What you can do after
Apply it via PyTorch to calculate similarities and select candidate transformer blocks for pruning.

Read how Rearchitecting LLMs explains cosine similarity →

AI-Powered Search

Cosine similarity measures the angular similarity between two vectors by dividing their dot product by the product of their magnitudes.

How it is used
Uses cosine similarity to rank query-document matches and compare semantic embeddings.
How deep it goes
Explains magnitude normalization and implements query-document scoring and pairwise comparison of normalized embeddings.
Examples
Compares apple-juice query and document vectors, then ranks similarities among four sentence embeddings.
What you can do after
Apply it via NumPy and sentence-transformers to score query-document vectors and compare normalized embeddings.

Read how AI-Powered Search explains cosine similarity →

Build AI-Enhanced Web Apps

Cosine similarity is a metric that measures the cosine of the angle between two vectors, with larger values indicating greater directional similarity.

How it is used
Uses cosine similarity to rank knowledge-base matches and check generated responses against expected outputs.
How deep it goes
Defines the vector-angle score and demonstrates comparison loops and threshold assertions without implementing the cosine function.
Examples
Compares an IT support password query with stored questions and tests a mocked response about AI’s influence against expected wording.
What you can do after
Apply it via string-comparison for a threshold test, or use a cosineSimilarity function to rank stored embeddings.

Read how Build AI-Enhanced Web Apps explains cosine similarity →

Human-in-the-Loop Machine Learning: Active learning and annotation for human-centered AI

Cosine similarity measures the similarity between nonzero vectors using the cosine of the angle between them.

How it is used
Uses cosine similarity to cluster high-dimensional data for diversity sampling and compare training with unlabeled data for representative sampling.
How deep it goes
Defines the normalized dot product and describes iterative clustering, while relying on PyTorch’s native function rather than detailing its implementation.
Examples
Illustrates angular similarity with stars and compares clusters of news headlines with potentially misleading pixel-based image clusters.
What you can do after
Apply it via PyTorch to cluster data and select items for annotation.

Read how Human-in-the-Loop Machine Learning: Active learning and annotation for human-centered AI explains cosine similarity →

Math and Architectures of Deep Learning

A dot product computed on unit-length vectors so that the score is insensitive to vector length.

How it is used
Uses normalized vector direction to compare TF-IDF document representations during retrieval.
How deep it goes
Explains normalization, angular geometry, and retrieval limitations, then connects the limitation to latent semantic analysis.
Examples
Uses TF-IDF document vectors and the d5–d6 gun-versus-violence mismatch to expose direct-overlap limits.
What you can do after
Apply cosine similarity to compare weighted document vectors and recognize when direct term matching misses latent semantic relations.

Read how Math and Architectures of Deep Learning explains cosine similarity →

Graph Algorithms for Data Science

Cosine similarity is a measure of the angular resemblance between two vectors, computed from the cosine of the angle between them.

How it is used
Uses vector representations to infer nearest-neighbor similarity networks for user segmentation.
How deep it goes
Defines angular similarity, score interpretation, normalization implications, and thresholded graph construction without presenting the underlying formula.
Examples
Demonstrates user vectors built from activity, degree, graphlet, closeness, and betweenness features.
What you can do after
Apply it via Neo4j GDS gds.knn to construct a scored similarity graph for downstream community detection.

Read how Graph Algorithms for Data Science explains cosine similarity →

Generative AI in Action

Cosine similarity measures the cosine of the angle between two vectors and indicates their directional similarity.

How it is used
Supports semantic retrieval by comparing query and document embeddings in a retrieval-augmented generation workflow.
How deep it goes
Defines directional similarity and its range, compares alternative metrics, and configures cosine distance in a Redis index without a mathematical derivation.
Examples
Uses a Longhorn query to retrieve three similar posts and a Redis HNSW schema configured with COSINE.
What you can do after
Apply it via Redis to configure an embedding index and retrieve similar posts.

Read how Generative AI in Action explains cosine similarity →

Fight Fraud with Machine Learning

Cosine similarity is a metric that quantifies the directional similarity between two vectors as the cosine of the angle between them.

How it is used
Supports duplicate checks and selfie-ID identity verification by comparing face embeddings.
How deep it goes
Defines directional vector similarity and works through inference, thresholding, and Siamese-network training with sigmoid and binary cross-entropy.
Examples
Demonstrates Einstein selfie pairs, an Einstein–McCarthy mismatch, ID comparisons, and duplicated or pasted-photo fraud cases.
What you can do after
Apply it via PyTorch to compare MTCNN-cropped, InceptionResnetV1-generated face embeddings for duplicate and selfie-ID checks.

Read how Fight Fraud with Machine Learning explains cosine similarity →

Build Python Web Apps with Streamlit

Cosine similarity is a metric that compares vectors by measuring the cosine of the angle between them.

How it is used
Supports embedding-based retrieval by ranking vectors according to the angle between them.
How deep it goes
Defines angle-based similarity and applies it to vector indexes, but does not derive the mathematical formula.
Examples
Uses plotted movie vectors and support-article embeddings as concrete similarity-search examples.
What you can do after
Operate/configure a Pinecone index using cosine as its vector similarity metric.

Read how Build Python Web Apps with Streamlit explains cosine similarity →

Build an Advanced RAG Application (From Scratch)

Cosine similarity measures the cosine of the angle between two vectors to quantify their directional relatedness.

How it is used
Ranks document embeddings against query embeddings within semantic search.
How deep it goes
Explains vector angles, score interpretation, cosine distance, and retrieval code, but does not derive the formula mathematically.
Examples
Uses pet-related vector diagrams, capital-country embeddings, and a six-sentence machine-learning retrieval example.
What you can do after
Apply it via Sentence Transformers and SciPy to rank documents by semantic relatedness.

Read how Build an Advanced RAG Application (From Scratch) explains cosine similarity →

LLM Evaluation and Alignment, The Foundational Ideas

Cosine similarity measures the orientational similarity of two non-zero vectors by calculating the cosine of the angle between them, with values from -1 to 1.

How it is used
Evaluates semantic overlap between candidate and reference texts when exact lexical matching misses paraphrases.
How deep it goes
Defines orientation-based scoring, demonstrates embedding construction, and stops short of solving contextual meaning and word order.
Examples
Uses attorney–lawyer paraphrases, weather–freezing paraphrases, and committee-approval sentences to illustrate semantic closeness.
What you can do after
Apply it via PyTorch to compare embedding vectors for semantically related passages.

Read how LLM Evaluation and Alignment, The Foundational Ideas explains cosine similarity →

AI Agents in Action, Second Edition

Cosine similarity measures the similarity of two nonzero vectors by the cosine of the angle between them, independently of their magnitudes.

How it is used
Supports vector similarity search for retrieving documents without making vector magnitude the main signal.
How deep it goes
Defines angular similarity, compares distance scales and alternatives, and demonstrates calculation and retrieval; it does not derive the formula.
Examples
Compares sentence vectors and asks for rankings on “blue” and “breakfast” using TF-IDF and embeddings.
What you can do after
Apply it via scikit-learn to compare document vectors and inspect similarity scores.

Read how AI Agents in Action, Second Edition explains cosine similarity →

AI Agents in Action

Cosine similarity is a measure of the angular similarity between two vectors, commonly used to compare the meanings represented by embeddings.

How it is used
Supports document comparison, semantic retrieval, and selection of a consistent generated answer.
How deep it goes
Defines angular similarity and distance, then demonstrates document similarity matrices and answer selection using embeddings.
Examples
Compares a set of documents and selects a generated answer closest to the mean embedding.
What you can do after
Apply it via scikit-learn to compare documents and interpret cosine distance scores in retrieval.

Read how AI Agents in Action explains cosine similarity →

LLM Customization and Fine-Tuning

A measure of the angle-based similarity between two vectors, often used to compare weighted text representations.

How it is used
Supports semantic retrieval, distribution-level drift detection, and screening for training-test overlap.
How deep it goes
Defines normalized dot products and works through centroid drift and pairwise overlap screening without a long mathematical derivation.
Examples
Compares query and chunk embeddings, reference and production centroids, and teacher-generated training examples against held-out tests.
What you can do after
Apply it via scikit-learn to screen training examples for excessive TF-IDF overlap with a held-out test set.

Read how LLM Customization and Fine-Tuning explains cosine similarity →

AI Recommender Systems

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

How it is used
Supports item retrieval by ranking vectors that point in similar directions across behavioral, metadata, and embedding-based recommendation pipelines.
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 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.

Read how AI Recommender Systems explains cosine similarity →

Machine Learning Platform Engineering

Cosine similarity measures the angular alignment of two vectors independently of their magnitudes.

How it is used
Uses cosine similarity to retrieve semantically relevant chunks in text-focused FAISS applications.
How deep it goes
Defines the semantic-direction rationale and operational trade-offs but stops short of mathematical derivation or implementation details.
Examples
Demonstrates similarity search over indexed documentation using the query “How do I rotate encryption keys?” and three retrieved results.
What you can do after
Evaluate retrieval quality by selecting a distance metric and tuning result counts and similarity thresholds.

Read how Machine Learning Platform Engineering explains cosine similarity →

Retrieval Augmented Generation, The Foundational Ideas

Cosine similarity is a metric that measures the angular similarity between two vectors.

How it is used
Ranks semantically related documents when exact keyword overlap is insufficient.
How deep it goes
Explains normalization, dot products, geometric values, and retrieval failures, but stops short of detailed mathematical derivation.
Examples
Compares automobile-maintenance retrieval scores and illustrates normalized vectors with differing lengths.
What you can do after
Evaluate semantic-ranking behavior and diagnose errors caused by magnitude removal, averaging, antonyms, or lost sentence structure.

Read how Retrieval Augmented Generation, The Foundational Ideas explains cosine similarity →

Domain-Specific Small Language Models

A similarity measure that compares vectors by the cosine of the angle between them.

How it is used
Ranks query-document embeddings for RAG retrieval and helps retrieve relevant past interactions in conversational memory.
How deep it goes
Defines its role in measuring embedding relationships, but provides no formula or implementation details for the similarity calculation.
Examples
Uses query-document embeddings in RAG and past-interaction search in Memoripy.
What you can do after
Apply it in embedding-based retrieval to select relevant documents or past interactions.

Read how Domain-Specific Small Language Models explains cosine similarity →

Graph Neural Networks in Action

Cosine similarity is a measure of similarity between vectors based on the cosine of the angle between them.

How it is used
Builds item-similarity workflows from graph-model embeddings for recommendation and product-bundle generation.
How deep it goes
Defines cosine similarity operationally through embedding comparison, matrix construction, and ranked nearest-neighbor retrieval.
Examples
Demonstrates a Marceline product bundle produced by ranking similarities for node 123.
What you can do after
Apply it via SciKit Learn and NumPy to rank products nearest to a chosen graph node.

Read how Graph Neural Networks in Action explains cosine similarity →

Build an AI Agent (From Scratch)

Cosine similarity is a measure of the angular similarity between two vectors, commonly computed as their dot product divided by the product of their magnitudes.

How it is used
Ranks semantically relevant documents or chunks during vector search and retrieval-augmented generation.
How deep it goes
Defines angular similarity and demonstrates implementation with embeddings, but does not derive the underlying vector formula.
Examples
Compares cat, kitten, and dog sentences and ranks documents about artificial intelligence, machine learning, Python, and pets.
What you can do after
Apply it via scikit-learn to rank embedded queries against stored document or chunk vectors.

Read how Build an AI Agent (From Scratch) explains cosine similarity →

Enterprise RAG

Cosine similarity measures the angular similarity between two vectors by computing the cosine of the angle between them.

How it is used
Configures vector retrieval to rank embedding vectors by directional semantic similarity.
How deep it goes
Defines angular similarity and applies it to index configuration, stopping short of mathematical derivations or implementation code.
Examples
Uses a 1,536-dimensional Azure AI Search vector field and compares Exhaustive KNN with HNSW.
What you can do after
Operate/configure Azure AI Search vector retrieval with cosine similarity and an appropriate nearest-neighbor strategy.

Read how Enterprise RAG explains cosine similarity →

MongoDB 8.0 in Action, Third Edition

Cosine similarity is a measure of the cosine of the angle between two vectors, indicating their directional similarity.

How it is used
Supports semantic search, recommendation systems, question answering, fraud detection, and retrieval-augmented chatbot workflows.
How deep it goes
Explains directional comparison and index configuration, then stops short of deriving the cosine formula or implementing the search algorithm.
Examples
Uses semantic document retrieval and MongoDB Atlas index definitions with 1,536-dimensional embedding fields.
What you can do after
Operate/configure it through Atlas CLI and mongosh, creating and verifying a cosine-based vector search index.

Read how MongoDB 8.0 in Action, Third Edition explains cosine similarity →

Transformers in Action

Cosine similarity is a metric that measures the directional similarity between two vectors using the cosine of the angle between them.

How it is used
Supports semantic comparison, vector retrieval, chatbot context resolution, and response-quality estimation.
How deep it goes
Defines cosine similarity as an embedding comparison measure and demonstrates computation, but does not derive its mathematical formula.
Examples
Uses contextual word meanings, sentence embeddings, query-document pairs, and a printed similarity matrix.
What you can do after
Apply it via SentenceTransformer and util.cos_sim to compare encoded queries with documents.

Read how Transformers in Action explains cosine similarity →

Building Reliable AI Systems

Cosine similarity is a measure of the angular similarity between two vectors, calculated from the cosine of the angle between them.

How it is used
Ranks text embeddings by semantic proximity in search and retrieval, including health-care policy lookup.
How deep it goes
Defines angular comparison of vector direction, notes scale invariance, and contrasts dot product’s sensitivity to magnitude.
Examples
Uses paraphrased therapy questions and a Pinecone index configured with the cosine metric.
What you can do after
Apply it via Pinecone by selecting cosine as the vector index metric for text embeddings.

Read how Building Reliable AI Systems explains cosine similarity →

Also covered in

What to know first

Related concepts

Questions about cosine similarity

What is cosine similarity?

Cosine similarity measures the directional alignment of two nonzero vectors as the cosine of the angle between them, equal to their dot product divided by the product of their magnitudes.

Which Manning books explain cosine similarity?

Natural Language Processing in Action, Second Edition, Rearchitecting LLMs, AI-Powered Search, Build AI-Enhanced Web Apps, Human-in-the-Loop Machine Learning: Active learning and annotation for human-centered AI, Math and Architectures of Deep Learning, Graph Algorithms for Data Science, Generative AI in Action and 17 more.

What should I know before learning cosine similarity?

Manning books that teach cosine similarity usually introduce embedding, vector space, similarity search and language model first.

What is the difference between cosine similarity and Euclidean distance?

Cosine similarity measures the directional alignment of two nonzero vectors as the cosine of the angle between them, equal to their dot product divided by the product of their magnitudes. Euclidean distance is the straight-line distance between two points, calculated as the square root of the sum of squared differences between corresponding coordinates.

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.