Kaggle competition: Titanic - Machine Learning from Disaster
df_test, _, _ = proc_df(df_test_raw, skip_flds=["Id","groupId","matchId"] )<split>
train_data=pd.read_csv('/kaggle/input/titanic/train.csv')
 train_data
X_train, X_valid, y_train, y_valid = train_test_split(df, y, test_size=0.2)
 print(X_train.shape, X_valid.shape, y_train.shape, y_valid.shape )<compute_train_metric>
test_data=pd.read_csv('/kaggle/input/titanic/test.csv')
 test_data
def mae(x,y): return abs(x-y ).mean()
def fixWinPlacePerc(X,y):
y = round(y /(1.0/(X - 1)))/(X - 1)
 return y
def print_score(m):
res = [mae(fixWinPlacePerc(X_train["maxPlace"], m.predict(X_train)) , y_train),
mae(fixWinPlacePerc(X_valid["maxPlace"], m.predict(X_valid)) , y_valid)]
print(res )<train_model>
train_results = train_data["Survived"].copy()
train_data.drop("Survived", axis=1, inplace=True, errors="ignore")
 titanic = pd.concat([train_data, test_data])
 traindex = train_data.index
testdex = test_data.index
%%time
m = RandomForestRegressor(n_estimators=10, n_jobs=-1)
 m.fit(X_train, y_train)
 print_score(m )<set_options>
titanic[titanic['Cabin']=='B51 B53 B55']
gc.collect()<set_options>
titanic.index=range(len(titanic))
set_rf_samples(20000 )<train_model>
titanic[pd.isnull(titanic['Fare'])]
%%time
m = RandomForestRegressor(n_estimators=10, n_jobs=-1)
 m.fit(X_train, y_train)
 print_score(m )<train_model>
mean=titanic[titanic['Pclass']==3][titanic['Embarked']=='S'][titanic['Sex']=='male'][titanic['Age']>=40][titanic['SibSp']==0][titanic['Parch']==0]
mean['Fare'].describe()
%%time
m = RandomForestRegressor(n_estimators=80, max_features=0.5, n_jobs=-1)
 m.fit(X_train, y_train)
 print_score(m )<train_model>
titanic[['Fare']]=titanic[['Fare']].fillna(value=7.69 )
%%time
m = RandomForestRegressor(n_estimators=80, max_features=0.5, min_samples_leaf=3, n_jobs=-1)
 m.fit(X_train, y_train)
 print_score(m )<train_model>
titanic.isnull().sum()
%%time
m = RandomForestRegressor(n_estimators=120, max_features=0.5, min_samples_leaf=3, n_jobs=-1)
 m.fit(X_train, y_train)
 print_score(m )<train_model>
titanic["Title"] = titanic.Name.str.extract('([A-Za-z]+)\.', expand=False)
 pd.crosstab(titanic['Title'], titanic['Sex'])
%%time
reset_rf_samples()
m = RandomForestRegressor(n_estimators=120, max_features=0.5, min_samples_leaf=3, n_jobs=-1)
 m.fit(X_train, y_train)
 print_score(m )<set_options>
titanic['Title'] = titanic['Title'].replace('Mlle', 'Miss')
 titanic['Title'] = titanic['Title'].replace('Ms', 'Miss')
 titanic['Title'] = titanic['Title'].replace('Mme', 'Mrs')
gc.collect()<compute_test_metric>
titanic['Title'] = titanic['Title'].replace(['Lady', 'Countess','Capt','Col','Don', 'Dr', 'Major','Rev', 'Sir', 'Jonkheer', 'Dona'], 'Not married' )
fi = rf_feat_importance(m, df); fi<train_model>
titanic['Title'] = titanic['Title'].replace(['Mr', 'Mrs'], 'Married' )
%%time
reset_rf_samples()
m = RandomForestRegressor(n_estimators=120, max_features=0.5, min_samples_leaf=3, n_jobs=-1)
 m.fit(X_train, y_train)
 print_score(m )<train_model>
titanic["Surname"] = titanic.Name.str.split(',' ).str.get(0 )
def get_oob(X):
set_rf_samples(20000)
 m = RandomForestRegressor(n_estimators=120, max_features=0.5, min_samples_leaf=3, n_jobs=-1, oob_score=True)
 m.fit(X, y_train)
 return m.oob_score_<define_variables>
