Distance Measures

Clustering algorithms need a way to quantify how similar or different two data points are. That's exactly what distance measures do - they turn the notion of "similarity" into a concrete number that an algorithm can work with.

Euclidean Distance

The most commonly used distance measure - it's simply the straight-line distance between two points, calculated using the Pythagorean theorem extended to multiple dimensions.

from scipy.spatial import distance

point_a = (2, 4)
point_b = (5, 8)

euclidean = distance.euclidean(point_a, point_b)
print("Euclidean Distance:", euclidean)

Manhattan Distance

Also called "city block" distance, it sums the absolute differences between coordinates, as if you could only move along grid lines (like navigating city blocks) rather than diagonally.

manhattan = distance.cityblock(point_a, point_b)
print("Manhattan Distance:", manhattan)

Minkowski Distance

A generalized form that includes both Euclidean and Manhattan distance as special cases, controlled by a parameter p. When p=1, it becomes Manhattan distance; when p=2, it becomes Euclidean distance.

Cosine Distance

Rather than measuring straight-line distance, cosine distance measures the angle between two vectors - making it especially useful for text data and high-dimensional data where the direction matters more than the magnitude.

Choosing the right distance measure matters - Euclidean distance works well for continuous, similarly-scaled features, while Cosine distance is usually preferred for sparse, high-dimensional data like text embeddings.

Coming Up Next

With a solid grasp of distance measures, you're ready to see how they're combined to merge clusters together through linkage functions.

Ready to Master Data Science?

Join Uncodemy's Data Science Course and build real, job-ready skills with expert mentors.

Explore Course