liveBook · Manning

Cosine similarity

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

How 14 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
Ranks candidate answers and identifies related or potentially redundant passages using text vectors.
How deep it goes
Derives the normalized dot product from vector norms, implements it in Python, and extends it to pairwise similarity matrices.
Examples
Compares an algorithmic-bias question with count-vector sentences and inspects a passage-embedding similarity heatmap.
What you can do after
Apply it via scikit-learn cosine_similarity to rank matches or via normalized embeddings to construct pairwise similarity matrices.

Read how Natural Language Processing in Action, Second Edition 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
Ranks query–document matches and identifies related items or phrases by comparing their vectors.
How deep it goes
Works through the formula, a NumPy calculation, sparse query-term features, and normalized embedding comparisons; it does not develop the linear algebra further here.
Examples
Compares food-item embeddings, scores two documents for “apple juice,” and ranks four encoded phrases.
What you can do after
Apply it via NumPy to score query–document vectors, or via SentenceTransformers embeddings and dot scores to rank phrase similarities.

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
Ranks query matches in an embedding-based knowledge base and scores generated text against expected output in tests.
How deep it goes
Defines the score’s directional meaning and walks through matching and threshold checks, but does not implement the embedding cosineSimilarity function.
Examples
Uses an IT support question-and-answer lookup and a mocked AI-response test with a 0.7 similarity threshold.
What you can do after
Apply it via JavaScript examples to select a knowledge-base match or check whether generated text exceeds a chosen similarity threshold.

Read how Build AI-Enhanced Web Apps 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
Ranks text embeddings by directional similarity during semantic search and retrieval.
How deep it goes
Defines the measure and its range, compares alternatives, and configures it in Redis without deriving the mathematics.
Examples
Uses a Redis blog-post index and a query returning three similar posts.
What you can do after
Apply it via a Redis vector index to retrieve posts similar to an embedded query.

Read how Generative AI in Action 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 →

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
Ranks vectorized documents for retrieval using either TF-IDF features or semantic embeddings.
How deep it goes
Defines the angle-based measure, interprets scores and distance, and works through Python comparisons without deriving the formula.
Examples
Compares sentences about skies, foxes, and breakfast, then tests “blue” and “breakfast” queries against TF-IDF and embedding rankings.
What you can do after
Apply it via scikit-learn to compare document vectors and interpret Chroma DB distance-ranked results.

Read how AI Agents in Action, Second Edition 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 →

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 →

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 →

Related concepts

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.