liveBook · Manning

Similarity metric

As explained in Natural Language Processing in Action, Second Edition

Similarity is a numerical or qualitative measure of how closely two patterns or signals resemble each other.

A similarity metric measures how closely two patterns or signals resemble each other. For represented text, it compares feature, topic, document-context, or embedding vectors; higher similarity generally means the represented meanings are closer. section 4.6

The choice of metric changes what “close” means. Common options include Euclidean, squared Euclidean, cosine, Minkowski, Manhattan, Jaccard, Mahalanobis, and Levenshtein distance. Some applications convert distance to similarity, but the conversion must match the scale and interpretation required. section 4.6

Comparing words and search results

A gensim word-vector model can calculate cosine similarity between “princess” and “queen,” returning 0.70705315983704509. The same vector space can rank documents by comparing an embedded query with embedded documents. For large collections, approximate nearest-neighbor search avoids comparing the query with every stored vector. section 6.3.4 section 10.3.3

Listing 4.12 Pairwise distances available in sklearn
'cityblock', 'cosine', 'euclidean', 'l1', 'l2', 'manhattan', 'braycurtis',
'canberra', 'chebyshev', 'correlation', 'dice', 'hamming', 'jaccard',
'kulsinski', 'mahalanobis', 'matching', 'minkowski', 'rogerstanimoto',
'russellrao', 'seuclidean', 'sokalmichener', 'sokalsneath', 'sqeuclidean',
'yule'

Use the available scikit-learn pairwise metrics as a practical menu when selecting a distance or similarity basis for vector comparisons. section 4.6

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Defines multiple distance families and applies cosine similarity, while noting that LSA preserves large but not always fine-grained distances.
Examples
Uses word-vector comparisons, semantic-search rankings, LSA-versus-TF-IDF checks, and scikit-learn’s catalog of pairwise metrics.
What it assumes
Assumes familiarity with vectors and text representations while explaining metric choices through NLP and search applications.
What you can do after
Apply it via gensim or vector-search tooling to compare terms, evaluate representations, and rank documents by relevance.
How it connects
Connects vector representations with embeddings, cosine similarity, semantic search, vector databases, approximate nearest neighbors, LSA, and TF-IDF.

Also explained in

Compare how 10 books explain similarity metric →

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.