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:
- Calculate distances between this incomplete row and all complete rows using available features
- Identify the K nearest neighbors—the most similar complete observations
- Aggregate their values (typically the mean) for the missing feature
- 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.