Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<categorify>
import numpy as np
import pandas as pd
import category_encoders as ce
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.impute import SimpleImputer
%%time
print('merging data...')
 train = train_transaction.merge(train_identity, how='left', left_index=True, right_index=True)
 test = test_transaction.merge(test_identity, how='left', left_index=True, right_index=True)
 print('dropping target...')
 y_train = train['isFraud'].copy()
del train_transaction, train_identity, test_transaction, test_identity
X_train = train.drop('isFraud', axis=1)
 X_test = test.copy()
del train, test
print('fillnas...')
 X_train = X_train.fillna(-999)
 X_test = X_test.fillna(-999)
 gc.collect()
print('Label Encoding...')
 for f in X_train.columns:
if X_train[f].dtype=='object' or X_test[f].dtype=='object':
lbl = preprocessing.LabelEncoder()
lbl.fit(list(X_train[f].values)+ list(X_test[f].values))
X_train[f] = lbl.transform(list(X_train[f].values))
X_test[f] = lbl.transform(list(X_test[f].values))
print('selecting features...')
 X_train = X_train[features]
X_test=X_test[features]
print('Done' )<split>
data = pd.read_csv('/kaggle/input/titanic/train.csv')
 print(data.columns)
 X_test = pd.read_csv('/kaggle/input/titanic/test.csv')
 data.dropna(axis=0, subset=["Embarked"], inplace=True)
 X = data.drop(columns="Survived")
 y = data.Survived.copy()
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.30, random_state=42)
 missing = [col for col in data.columns if data[col].isna().any() ]
print("Missing:", missing)
 print("no_missing:", [(col, str(sum(data[col].isna())) + '/' + str(data.size)) for col in missing])
 test_missing = [col for col in X_test.columns if X_test[col].isna().any() ]
print("Test Missing:", test_missing)
 print("no_missing:", [(col, str(sum(X_test[col].isna())) + '/' + str(X_test.size)) for col in test_missing])
 X_train["Age"] = X_train["Age"].fillna(X_train["Age"].median())
 X_valid["Age"] = X_valid["Age"].fillna(X_train["Age"].median())
 X_test["Age"] = X_test["Age"].fillna(X_train["Age"].median())
 X_train["Cabin"] = X_train["Cabin"].fillna("NO_CABIN")
 X_valid["Cabin"] = X_valid["Cabin"].fillna("NO_CABIN")
 X_test["Cabin"] = X_test["Cabin"].fillna("NO_CABIN")
 X_test["Fare"] = X_test["Fare"].fillna(X_train["Fare"].median())
 def extract_name_title(name):
last_name, rest = name.split(", ", 1)
 title, rest = rest.split(".", 1)
 return title
X_train["Name"] = X_train["Name"].apply(extract_name_title)
 X_valid["Name"] = X_valid["Name"].apply(extract_name_title)
 X_test["Name"] = X_test["Name"].apply(extract_name_title)
 print("Unique train titles:", X_train["Name"].unique())
 print("Unique valid titles:", X_valid["Name"].unique())
 X_train["Sex"] = X_train["Sex"].apply(lambda x: 1 if x == "male" else 0, convert_dtype=True)
 X_valid["Sex"] = X_valid["Sex"].apply(lambda x: 1 if x == "male" else 0, convert_dtype=True)
 X_test["Sex"] = X_test["Sex"].apply(lambda x: 1 if x == "male" else 0, convert_dtype=True)

print(X_train.head(20))
categorical = [col for col in X_train.columns if X_train[col].dtype == "object"]
print("Categorical:", categorical)
 target_enc = ce.TargetEncoder(cols=categorical)
 X_train = target_enc.fit_transform(X_train, y_train)
 X_valid = target_enc.transform(X_valid, y_valid)
 X_test = target_enc.transform(X_test, None)
 print(X_train.head(20))
<set_options><EOS> 
model = XGBClassifier(n_estimators=500)
 model.fit(X_train, y_train)
 predictions = model.predict(X_valid)
 print(y_valid.head(20))
print(predictions[:20])
 print("Accuracy:" , 1-mean_absolute_error(predictions, y_valid))
print("F1:", f1_score(predictions, y_valid))
selector = SelectKBest(f_classif, k=5)
 X_new = selector.fit_transform(X_train, y_train)
 selected_features = pd.DataFrame(selector.inverse_transform(X_new),
index=X_train.index,
columns=X_train.columns)
 selected_features.head(30)
 test_predictions = model.predict(X_test)
 submission = pd.DataFrame({
"PassengerId": X_test["PassengerId"],
"Survived": test_predictions
})
 submission.to_csv('submission.csv', index=False )