Course contentsShow
← Machine Learning and Deep Learning
Lesson 434 of 3,53810. Feature EngineeringFree lesson

K-Nearest Neighbors Imputation

Imputing missing values using similar observations based on feature distance and preserving local patterns.

K-Nearest Neighbors Imputation

You'll learn how to fill in missing values by borrowing information from the most similar complete observations in your dataset.

The Core Idea

K-Nearest Neighbors (KNN) Imputation takes a sophisticated approach to handling missing data: instead of using global statistics like mean or median, it finds the K most similar complete observations (neighbors) to the row with missing data, then uses their values to estimate what's missing.

Think of it like asking your closest friends for recommendations. If you've never tried a particular restaurant, you'd trust suggestions from friends with similar tastes much more than the city-wide average rating. KNN imputation works the same way—it finds "neighboring" data points based on feature similarity and uses their values.

How It Works

When a row has a missing value:

  1. Calculate distances between this incomplete row and all complete rows using available features
  2. Identify the K nearest neighbors—the most similar complete observations
  3. Aggregate their values (typically the mean) for the missing feature
  4. Fill in the gap with this local estimate

The "distance" here refers to similarity measures you've seen before (like Euclidean distance), calculated only on features that aren't missing.

Why It Preserves Patterns

Unlike simple mean imputation, KNN respects the local structure of your data. If high-income individuals tend to spend more on luxury goods, KNN will impute missing spending values for high earners using similar high earners—not the overall average that includes everyone.

Key Takeaway: K-Nearest Neighbors Imputation fills missing values using similar observations rather than global statistics, preserving local patterns and relationships in your data at the cost of increased computation.