Search code examples
pythonmissing-dataknnimputation

how and using which library can i use knn imputation for missing value analysis


I am trying to to impute the missing values using knn but i couldnt able to use the code: from fancyimpute import KNN
is there any other library for knn imputation ?


Solution

  • An alternative library is sklearn:

    from sklearn.impute import KNNImputer
    

    An example from skleanr's documentation:

    import numpy as np
    from sklearn.impute import KNNImputer
    
    X = [[1, 2, np.nan], [3, 4, 3], [np.nan, 6, 5], [8, 8, 7]]
    imputer = KNNImputer(n_neighbors=2)
    imputer.fit_transform(X)
    

    Output:

    array([[1. , 2. , 4. ],
           [3. , 4. , 3. ],
           [5.5, 6. , 5. ],
           [8. , 8. , 7. ]])