liveBook · Manning

Distance

As explained in Natural Language Processing in Action, Second Edition

A distance metric is a function that quantifies dissimilarity between objects while satisfying the mathematical properties of a metric.

Distance measures how far apart objects or representations are. In natural language processing, character-based measures can miss meaning: “good” and “okay” differ in spelling, while “bad” and “bag” look alike but mean different things. Vector-based distances are useful when comparing word, text, topic, or document representations. section 1.5.3 section 4.6

Common choices include:

  • Euclidean (L2), squared Euclidean, cosine, Minkowski, Manhattan (L1), Jaccard, Mahalanobis, and Levenshtein (edit) distance. section 4.6
  • Choose a measure that suits the representation and task: edit distance counts character changes, while vector distances can compare represented text. section 1.5.3

Checking topic representations

Compare distances between LSA topic vectors with those from a higher-dimensional TF–IDF representation. LSA preserves large distances but may not retain close relationships, so nearby topic vectors do not always reflect fine-grained similarity in document subject matter. section 4.6

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'

Browse the pairwise distance options listed for scikit-learn. section 4.6

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Names common distance measures and explains vector comparisons, noting that LSA may lose fine-grained proximity.
Examples
Contrasts “good” with “okay” and “bad” with “bag,” then compares LSA topic-vector distances with TF–IDF distances.
What you can do after
Evaluate whether topic vectors retain document relationships by comparing their distances with those in a higher-dimensional TF–IDF representation.
How it connects
Connects distance to vector representations, TF–IDF, bag-of-words, latent semantic analysis, and singular value decomposition.

Also explained in

Compare how 10 books explain 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.