Cal11 calculator

Python Calculate Euclidiean Distance in N Space

Reviewed by Calculator Editorial Team

Euclidean distance is a fundamental concept in mathematics and computer science that measures the straight-line distance between two points in Euclidean space. This guide explains how to calculate Euclidean distance in n-dimensional space using Python, including the formula, Python implementation, and practical applications.

What is Euclidean Distance?

Euclidean distance, also known as Euclidean metric, is the "ordinary" straight-line distance between two points in Euclidean space. It is the most common distance metric used in machine learning, data analysis, and geometry.

The concept extends to n-dimensional space, where the distance between two points is calculated using the Pythagorean theorem generalized to higher dimensions.

Euclidean Distance Formula

The Euclidean distance between two points \( p \) and \( q \) in n-dimensional space is given by:

\[ d(p, q) = \sqrt{\sum_{i=1}^{n} (q_i - p_i)^2} \]

Where:

  • \( p \) and \( q \) are points in n-dimensional space
  • \( p_i \) and \( q_i \) are the coordinates of points \( p \) and \( q \) respectively
  • \( n \) is the number of dimensions

This formula calculates the square root of the sum of squared differences between corresponding coordinates of the two points.

Python Implementation

Here's a Python function to calculate Euclidean distance in n-dimensional space:

import math

def euclidean_distance(point1, point2):
    """
    Calculate the Euclidean distance between two points in n-dimensional space.

    Args:
        point1 (list): Coordinates of the first point
        point2 (list): Coordinates of the second point

    Returns:
        float: Euclidean distance between the two points
    """
    if len(point1) != len(point2):
        raise ValueError("Points must have the same number of dimensions")

    squared_distance = sum((p - q) ** 2 for p, q in zip(point1, point2))
    return math.sqrt(squared_distance)

The function takes two points as input, checks that they have the same number of dimensions, calculates the sum of squared differences, and returns the square root of that sum.

Example Calculation

Let's calculate the Euclidean distance between two points in 3D space:

Point 1 Point 2 Difference Squared Difference
2 5 3 9
3 7 4 16
6 9 3 9
Sum of squared differences 34
Euclidean distance 5.830951894845301

Using the Python function:

point1 = [2, 3, 6]
point2 = [5, 7, 9]
distance = euclidean_distance(point1, point2)
print(f"Euclidean distance: {distance}")

The output will be: Euclidean distance: 5.830951894845301

Applications

Euclidean distance has numerous applications in various fields:

  • Machine Learning: Used in k-nearest neighbors (k-NN) algorithms, clustering, and classification
  • Data Analysis: Measures similarity between data points in multivariate datasets
  • Computer Graphics: Calculates distances between points in 3D space for rendering and collision detection
  • Recommendation Systems: Measures similarity between user preferences or item features
  • Physics and Engineering: Calculates distances between physical objects in simulations

FAQ

What is the difference between Euclidean distance and Manhattan distance?
Euclidean distance measures the straight-line distance between points, while Manhattan distance (also known as taxicab distance or L1 distance) measures the sum of absolute differences between coordinates. Euclidean distance is more sensitive to diagonal movements.
Can Euclidean distance be used for categorical data?
No, Euclidean distance is designed for numerical data. For categorical data, other distance metrics like Hamming distance or Jaccard similarity are more appropriate.
How does Euclidean distance handle different scales of features?
Euclidean distance is sensitive to the scale of features. It's important to normalize or standardize features to the same scale before calculating distances, especially when features have different units or ranges.