liveBook · Manning

Cosine distance

As explained in Natural Language Processing in Action, Second Edition

The cosine distance metric measures the angular dissimilarity between vectors and is commonly used to estimate similarity in their encoded meanings.

Cosine distance measures how differently two vectors point. In natural language processing, comparing the directions of encoded messages can estimate similarity in meaning more effectively than comparing individual words or raw vector lengths. section 1.6 section 3.3

Euclidean distance measures the length of the difference between vectors. Cosine distance instead tracks the angle between them: larger angles produce values closer to 1, and vectors 90 degrees apart have distance 1. Its bounded range makes it practical for high-dimensional representations. section 3.3

Cosine distance supports several stages of language-vector analysis:

  • Represent words or messages with continuous vector dimensions such as topic, sentiment, or question-likelihood. section 1.6
  • Compare high-dimensional vectors when Euclidean distance would produce unwieldy values. section 3.3
  • Use cosine similarity, calculated as 1 minus cosine distance, when a larger score should mean greater closeness. section 3.3
  • Apply the metric to natural-language representations such as bag-of-words, n-grams, and TF–IDF vectors. section 3.10

A small NumPy comparison

For vectors v1 = (1, 0) and v2 = (2, 1), subtracting v1 from v2 gives (-1, -1), whose Euclidean length illustrates ordinary vector distance. With much larger vectors, compare their directions using cosine distance; the result remains bounded by 1 regardless of dimensionality. section 3.3

Figure 3.2 2D Euclidean distance
figure

Inspect the two-dimensional diagram to visualize how vector subtraction produces a difference arrow and an ordinary Euclidean distance. section 3.3

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains angular meaning, bounded scaling, Euclidean contrast, and similarity conversion, but does not derive the full formula.
Examples
Uses message-quality vectors, a two-dimensional NumPy subtraction, and a diagram of Euclidean vector distance.
What it assumes
Assumes basic vectors, subtraction, and the Pythagorean theorem while motivating reduced-dimensional representations from intuitive language examples.
What you can do after
Apply cosine distance to compare high-dimensional language vectors and convert it into cosine similarity when needed.
How it connects
Connects vector representations with Euclidean distance, cosine similarity, dimensionality reduction, TF–IDF, and semantic search.

Also explained in

Compare how 6 books explain cosine distance →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.