• Nearest Neighbor imputation code example
Example: Nearest neighbors imputation
# Nearest neighbors imputation
import numpy as np
from sklearn.impute import KNNImputer
nan = np.nan
X = [[1, 2, nan], [3, 4, 3], [nan, 6, 5], [8, 8, 7]]
imputer = KNNImputer(n_neighbors=2, weights="uniform")
imputer.fit_transform(X)
# array([[1. , 2. , 4. ],
# [3. , 4. , 3. ],
# [5.5, 6. , 5. ],
# [8. , 8. , 7. ]])