Instance & Kernel Methods · Lesson 30

Distance Metrics

Distance Metrics belongs to distance-based learning.

ConceptWorked examplePracticeKnowledge check
Textbook walkthrough

What Distance Metrics actually means

Distance Metrics belongs to distance-based learning. K-nearest neighbours makes predictions from training examples close to the query under a chosen distance metric, so feature scale and the meaning of distance are central assumptions.

Distance Metrics matters because instance- and kernel-based methods depend directly on distance, margin or similarity geometry. Feature scale and hyperparameters can therefore change which observations are considered close or which boundary is preferred.

Deeper walkthrough

Read Distance Metrics as a mechanism, not a recipe

Treat this as a sequence of observable decisions rather than one opaque command. Stage 1: Scale/transform features so distances reflect meaningful comparability. Stage 2: Compute distance from the query point to training samples. Stage 3: Select the k nearest neighbours. Final checkpoint: Choose k and distance settings using validation.

Mechanism

Follow the transformation

Scale/transform features so distances reflect meaningful comparability.

Compute distance from the query point to training samples.

Select the k nearest neighbours.

Evidence

Know what would convince you

  • Fit a tiny or baseline case first and confirm prediction shape/range and a few outputs.
  • Evaluate with the same held-out folds/metric as competing models and inspect variability, not just the mean.
Useful distinctionSmall k: Flexible/local, lower bias and higher variance.
How it works

Trace the mechanism step by step

  1. Scale/transform features so distances reflect meaningful comparability.
  2. Compute distance from the query point to training samples.
  3. Select the k nearest neighbours.
  4. Classification uses a vote/probability from neighbour labels; regression averages (or weights) neighbour targets.
  5. Choose k and distance settings using validation.
Worked demonstration

Make the concept concrete

Demonstration

Python example

# Step 1 — Import the module so its functions/classes are available to the rest of this example.
import math
# Step 2 — Compute the right-hand expression and store its result in `a` for the next step.
a=(1,2); b=(4,6)
# Step 3 — Compute the right-hand expression and store its result in `euclid` for the next step.
euclid=math.sqrt((a[0]-b[0])**2+(a[1]-b[1])**2)
# Step 4 — Compute the right-hand expression and store its result in `manhattan` for the next step.
manhattan=abs(a[0]-b[0])+abs(a[1]-b[1])
# Step 5 — Display the current value explicitly so the result/state can be inspected during execution.
print(euclid, manhattan)
Expected / illustrative result
5.0 7
Different metrics encode different geometry; feature scaling can change which points are considered nearest.
Interpret the result.

For Distance Metrics, connect the reported result to the exact training/validation/prediction step that produced it and check one prediction, fold or metric component independently.

Distinctions & related ideas

Know what this is — and what it is not

Small kFlexible/local, lower bias and higher variance.
Large kSmoother, higher bias and lower variance.
Euclidean distanceStraight-line distance; sensitive to scale.
Manhattan distanceSum of absolute coordinate differences.
Use deliberately

When it is appropriate

Use Distance Metrics when its inductive assumptions fit the feature/target structure and it can be compared fairly with a simpler baseline on unseen data.

Boundary conditions

When to stop or reconsider

Prefer a simpler or different model when the sample size, representation, computational budget, interpretability requirement or data geometry conflicts with this method.

Common mistakes

Failure modes to recognise

  • Judging the model only by training fit instead of generalisation on held-out data.
  • Comparing models with inconsistent preprocessing, folds or evaluation metrics.
  • Tuning complexity without checking a simple baseline, error patterns and variance across splits.
Verification

How to check the result

  • Fit a tiny or baseline case first and confirm prediction shape/range and a few outputs.
  • Evaluate with the same held-out folds/metric as competing models and inspect variability, not just the mean.
  • Inspect errors/residuals or decision boundaries and vary one key hyperparameter to verify expected behaviour.
Hands-on practice

Demonstrate understanding

Try this:

Build a tiny, inspectable example of Distance Metrics. First scale/transform features so distances reflect meaningful comparability. Then compute distance from the query point to training samples. Write the expected result before running it, and explain one condition that would make the result misleading or invalid.

Start with a small baseline and a fixed validation split/fold assignment. Predict what increasing or decreasing one complexity control should do before testing it.
Knowledge check

Check reasoning, not memorisation

Before trusting a result from Distance Metrics, which check provides the strongest evidence that you understand and applied it correctly?

Quick reference

Keep the important distinctions visible

Step 1Scale/transform features so distances reflect meaningful comparability.
Step 2Compute distance from the query point to training samples.
Step 3Select the k nearest neighbours.
Step 4Classification uses a vote/probability from neighbour labels; regression averages (or weights) neighbour targets.
Lesson summary

What to remember

  • Distance Metrics belongs to distance-based learning. K-nearest neighbours makes predictions from training examples close to the query under a chosen distance metric, so feature scale and the meaning of distance are central assumptions.
  • Scale/transform features so distances reflect meaningful comparability.
  • Judging the model only by training fit instead of generalisation on held-out data.
  • Fit a tiny or baseline case first and confirm prediction shape/range and a few outputs.