Module 5: Distance Metrics Deep Dive

Euclidean Distance: Geometric Distance

What Euclidean distance is

It measures the "straight-line" distance between two points in space (a straight line).

Formula:

euclidean(A, B) = sqrt(Σ(A_i - B_i)²)

Implementation:

import numpy as np

def euclidean_distance(a, b):
    """Euclidean distance"""
    return np.linalg.norm(a - b)

# Example
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

dist = euclidean_distance(a, b)
print(f"Euclidean: {dist:.4f}")  # 5.1962

Euclidean vs cosine

# Vectors with the SAME direction but DIFFERENT magnitude
a = np.array([1, 2, 3])
b = np.array([10, 20, 30])

# Cosine (direction only)
cos = cosine_similarity(a, b)
print(f"Cosine: {cos:.4f}")  # 1.0 (identical)

# Euclidean (considers magnitude)
euc = euclidean_distance(a, b)
print(f"Euclidean: {euc:.4f}")  # 33.6749 (very different)

# Conclusion: Euclidean is sensitive to magnitude

When to use Euclidean

Magnitude matters:

  • Image embeddings (pixel distances)
  • Audio signals

Normalized embeddings:

  • If all embeddings have magnitude = 1.0
  • Then Euclidean ~ Cosine

Don't use it for:

  • Text embeddings (OpenAI, SBERT)
  • Embeddings on different scales

Batch Euclidean

from scipy.spatial.distance import cdist

X = np.array([[1, 2], [3, 4]])
Y = np.array([[1, 2], [5, 6]])

# Distance matrix
dists = cdist(X, Y, metric='euclidean')
print(dists)
# [[0.0,    5.6569],
#  [2.8284, 2.8284]]

Summary

  • Straight-line distance in space
  • ⚠️ Sensitive to magnitude
  • Use: Image/audio embeddings
  • Avoid: Non-normalized text embeddings

Module 5 - Capsule 03