k-NN Imputation

medium
Checkpoint challenge
Solve this to show you know the material and jump straight to Unit 15. Everything before it counts as cleared.

Fill missing values (NaN) from similar rows. Implement knn_impute(X, k):

  • distance between two rows uses only the columns both have, as the root of the mean squared difference over those shared columns (rows with no shared columns are infinitely far)
  • for each missing cell (i, j), take the k nearest other rows that have column j (ties by lower row index) and fill with the mean of their values
  • impute from the original data, not from already-filled cells

Return a new array.

#preprocessing#missing-data#knn2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
k-NN Imputation · TechGradify