liveBook · Manning

Distance

As explained in Data Without Labels

A distance metric is a distance function that satisfies the metric-space properties, including nonnegativity, symmetry, and the triangle inequality.

Distance measures how far apart two observations are under a chosen rule. In clustering, that rule helps determine which observations appear similar: for centroid-based clustering, a smaller distance to a cluster centroid means greater similarity. Choosing a different distance measure—or different attributes—can change the resulting groups, so start with the business question and the variables that matter to it. section 2.3 section 2.2

Common ways to measure distance between points include:

  • Euclidean distance: the straight-line distance. For points (x1, y1) and (x2, y2), calculate √((x2 − x1)² + (y2 − y1)²). section 2.3
  • Manhattan distance: movement along perpendicular grid axes, calculated as |x2 − x1| + |y2 − y1|. section 2.3
  • Chebyshev distance: the largest absolute difference along any coordinate dimension. section 2.3
Figure 2.5 Euclidean distance, Chebyshev distance, Manhattan distance, and cosine distance are the primary distance metrics used. Note how the distance is different for two points using these metrics. In Euclidean distance, the direct distance is measured between two points, as shown by the first figure on the left.
figure

Compare the paths and measurements for the same two points. The picture makes clear why changing the distance rule can change a clustering result. section 2.3

In hierarchical clustering, keep the point-to-point distance measure separate from the linkage rule that turns point distances into a distance between clusters. Single linkage uses the closest pair across clusters; complete linkage uses the farthest pair; average linkage averages all cross-cluster pairs. Ward linkage instead aims to minimize variability in the merged cluster. Changing the linkage rule can change which clusters merge. section 2.4.2

Try a hierarchical clustering cut

With prepared data in X_standard, run scipy.cluster.hierarchy.linkage(X_standard, 'average', metric='euclidean'), then plot its dendrogram. Try complete and Ward linkage to compare the merges. A separate distance cutoff passed to fcluster(..., criterion='distance') determines the final groups; it is not the same choice as metric='euclidean'. Plot the assigned clusters and vary the cutoff: different values can produce different numbers of clusters. section 2.4.5

There is no one distance choice for every dataset. Euclidean distance is often used, but the attributes, distance measure, linkage rule, and cutoff should be checked against the intended interpretation of the clusters. Distance also matters in DBSCAN, where a radius and the distances to nearby points help determine neighborhoods; its results are sensitive to the chosen settings. section 2.7 section 2.5.2

How Data Without Labels covers it

How deep it goes
Defines three distance measures with coordinate formulas and applies them in clustering workflows, without developing formal metric-space properties.
Examples
Uses two-point coordinate formulas, visual distance comparisons, and SciPy dendrograms with varied linkage rules and cutoffs.
What it assumes
Assumes observations with usable attributes and prepared numeric data for the Python workflow, while explaining common distance measures from coordinates.
What you can do after
Apply it via SciPy hierarchical clustering to compare distance settings and linkage rules, then inspect how a cutoff changes the groups.
How it connects
Connects distance to centroids, hierarchical linkage, dendrogram cutoffs, and DBSCAN radius selection.

Also explained in

Compare how 10 books explain distance →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data Without Labels. The section links point to the parts of the book it draws on.