Notes

Euclidean Distance

When you say two data points are “close,” you’re really making a mathematical choice about what closeness means. Euclidean distance is the most familiar version: it matches the straight-line distance you’d measure with a ruler.

What it is (and how it’s computed)

Given two feature vectors x and y with the same number of features, Euclidean distance measures the length of the difference between them:

d(x, y) = sqrt( sum_i (x_i - y_i)^2 )

Squaring makes all differences positive and emphasizes larger gaps; the square root brings the result back to the original units. In 2D it’s the Pythagorean theorem; in many dimensions it’s the same idea repeated across all features.

Why it matters in supervised learning (especially k-NN)

In k-nearest neighbors, predictions come from the “nearest” training examples, so the distance metric directly controls which points get to vote (classification) or get averaged (regression). If you use Euclidean distance, features with larger numeric scales dominate. For example, in credit scoring, “annual income” (0–200,000) can overwhelm “number of late payments” (0–10) unless you standardize features (e.g., z-scores). In scikit-learn, this shows up when you use KNeighborsClassifier with its default metric (Minkowski with p=2, which is Euclidean).

Practical examples and common pitfalls
  • Spam detection: Euclidean distance on raw word counts is usually a poor fit; cosine distance often matches text better.
  • House price prediction: Mixing square footage, neighborhood encodings, and year built requires careful scaling/encoding or distances become misleading.
  • Outliers: A single extreme feature value can greatly increase Euclidean distance, reshaping neighborhoods.

Euclidean Distance is the straight-line distance between two points in a feature space, computed as the square root of the sum of squared differences across corresponding features (the L2 norm). It defines similarity geometrically: smaller values mean more similar examples. It matters because supervised methods like k-nearest neighbors (k-NN) rely on it to select neighbors and weight influence; poor distance choice or scaling distorts neighborhoods and degrades predictions.

Think of a map: the straight-line distance between two places is the shortest path “as the crow flies.” Euclidean Distance is that same idea, but for data. It tells you how far apart two things are by treating their details as coordinates—like comparing two people by height and weight, or two emails by how “spammy” their words look.

In supervised learning, especially methods like k-nearest neighbors, this distance helps the model decide what’s “close.” If a new case is closest to several known examples labeled “spam,” it’s likely spam too. It’s a simple, intuitive way to measure similarity.