SKLearner Home | About | Contact | Examples

Scikit-Learn euclidean_distances() Metric

The euclidean_distances() function in scikit-learn is used to calculate pairwise Euclidean distances between two sets of data points. Euclidean distance is the straight-line distance between two points in Euclidean space, which is the square root of the sum of the squared differences of their coordinates.

In mathematical terms, the Euclidean distance between two points x and y in an n-dimensional space is calculated as:

sqrt((x1 - y1)^2 + (x2 - y2)^2 + ... + (xn - yn)^2)

Pairwise distance calculations are commonly used in various machine learning tasks, such as clustering, nearest neighbor classification, and anomaly detection. By measuring the distances between data points, we can identify similarities or dissimilarities and make informed decisions based on their proximity.

However, it’s important to note that euclidean_distances() calculates distances based on the raw feature values, which can be sensitive to the scale of the features. If the features have different scales or units, it’s recommended to normalize or standardize the data before calculating distances to ensure fair comparisons.

from sklearn.datasets import make_blobs
from sklearn.metrics import euclidean_distances

# Generate a synthetic dataset with distinct clusters
X, _ = make_blobs(n_samples=10, centers=3, n_features=2, random_state=42)

# Calculate pairwise Euclidean distances
distances = euclidean_distances(X)

# Extract distances between specific data points
dist_0_1 = distances[0, 1]
dist_2_3 = distances[2, 3]

print(f"Distance between point 0 and 1: {dist_0_1:.2f}")
print(f"Distance between point 2 and 3: {dist_2_3:.2f}")

Running the example gives an output like:

Distance between point 0 and 1: 2.04
Distance between point 2 and 3: 14.87
  1. Generate a synthetic dataset using make_blobs() to create distinct clusters of data points. In this example, we generate 10 samples with 3 centers and 2 features.

  2. Calculate the pairwise Euclidean distances between all data points using euclidean_distances(). The resulting distances variable is a square matrix where each element represents the distance between the corresponding data points.

  3. Demonstrate how to access specific pairwise distances from the distance matrix. In this case, we extract the distance between points 0 and 1 (dist_0_1) and the distance between points 2 and 3 (dist_2_3).

This example showcases how to use the euclidean_distances() function from scikit-learn to calculate pairwise Euclidean distances between data points. By measuring these distances, we can gain insights into the spatial relationships and similarities within the dataset, which can be valuable for various machine learning tasks.



See Also