text_plain_output_1.png
from sklearn.impute import KNNImputer

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

train = pd.read_csv('/kaggle/input/titanic/train.csv')
test = pd.read_csv('/kaggle/input/titanic/test.csv')

predictors = ['Pclass', 'Sex', 'Age', 'Parch', 'SibSp', 'Fare', 'Embarked']

X_train = train[predictors]
y_train = train['Survived']
X_test = test[predictors]

knn_imputer = KNNImputer()
X_train = pd.DataFrame(knn_imputer.fit_transform(X_train), columns=predictors)

X_test = pd.DataFrame(knn_imputer.transform(X_test), columns=predictors)

X_test['Embarked'] = ['S' if l == 1 else 'C' if l == 2 else 'Q' for l in X_test['Embarked']]
X_test['Embarked'].value_counts()