titanic=titanic.drop(['Name'],axis=1 )
cols = ["numGroups","killPoints","winPoints","killStreaks","kills","longestKill"]<drop_column>
titanic.Ticket.value_counts()
for col in cols:
print(col, get_oob(X_train.drop(labels=col, axis=1)) )<define_variables>
titanic['TicketFreq']=titanic.groupby('Ticket')['Ticket'].transform('count')
cols = ["killPoints","longestKill"]<drop_column>
titanic['customizedFare']=titanic.Fare/(titanic.TicketFreq*titanic.Pclass )
get_oob(X_train.drop(labels=cols, axis=1))<save_model>
titanic.isnull().sum()
np.save(f'{PATH_TMP}features_keep.npy', np.array(X_train.columns))<load_pretrained>
titanic.loc[(titanic.Age.isnull())&(titanic.Title=='Master'),'Age']=int(4.0 )
features_keep = np.load(f'{PATH_TMP}features_keep.npy' )<train_model>
titanic.isnull().sum()
%%time
reset_rf_samples()
m = RandomForestRegressor(n_estimators=120, max_features=0.5, min_samples_leaf=3, n_jobs=-1)
 m.fit(X_train, y_train)
 print_score(m )<prepare_x_and_y>
titanic.loc[(titanic.Age.isnull())&(titanic.Sex=='female'),'Age']=int(27.0 )
df, y, _ = proc_df(df_raw, 'winPlacePerc', skip_flds=["Id","groupId","matchId"] )<create_dataframe>
titanic[titanic['Title']!='Master'][titanic['Sex']=='male'].Age.median()
df = df[features_keep].copy()<create_dataframe>
titanic.loc[(titanic.Age.isnull())&(titanic.Sex=='male'),'Age']=int(30.0 )
df_test = df_test[features_keep].copy()<split>
titanic['Embarked'].fillna('S',inplace=True)
X_train, X_valid, y_train, y_valid = train_test_split(df, y, test_size=0.2)
 print(X_train.shape, X_valid.shape, y_train.shape, y_valid.shape )<train_model>
titanic.isnull().sum()
%%time
reset_rf_samples()
m = RandomForestRegressor(n_estimators=120, max_features=0.5, min_samples_leaf=3, n_jobs=-1)
 m.fit(X_train, y_train)
 print_score(m )<save_to_csv>
titanic['Family']=titanic['SibSp']+titanic['Parch']+1
titanic=titanic.drop(['SibSp','Parch'],axis=1 )
submission = pd.DataFrame({"Id": ids, "winPlacePerc": preds})
 submission.to_csv(f'{PATH_WORKING}submission.csv', index=False )<set_options>
def FamilyGroup(family):
a=''
if family<=1:
a='Solo'
elif family<=4:
a='Small'
else:
a='Large'
return a
titanic['FamilyGroup']=titanic['Family'].map(FamilyGroup)
 titanic=titanic.drop(['Family'],axis=1 )
!pip install ultimate
gc.enable() 
<define_variables>
titanic['Cabin']=titanic['Cabin'].notnull().astype(str ).str[0]
INPUT_DIR = ".. /input/"<load_from_csv>
titanic['Cabin'].isnull().sum()
def feature_engineering(is_train=True):
if is_train:
print("processing train_V2.csv")
 df = pd.read_csv(INPUT_DIR + 'train_V2.csv')
 df = df[df['maxPlace'] > 1]
else:
print("processing test_V2.csv")
 df = pd.read_csv(INPUT_DIR + 'test_V2.csv')
 state('totalDistance')
 s = timer()
df['totalDistance'] = df['rideDistance'] + df["walkDistance"] + df["swimDistance"]
e = timer()
state('totalDistance', False, e - s)
 state('rankPoints')
 s = timer()
df['rankPoints'] = np.where(df['rankPoints'] <= 0 ,0 , df['rankPoints'])
 e = timer()
state('rankPoints', False, e-s)
 target = 'winPlacePerc'
