text_html_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_train['Title'] = [s.split(',')[1].split('.')[0].strip() for s in train['Name']]
X_train['Title'] = X_train['Title'].astype('category')
replacements = {'Mlle': 'Miss', 'Mme': 'Mrs', 'Ms': 'Mrs'}
X_train['Title'] = X_train['Title'].replace(replacements)
counts = X_train['Title'].value_counts()
mask = X_train['Title'].isin(counts[counts < 10].index)
X_train['Title'][mask] = 'Other'
X_train['Title'].value_counts()

X_test['Title'] = [s.split(',')[1].split('.')[0].strip() for s in test['Name']]
X_test['Title'] = X_test['Title'].astype('category')
replacements = {'Mlle': 'Miss', 'Mme': 'Mrs', 'Ms': 'Mrs'}
X_test['Title'] = X_test['Title'].replace(replacements)
counts = X_test['Title'].value_counts()
mask = X_test['Title'].isin(counts[counts < 10].index)
X_test['Title'][mask] = 'Other'
X_test['Title'].value_counts()