KNN Imputer
You can access the code HERE:
https://github.com/nabinadhikari96/100-days-of-machine-learning/tree/main/day39-knn-imputer
KNN Imputer MultiVariate
KNN Imputer is an imputation method for handling missing values in a dataset. It stands for “k-nearest neighbors” imputer.
The KNN Imputer method works by replacing missing values in a feature with the average (mean) value of the k-nearest neighbors of that value. The number of nearest neighbors (k) is a hyperparameter that can be specified by the user.
To use the KNN Imputer method, you first need to install the knnimpute package. Then, you can use the KNNImputer class from the knnimpute package to perform the imputation.
Here is an example of how you might use the KNN Imputer method to impute missing values in a Pandas DataFrame:
Copy codefrom knnimpute import KNNImputer
import pandas as pd# Load the data
df = pd.read_csv('data.csv')# Initialize the imputer
imputer = KNNImputer(k=5)# Impute the missing values
df_imputed = imputer.fit_transform(df)
In this example, the KNN Imputer will replace missing values in the DataFrame with the average value of the 5 nearest neighbors of that value.
It is important to note that the KNN Imputer method can be computationally expensive for large datasets, as it requires calculating the distance between all pairs of observations. In addition, the quality of the imputations may depend on the choice of the k hyperparameter.
Imputation for completing missing values using k-Nearest Neighbors.
Each sample’s missing values are imputed using the mean value from n_neighbors the nearest neighbors found in the training set. Two samples are close if the features that neither is missing are close.
nan_euclidean distance:
It is used when we cannot calculate the distance(number,missing_value).
dist(x,y) = sqrt(weight * sq. distance from present coordinates) where, weight = Total # of coordinates / # of present coordinates
#import libraries
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.impute import KNNImputer,SimpleImputer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
Get Nabin Adhikari’s stories in your inbox
Join Medium for free to get updates from this writer.
#read csv file
df = pd.read_csv(‘train.csv’)[[‘Age’,’Pclass’,’Fare’,’Survived’]]
df.head()
#calculating percentage of missing value
df.isnull().mean() * 100
#traget and features
X = df.drop(columns=[‘Survived’])
y = df[‘Survived’]
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=2)
X_train.head()
#calculating with the most 3 nearest data
knn = KNNImputer(n_neighbors=3,weights=’distance’)
X_train_trf = knn.fit_transform(X_train)
X_test_trf = knn.transform(X_test)
#using model
lr = LogisticRegression()
lr.fit(X_train_trf,y_train)
y_pred = lr.predict(X_test_trf)
accuracy_score(y_test,y_pred)
# Comparision with Simple Imputer → mean
si = SimpleImputer()
X_train_trf2 = si.fit_transform(X_train)
X_test_trf2 = si.transform(X_test)
#using model
lr = LogisticRegression()
lr.fit(X_train_trf2,y_train)
y_pred2 = lr.predict(X_test_trf2)
accuracy_score(y_test,y_pred2)
accuracy of Knn Imputer > accuracy of Simple Imputer




