Kaggle competition: Titanic - Machine Learning from Disaster
<rename_columns>
train_df = pd.read_csv('.. /input/train.csv', index_col='PassengerId')
 test_df = pd.read_csv('.. /input/test.csv', index_col='PassengerId')
 train_df.head()
train.set_index("Id", inplace=True)
 train.index.name = "Id"<prepare_x_and_y>
print('Train dataset:')
 print(train_df.isna().sum() [train_df.isna().any() ])
 print('
Test dataset:')
 print(test_df.isna().sum() [test_df.isna().any() ] )
temp_1 = train.loc[:, feature_1]
temp_2 = train.loc[:, feature_2]<feature_engineering>
cat_feat = ['Sex', 'Embarked']
for cf in cat_feat:
if cf != cat_feat[0]:
print()
print(train_df[cf].value_counts() / train_df[cf].count() )
temp_1.groupby("matchId" ).transform(minmax)
 for i in temp_2.columns[:4]:
temp_2[i] =(temp_2[i] - min(temp_2[i])) /(max(temp_2[i])- min(temp_2[i]))<merge>
train_df = train_df.drop(['Ticket', 'Cabin'], axis=1)
 test_df = test_df.drop(['Ticket', 'Cabin'], axis=1 )
X = pd.merge(temp_1, temp_2, on="Id")
 X = pd.merge(X, train.loc[:, ["matchType_1", "winPlacePerc"]], on="Id" )<feature_engineering>
extract_title = lambda df: df.Name.str.extract(r'([A-Za-z]+)\.', expand=False)
 train_df['Title'] = extract_title(train_df)
 test_df['Title'] = extract_title(test_df)
 train_df.Title.value_counts()
<feature_engineering>
def replace_titles(df):
df['Title'] = df['Title'].replace(['Lady', 'Countess','Capt', 'Col',\
	'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 df['Title'] = df['Title'].replace('Mlle', 'Miss')
 df['Title'] = df['Title'].replace('Ms', 'Miss')
 df['Title'] = df['Title'].replace('Mme', 'Mrs')
 return df
train_df = replace_titles(train_df)
 test_df = replace_titles(test_df)
 train_df = train_df.drop('Name', axis=1)
 test_df = test_df.drop('Name', axis=1 )
<feature_engineering>
def add_family(df):
df['Family'] = df.SibSp + df.Parch
return df
train_df = add_family(train_df)
 test_df = add_family(test_df )
<filter>
family_feat = ['SibSp', 'Parch']
train_df = train_df.drop(family_feat, axis=1)
 test_df = test_df.drop(family_feat, axis=1 )
<count_values>
def fill_ages(df):
df.loc[(df.Age.isnull())&(df.Title == 'Master'), 'Age'] = df[df.Title == 'Master'].Age.median()
df.loc[(df.Age.isnull())&(df.Title != 'Master'), 'Age'] = df[df.Title != 'Master'].Age.median()
return df
def fill_embarked(df):
df['Embarked'] = df.Embarked.fillna('S')
 return df
train_df = fill_ages(fill_embarked(train_df))
test_df = fill_ages(fill_embarked(test_df))
test_df['Fare'] = test_df.Fare.fillna(test_df.Fare.median() )
print("Name: ", feature, "
Count: ", len(feature))<define_variables>
def add_bands(df):
df['AgeBand'] = pd.cut(df.Age, bins=5)
 df['FareBand'] = pd.qcut(df.Fare, q=4)
 return df
train_df = add_bands(train_df)
 test_df = add_bands(test_df)
 train_df = train_df.drop(['Age', 'Fare'], axis=1)
 test_df = test_df.drop(['Age', 'Fare'], axis=1 )
list_feat = ["assists", "boosts", "damageDealt", "DBNOs", "headshotKills", "heals",
"killPlace", "killPoints", "kills", "killStreaks", "longestKill",
"matchDuration", "maxPlace", "rankPoints", "revives", "rideDistance",
"roadKills", "swimDistance", "teamKills", "vehicleDestroys", "walkDistance",
"weaponsAcquired", "winPoints", "player", "fpp", "tpp"]<define_variables>
def factorize(df):
df['Sex'] = df.Sex.factorize() [0]
df['AgeBand'] = df.AgeBand.factorize(sort=True)[0]
df['FareBand'] = df.FareBand.factorize(sort=True)[0]
return df
def one_hot_encode(df):
return pd.get_dummies(df, columns=['Embarked', 'Title'])
 train_df = one_hot_encode(factorize(train_df))
test_df = one_hot_encode(factorize(test_df))
train_df.head()
list_feat_1 = ["assists", "boosts", "damageDealt", "DBNOs", "headshotKills", "heals",
"killPlace", "killPoints", "kills", "killStreaks", "longestKill",
"matchDuration", "maxPlace", "rankPoints", "revives", "rideDistance",
"roadKills", "swimDistance", "teamKills", "vehicleDestroys", "walkDistance",
"weaponsAcquired", "winPoints", "player", "fpp", "tpp", "matchId"]<prepare_x_and_y>
X = train_df.drop('Survived', axis=1)
 y = train_df.Survived
train = X<choose_model_class>
param_grid = {
'n_estimators': [90, 95, 100],
'learning_rate': [0.009, 0.01],
'max_depth': [3],
'min_child_weight' :range(1, 3),
'gamma': [0, 0.001, 0.005]
}
gsearch = GridSearchCV(cv=5, estimator=XGBClassifier() , param_grid=param_grid, n_jobs=-1)
 gsearch.fit(X, y)
 gsearch.best_params_, gsearch.best_score_
model_1 = keras.models.Sequential()
model_1.add(Dense(32, input_dim=len(list_feat), activation="elu", kernel_initializer="he_normal"))
model_1.add(Dense(64, activation="elu", kernel_initializer="he_normal"))
model_1.add(Dense(128, activation="elu", kernel_initializer="he_normal"))
model_1.add(keras.layers.Dropout(0.25))
model_1.add(Dense(256, activation="elu", kernel_initializer="he_normal"))
model_1.add(Dense(256, activation="elu", kernel_initializer="he_normal"))
model_1.add(keras.layers.Dropout(0.25))
model_1.add(Dense(128, activation="elu", kernel_initializer="he_normal"))
model_1.add(Dense(64, activation="elu", kernel_initializer="he_normal"))
model_1.add(Dense(32, activation="elu", kernel_initializer="he_normal"))
model_1.add(keras.layers.Dropout(0.25))
model_1.add(Dense(1, activation="sigmoid"))
model_1.compile(optimizer="RMSprop", loss='MAE', metrics=["MAE"] )<prepare_x_and_y>
param_grid = {
'C': np.arange(0.99, 1.1, 0.01)
 }
model = SVC(gamma='auto')
 gsearch = GridSearchCV(cv=5, estimator=model, param_grid=param_grid, n_jobs=-1)
 gsearch.fit(X, y)
 print(f'Best params: {gsearch.best_params_}')
 print(f'Best score: {gsearch.best_score_}')
x_train = train.loc[train.matchType_1 == "solo", list_feat]
y_train = train.loc[train.matchType_1 == "solo", ["winPlacePerc"]]<train_model>
param_grid = {
'n_estimators': range(200, 300, 50),
'max_depth': range(2, 5),
'min_samples_split': [2, 3, 4],
'bootstrap': [True, False]
}
model = RandomForestClassifier()
gsearch = GridSearchCV(cv=5, estimator=model, param_grid=param_grid, n_jobs=-1)
 gsearch.fit(X, y)
 print(f'Best params: {gsearch.best_params_}')
 print(f'Best score: {gsearch.best_score_}' )
model_1.fit(x=x_train, y=y_train, epochs=50, batch_size=10000, validation_split=0.2, shuffle=True)
 model_1.fit(x=x_train, y=y_train, epochs=30, batch_size=2000, validation_split=0.2, shuffle=True )<train_model>
model = SVC(C = 1.26, gamma = 0.09)
 model.fit(X, y)
 predictions = model.predict(test_df )
<save_model>
submit_df = pd.DataFrame({
'PassengerId': test_df.index.values,
'Survived': predictions
})
 submit_df.to_csv('submission.csv', index=False )