# import necessary libraries
import pandas as pd
import numpy as np
from sklearn.datasets import load_breast_cancer
# load the dataset
data = load_breast_cancer()
# convert the data to a Pandas dataframe
df = pd.DataFrame(data.data, columns=data.feature_names)
# replace 20% features values with NaN
mask = np.random.rand(*df.shape) < 0.2
df[mask] = np.nan
# add the target column to the dataframe
df['target'] = data.target
# import KNNImputer
from sklearn.impute import KNNImputer
# initialize the object
imputer = KNNImputer(n_neighbors=2)
# apply imputer on training set
X_train_imputed = imputer.fit_transform(X_train)
# apply imputer on testing set
X_test_imputed = imputer.transform(X_test)