Euclidean distance is the ordinary, straight-line distance you'd measure with a ruler, generalized to any number of dimensions — the square root of the sum of the squared differences between corresponding coordinates. It's the most commonly used distance metric for numeric data, but scipy.spatial.distance provides many alternative metrics, like cosine, Manhattan/cityblock, and Hamming distance, that are more appropriate for certain kinds of data or certain notions of similarity.
1Understanding spatial.distance.euclidean()
Euclidean distance is the ordinary, straight-line distance you'd measure with a ruler, generalized to any number of dimensions — the square root of the sum of the squared differences between corresponding coordinates. It's the most commonly used distance metric for numeric data, but scipy.spatial.distance provides many alternative metrics, like cosine, Manhattan/cityblock, and Hamming distance, that are more appropriate for certain kinds of data or certain notions of similarity.
Euclidean distance is sensitive to the scale of each dimension — a coordinate ranging from 0 to 1000 will dominate the distance calculation compared to one ranging from 0 to 1, unless the data is normalized/standardized first, which matters a lot for tasks like clustering that rely on meaningful distances.
from scipy.spatial import distance
p1 = (0, 0)
p2 = (3, 4)
print(distance.euclidean(p1, p2))2Practical Example
Here is a real-world application of spatial.distance.euclidean() showing how it is used in production SciPy code.
from scipy.spatial import distance
p1 = (1, 2, 3)
p2 = (4, 6, 3)
print(distance.euclidean(p1, p2))3Best Practices
Follow these guidelines when working with spatial.distance.euclidean():
1. Normalize or standardize features to comparable scales before computing Euclidean distances across multiple dimensions, if the dimensions represent fundamentally different units or ranges
2. Use scipy.spatial.distance.euclidean() for pairwise checks, but np.linalg.norm() or pdist()/cdist() for computing many distances at once, since they're more efficient for that case
3. Consider whether a different distance metric, like cosine distance for direction-focused comparisons, better fits your actual notion of similarity before defaulting to Euclidean
Tip: Euclidean distance is sensitive to the scale of each dimension — a coordinate ranging from 0 to 1000 will dominate the distance calculation compared to one ranging from 0 to 1, unless the data is normalized/standardized first, which matters a lot for tasks like clustering that rely on meaningful distances.
from scipy.spatial import distance
p1 = (0, 0)
p2 = (3, 4)
print(distance.euclidean(p1, p2))