features = list(df.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchDuration")
 features.remove("matchType")
 y = None
if is_train:
y = np.array(df.groupby(['matchId','groupId'])[target].agg('mean'), dtype=np.float64)
 features.remove(target)
 print("get group mean feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('mean')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
if is_train: df_out = agg.reset_index() [['matchId','groupId']]
else: df_out = df[['matchId','groupId']]
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_mean", "_mean_rank"], how='left', on=['matchId', 'groupId'])
 print("get group max feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('max')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_max", "_max_rank"], how='left', on=['matchId', 'groupId'])
 print("get group min feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('min')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_min", "_min_rank"], how='left', on=['matchId', 'groupId'])
 print("get group size feature")
 agg = df.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_out = df_out.merge(agg, how='left', on=['matchId', 'groupId'])
 print("get match mean feature")
 agg = df.groupby(['matchId'])[features].agg('mean' ).reset_index()
df_out = df_out.merge(agg, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 print("get match size feature")
 agg = df.groupby(['matchId'] ).size().reset_index(name='match_size')
 df_out = df_out.merge(agg, how='left', on=['matchId'])
 df_out.drop(["matchId", "groupId"], axis=1, inplace=True)
 X = np.array(df_out, dtype=np.float64)
 del df, df_out, agg, agg_rank
gc.collect()
return X, y 
<normalization>
titanic.Cabin.isnull()
%%time
x_train, y = feature_engineering(True)
 scaler = preprocessing.MinMaxScaler(feature_range=(-1, 1), copy=False ).fit(x_train )<train_model>
titanic=titanic.drop(['Fare','PassengerId','TicketFreq','Title','Ticket','Surname'], axis=1)
print("x_train", x_train.shape, x_train.max() , x_train.min())
 scaler.transform(x_train)
 print("x_train", x_train.shape, x_train.max() , x_train.min() )<choose_model_class>
def AgeGroup(age):
a=''
if age<=15:
a='Child'
elif age<=30:
a='Young'
elif age<=50:
a='Adult'
else:
a='Old'
return a
titanic['AgeGroup']=titanic['Age'].map(AgeGroup)
 titanic=titanic.drop(['Age'],axis=1 )
%%time
NN_model = Sequential()
NN_model.add(Dense(x_train.shape[1], input_dim = x_train.shape[1], activation='relu'))
NN_model.add(Dense(136, activation='relu'))
NN_model.add(Dense(136, activation='relu'))
NN_model.add(Dense(136, activation='relu'))
NN_model.add(Dense(136, activation='relu'))
NN_model.add(Dense(1, activation='linear'))
NN_model.compile(loss='mean_absolute_error', optimizer='adam', metrics=['mean_absolute_error'])
 NN_model.summary() 
<train_model>
titanic_data=pd.get_dummies(titanic,columns=['Sex','Embarked','FamilyGroup','AgeGroup','Cabin'] )
%%time
NN_model.fit(x=x_train, y=y, batch_size=1000,
epochs=30, verbose=1, callbacks=callbacks_list,
validation_split=0.15, validation_data=None, shuffle=True,
class_weight=None, sample_weight=None, initial_epoch=0,
steps_per_epoch=None, validation_steps=None)
 del x_train, y
gc.collect()<normalization>
titanic_data.loc[891:1308]
x_test, _ = feature_engineering(False)
 scaler.transform(x_test)
 print("x_test", x_test.shape, x_test.max() , x_test.min())
 np.clip(x_test, out=x_test, a_min=-1, a_max=1)
 print("x_test", x_test.shape, x_test.max() , x_test.min() )<predict_on_test>
train_df = titanic_data.loc[0:890]
train_df['Survived'] = train_results
test_df = titanic_data.loc[891:1308]
%%time
pred = NN_model.predict(x_test)
 del x_test
gc.collect()<prepare_output>
X=train_df.drop(['Survived'],axis=1)
 X.head()
y=train_df.Survived
y.head()
pred = pred.reshape(-1)
 pred =(pred + 1)/ 2<load_from_csv>
df_test = pd.read_csv(INPUT_DIR + 'test_V2.csv' )<feature_engineering>
xgbr=XGBClassifier(n_estimators=2800,
min_child_weight=0.1,
learning_rate=0.002,
max_depth=2,
subsample=0.47,
colsample_bytree=0.35,
gamma=0.4,
reg_lambda=0.4,
random_state=42,
n_jobs=-1,)
 xgbr.fit(X,y)
 predicts=xgbr.predict(test_df)
<feature_engineering><EOS> 
submission = pd.DataFrame({
"PassengerId": test_data["PassengerId"],
"Survived": predicts
})
 submission.Survived = submission.Survived.round().astype("int")
 submission.to_csv('titanic.csv', index=False)
 print("Submitted Successfully")