Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<feature_engineering>
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import cross_val_score
import re
import xgboost as xgb
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
df_train['Age_cat'] = 0
df_train.loc[df_train['Age'] < 21, 'Age_cat'] = 0
df_train.loc[(21 <= df_train['Age'])&(df_train['Age'] < 27), 'Age_cat'] = 1
df_train.loc[(27 <= df_train['Age'])&(df_train['Age'] < 33), 'Age_cat'] = 2
df_train.loc[(33 <= df_train['Age'])&(df_train['Age'] < 39), 'Age_cat'] = 3
df_train.loc[39 <= df_train['Age'], 'Age_cat'] = 4<feature_engineering>
train = pd.read_csv('.. /input/titanic/train.csv')
 test = pd.read_csv('.. /input/titanic/test.csv')
 print("Train set size:", train.shape)
 print("Test set size:", test.shape )
df_test['Age_cat'] = 0
df_test.loc[df_train['Age'] < 21, 'Age_cat'] = 0
df_test.loc[(21 <= df_train['Age'])&(df_test['Age'] < 27), 'Age_cat'] = 1
df_test.loc[(27 <= df_train['Age'])&(df_test['Age'] < 33), 'Age_cat'] = 2
df_test.loc[(33 <= df_train['Age'])&(df_test['Age'] < 39), 'Age_cat'] = 3
df_test.loc[39 <= df_train['Age'], 'Age_cat'] = 4<count_values>
test_ID = test['PassengerId']
train=train.drop(columns=['PassengerId',"Ticket","Cabin"])
 test=test.drop(columns=["PassengerId","Ticket","Cabin"] )
print(df_train['Age_cat'].value_counts() )<categorify>
train['FamilySize'] = train['SibSp'] + train['Parch'] + 1
test['FamilySize'] = test['SibSp'] + test['Parch'] + 1
df_train = pd.get_dummies(df_train, columns=['Age_cat'], prefix='Age_cat')
 df_test = pd.get_dummies(df_test, columns=['Age_cat'], prefix='Age_cat' )<drop_column>
train=train.drop(columns=["SibSp","Parch"])
 test=test.drop(columns=["SibSp","Parch"] )
df_train.drop(['Age'], axis=1, inplace=True)
 df_test.drop(['Age'], axis=1, inplace=True)
 df_train.head()<categorify>
for dataset in [train,test]:
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1
df_train['Initial'] = df_train['Initial'].map({'Master': 0, 'Miss': 1, 'Mr': 2, 'Mrs': 3, 'Other': 4})
 df_test['Initial'] = df_test['Initial'].map({'Master': 0, 'Miss': 1, 'Mr': 2, 'Mrs': 3, 'Other': 4})
<count_values>
train['Embarked'] = train['Embarked'].fillna('S')
 train['Embarked'] = train['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 test['Embarked'] = test['Embarked'].fillna('S')
 test['Embarked'] = test['Embarked'].map({'S': 0, 'C': 1, 'Q': 2} ).astype(int)
 train['Sex'] = train['Sex'].map({'male': 0, 'female': 1})
 test['Sex'] = test['Sex'].map({'male': 0, 'female': 1})
df_train['Embarked'].unique()
df_train['Embarked'].value_counts()<categorify>
def get_title(name):
title_search = re.search('([A-Za-z]+)\.', name)
 if title_search:
return title_search.group(1)
 return ""
for dataset in [train,test]:
dataset['Title'] = dataset['Name'].apply(get_title)
 for dataset in [train,test]:
dataset['Title'] = dataset['Title'].replace(['Lady', 'Countess','Capt', 'Col','Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 title_mapping = {"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5}
dataset['Title'] = dataset['Title'].map(title_mapping)
 dataset['Title'] = dataset['Title'].fillna(0)
df_train['Embarked'] = df_train['Embarked'].map({'C': 0, 'Q': 1, 'S': 2})
 df_test['Embarked'] = df_test['Embarked'].map({'C': 0, 'Q': 1, 'S': 2} )<count_missing_values>
train=train.drop(columns=["Name"])
 test=test.drop(columns=["Name"] )
df_train['Embarked'].isnull().any()<categorify>
y=train.Survived
train=train.drop(columns=["Survived"])
 all_data = pd.concat(( train,test))
df_train['Sex'] = df_train['Sex'].map({'female': 0, 'male': 1})
 df_test['Sex'] = df_test['Sex'].map({'female': 0, 'male': 1} )<categorify>
all_data = all_data.fillna(all_data.mean() )
df_train = pd.get_dummies(df_train, columns=['Initial'], prefix='Initial')
 df_test = pd.get_dummies(df_test, columns=['Initial'], prefix='Initial' )<categorify>
all_data.columns[all_data.isnull().any() ]
df_train = pd.get_dummies(df_train, columns=['Embarked'], prefix='Embarked')
 df_test = pd.get_dummies(df_test, columns=['Embarked'], prefix='Embarked' )<drop_column>
Ntrain = all_data[:train.shape[0]]
test = all_data[train.shape[0]:]
df_train.drop(['PassengerId', 'Name', 'SibSp', 'Parch'], axis=1, inplace=True)
 df_test.drop(['PassengerId', 'Name', 'SibSp', 'Parch'], axis=1, inplace=True )<set_options>
def cv(model,cv=10):
cvx= np.sqrt(cross_val_score(model, Ntrain, y, scoring="accuracy", cv=10))
return(cvx )
%matplotlib inline 
<prepare_x_and_y>
ran_for = RandomForestClassifier(n_estimators=50)
 cv(ran_for, cv=10 ).mean()
X_train = df_train.drop('Survived', axis=1 ).values
target_label = df_train['Survived'].values
X_test = df_test.values<split>
xgb_cl= xgb.XGBClassifier(colsample_bytree= 0.70,learning_rate=0.03, max_depth= 5, min_child_weight= 4, n_estimators= 50, nthread= 4, subsample= 0.7)
 cv(xgb_cl ).mean()
X_tr, X_vld, y_tr, y_vld = train_test_split(X_train,target_label, test_size=0.3, random_state=2018 )<compute_train_metric>
gbm = xgb.XGBClassifier(
 n_estimators= 200,
max_depth= 4,
min_child_weight= 2,
gamma=0.9,
subsample=0.8,
colsample_bytree=0.8,
objective= 'binary:logistic',
nthread= -1,
scale_pos_weight=1)
 cv(gbm ).mean()
<compute_train_metric><EOS> 
sub = pd.DataFrame()
sub['PassengerId'] = test_ID
xgb_cl.fit(Ntrain,y)
 ran_for.fit(Ntrain,y)
 gbm.fit(Ntrain,y)
 sub['Survived'] =(0.6*xgb_cl.predict(test)+0.3*gbm.predict(test)+0.1*ran_for.predict(test))
sub['Survived'] =sub['Survived'].apply(lambda f: 1 if f>=0.5 else 0)
 sub.to_csv('ensbleX.csv',index=False )