text_html_output_1.png
from category_encoders import TargetEncoder

from sklearn.impute import KNNImputer

from sklearn.preprocessing import PowerTransformer

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[['Sex', 'Embarked', 'Pclass', 'Title']] = X_train[['Sex', 'Embarked', 'Pclass', 'Title']].astype('category')

X_test[['Sex', 'Embarked', 'Pclass', 'Title']] = X_test[['Sex', 'Embarked', 'Pclass', 'Title']].astype('category')

X_train_ohe = pd.get_dummies(X_train)

X_test_ohe = pd.get_dummies(X_test)

encoder = TargetEncoder(return_df=True)
X_train_te = encoder.fit_transform(X_train, y_train)

X_test_te = encoder.transform(X_test)

num = ['Age', 'Parch', 'SibSp', 'Fare']
power = PowerTransformer()
train_power = pd.DataFrame(power.fit_transform(X_train[num]), columns=num)

test_power = pd.DataFrame(power.transform(X_test[num]), columns=num)

X_train_ohe_power = pd.concat([train_power[num], X_train_ohe.drop(num, axis=1)], axis=1)

X_test_ohe_power = pd.concat([test_power[num], X_test_ohe.drop(num, axis=1)], axis=1)

X_train_te_power = pd.concat([train_power[num], X_train_te.drop(num, axis=1)], axis=1)

X_test_te_power = pd.concat([test_power[num], X_test_te.drop(num, axis=1)], axis=1)
X_test_te_power.head()