-2

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 ?

1 Answers1

0

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. ]])
Celius Stingher
  • 17,835
  • 6
  • 23
  • 53
  • First of all thankyou for the response! actually i wanted to impute the missing values in the data with 22 variables ...and i wanted to impute in the same file itself but when i used the knnimputer my output comes in the form of array like this array([[ 55., 0., 2., ..., 999., 0., 0.], [ 40., 1., 1., ..., 999., 0., 0.], [ 42., 9., 1., ..., 999., 0., 0.], ..., [ 27., 1., 2., ..., 999., 3., 0.], [ 51., 10., 0., ..., 999., 0., 0.], [ 38., 1., 1., ..., 999., 0., 0.]]) – Kumar Rishabh Dec 27 '19 at 14:06
  • If it's a dataframe, you can use the array to replace the value in the series (given they have the same length) – Celius Stingher Dec 27 '19 at 14:27