🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEscipy

scipy Documentation

LOADING ENGINE...

spatial.distance.euclidean()

AI & DATA SCIENCE // spatial-distance-euclidean

scipy.spatial.distance.euclidean() computes the straight-line (Euclidean) distance between two points in any number of dimensions.

Syntax

scipy.spatial.distance.euclidean(u, v)

Deep Dive Course

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.

editor.html
from scipy.spatial import distance

p1 = (0, 0)
p2 = (3, 4)
print(distance.euclidean(p1, p2))
localhost:3000

2Practical Example

Here is a real-world application of spatial.distance.euclidean() showing how it is used in production SciPy code.

editor.html
from scipy.spatial import distance

p1 = (1, 2, 3)
p2 = (4, 6, 3)
print(distance.euclidean(p1, p2))
localhost:3000

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.

editor.html
from scipy.spatial import distance

p1 = (0, 0)
p2 = (3, 4)
print(distance.euclidean(p1, p2))
localhost:3000

Examples

Example 01Basic Usage
from scipy.spatial import distance

p1 = (0, 0)
p2 = (3, 4)
print(distance.euclidean(p1, p2))
Example 02Advanced Example
from scipy.spatial import distance

p1 = (1, 2, 3)
p2 = (4, 6, 3)
print(distance.euclidean(p1, p2))

Best Practices

  • Normalize or standardize features to comparable scales before computing Euclidean distances across multiple dimensions, if the dimensions represent fundamentally different units or ranges
  • 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
  • Consider whether a different distance metric, like cosine distance for direction-focused comparisons, better fits your actual notion of similarity before defaulting to Euclidean

Interview Question

Why does Euclidean distance require normalizing or standardizing features first, when those features represent fundamentally different scales, like age in years and income in dollars?

Hint: Think about how much each dimension's raw numeric range contributes to the overall squared-difference sum.

Euclidean distance sums the squared difference across every dimension equally, in raw numeric terms, without any awareness of what those numbers actually represent or what range is typical for each one. A feature like income, which might range across tens of thousands of units, will produce squared differences enormously larger than a feature like age, which only ranges across a couple dozen units, so the income dimension ends up completely dominating the total distance regardless of how meaningfully similar or different two points actually are in terms of age. Normalizing or standardizing each feature to a comparable scale first ensures every dimension contributes proportionally to the overall notion of distance/similarity, rather than letting whichever dimension happens to have the largest raw numeric range dominate by default.

Exercises

MediumPractice using spatial.distance.euclidean() in a real scenario.
View Solution
from scipy.spatial import distance

p1 = (0, 0)
p2 = (3, 4)
print(distance.euclidean(p1, p2))

Frequently Asked Questions

Why does Euclidean distance require normalizing or standardizing features first, when those features represent fundamentally different scales, like age in years and income in dollars?

Euclidean distance sums the squared difference across every dimension equally, in raw numeric terms, without any awareness of what those numbers actually represent or what range is typical for each one. A feature like income, which might range across tens of thousands of units, will produce squared differences enormously larger than a feature like age, which only ranges across a couple dozen units, so the income dimension ends up completely dominating the total distance regardless of how meaningfully similar or different two points actually are in terms of age. Normalizing or standardizing each feature to a comparable scale first ensures every dimension contributes proportionally to the overall notion of distance/similarity, rather than letting whichever dimension happens to have the largest raw numeric range dominate by default.

Related Functions

spatial-distance-cosinespatial-kdtreedf-astype