KNNimputer is a scikit-learn class used to fill out or predict the missing values in a dataset. It is a more useful method which works on the basic approach of the KNN algorithm rather than the naive approach of filling all the values with mean or the median. In this approach, we specify a distance from the missing values which is also known as the K parameter. The missing value will be predicted in reference to the mean of the neighbours. It is implemented by the KNNimputer() method which contains the following arguments:
n_neighbors: number of data points to include closer to the missing value. metric: the distance metric to be used for searching. values – {nan_euclidean. callable} by default – nan_euclidean weights: to determine on what basis should the neighboring values be treated values -{uniform , distance, callable} by default- uniform.
Code: Python code to illustrate KNNimputor classÂ
python3
# import necessary librariesimport numpy as npimport pandas as pdÂ
# import the KNNimputer classfrom sklearn.impute import KNNImputerÂ
Â
# create dataset for marks of a studentdict = {'Maths':[80, 90, np.nan, 95],        'Chemistry': [60, 65, 56, np.nan],        'Physics':[np.nan, 57, 80, 78],       'Biology' : [78,83,67,np.nan]}Â
# creating a data frame from the listBefore_imputation = pd.DataFrame(dict)#print dataset before imputationprint("Data Before performing imputation\n",Before_imputation)Â
# create an object for KNNImputerimputer = KNNImputer(n_neighbors=2)After_imputation = imputer.fit_transform(Before_imputation)# print dataset after performing the operationprint("\n\nAfter performing imputation\n",After_imputation) |
Output:
Data Before performing imputation
Maths Chemistry Physics Biology
0 80.0 60.0 NaN 78.0
1 90.0 65.0 57.0 83.0
2 NaN 56.0 80.0 67.0
3 95.0 NaN 78.0 NaN
After performing imputation
[[80. 60. 68.5 78. ]
[90. 65. 57. 83. ]
[87.5 56. 80. 67. ]
[95. 58. 78. 72.5]]
Note: After transforming the data becomes a numpy array.

… [Trackback]
[…] Read More to that Topic: geeksforgeeks.org/python-imputation-using-the-knnimputer/ […]