Kaggle competition: Titanic - Machine Learning from Disaster
def min_by_team(df,df_Group, features):
print("Working on Min by Team features...")
 inittime = dt.datetime.now()
agg = df_Group[features].min()
print("Features : Min Created in ", dt.datetime.now() - inittime)
 return df.merge(agg, suffixes=['', '_min'], how='left', on=['matchId', 'groupId'])
 def max_by_team(df,df_Group, features):
print("Working on Max by Team features...")
 inittime = dt.datetime.now()
agg = df_Group[features].max()
print("Features : MAX Created in ", dt.datetime.now() - inittime)
 return df.merge(agg, suffixes=['', '_max'], how='left', on=['matchId', 'groupId'])
 def sum_by_team(df,df_Group, features):
print("Working on Sum by Team features...")
 inittime = dt.datetime.now()
agg = df_Group[features].sum()
print("Features : SUM Created in ", dt.datetime.now() - inittime)
 return df.merge(agg, suffixes=['', '_sum'], how='left', on=['matchId', 'groupId'])
 def median_by_team(df,df_Group, features):
print("Working on Median by Team features...")
 inittime = dt.datetime.now()
agg = df_Group[features].median()
print("Features : MEDIAN Created in ", dt.datetime.now() - inittime)
 return df.merge(agg, suffixes=['', '_median'], how='left', on=['matchId', 'groupId'])
 def mean_by_team(df,df_Group, features):
print("Working on Mean by Team features...")
 inittime = dt.datetime.now()
agg = df_Group[features].mean()
print("Features : MEAN Created in ", dt.datetime.now() - inittime)
 return df.merge(agg, suffixes=['', '_mean'], how='left', on=['matchId', 'groupId'])
 def rank_by_team(df,df_Group, features):
print("Working on Rank by Team features...")
 inittime = dt.datetime.now()
agg = df_Group[features].mean()
print("Means Created")
 agg1 = agg.groupby('matchId')[features].rank(pct=True)
 print("Features : RANK Created in ", dt.datetime.now() - inittime)
 df= df.merge(agg, suffixes=['', '_mean'], how='left', on=['matchId', 'groupId'])
 return df.merge(agg1, suffixes=['', '_mean_rank'], how='left', on=['matchId', 'groupId'])
 print("Functions created" )<prepare_x_and_y>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 test_data = pd.read_csv("/kaggle/input/titanic/test.csv" )
Y_Column = 'winPlacePerc'
ColumnList = ['assists', 'boosts', 'damageDealt', 'DBNOs',
'headshotKills', 'heals', 'killPlace', 'killPoints', 'kills',
'killStreaks', 'longestKill', 'maxPlace', 'numGroups', 'revives',
'rideDistance', 'roadKills', 'swimDistance', 'teamKills',
'vehicleDestroys', 'walkDistance', 'weaponsAcquired', 'winPoints']
df['headshotKills_over_kills'] = df['headshotKills'] / df['kills']
df['headshotKills_over_kills'].fillna(0, inplace=True)
 df['headshotKills_over_kills'].replace(np.inf, 0, inplace=True)
 df['killPlace_over_maxPlace'] = df['killPlace'] / df['maxPlace']
df['killPlace_over_maxPlace'].fillna(0, inplace=True)
 df['killPlace_over_maxPlace'].replace(np.inf, 0, inplace=True)
 df['walkDistance_over_heals'] = df['walkDistance'] / df['heals']
df['walkDistance_over_heals'].fillna(0, inplace=True)
 df['walkDistance_over_heals'].replace(np.inf, 0, inplace=True)
 df['walkDistance_over_kills'] = df['walkDistance'] / df['kills']
df['walkDistance_over_kills'].fillna(0, inplace=True)
 df['walkDistance_over_kills'].replace(np.inf, 0, inplace=True)
 df= reduce_mem_usage(df)
 cols_to_drop = ['Id', 'groupId', 'matchId', 'winPlacePerc','Type','matchType']
features = [col for col in ColumnList if col not in cols_to_drop]
df_Group = df.groupby(['matchId','groupId'])
 df= reduce_mem_usage(df)
 df= median_by_team(df,df_Group,features)
 df= reduce_mem_usage(df)
 df= rank_by_team(df,df_Group,features)
 df= reduce_mem_usage(df)
 print("New variables created")
 for colname in ColumnList:
df[colname+ '_1']= df[colname]/(max(df[colname])- min(df[colname]))
df= df.drop(columns= [colname])
 df= reduce_mem_usage(df)
 print(colname,'_1',' created')
 df['totalDistance'] = df['rideDistance_1'] + df['walkDistance_1'] + df['swimDistance_1']
print("New variables created : totalDistance")
 df['kills_assists'] =(df['kills_1'] + df['assists_1'])
 print("New variables created : kills_assists")
 df['healthitems'] = df['heals_1'] + df['boosts_1']
print("New variables created : healthitems")
 df['kills_']= df['kills_1'] + df['longestKill_1']+ df['killStreaks_1']
print("New variables created : kills_")
 df['killtypes']= df['headshotKills_1'] + df['roadKills_1']+ df['teamKills_1']+ df['vehicleDestroys_1']
print("New variables created : killtypes")
 df['Others']= df['damageDealt_1'] + df['DBNOs_1']+ df['revives_1'] + df['weaponsAcquired_1']
print("New variables created : Others")
 df['Points_'] = df['killPoints_1'] + df['winPoints_1']
print("New variables created : Points_")
 df= reduce_mem_usage(df)
 print(df.columns)
 ColumnList = df.columns.tolist()
print(ColumnList)
 ColumnList.remove('Id')
 ColumnList.remove('groupId')
 ColumnList.remove('matchId')
 ColumnList.remove('Type')
 ColumnList.remove('winPlacePerc')
 ColumnList.remove('matchType')
 df= reduce_mem_usage(df)
 df_Train = df[df['Type']== 'Train']
df_Test = df[df['Type']== 'Test']
print("Train and Test Data sets created")
 df_Train= df_Train.set_index(['Id', 'groupId', 'matchId','Type'])
 df_Test= df_Test.set_index(['Id', 'groupId', 'matchId','Type'])
 print("Indexes set")
 X_Train = df_Train[ColumnList]
Y_Train = df_Train[Y_Column]
X_test = df_Test[ColumnList]
print(X_Train.shape, Y_Train.shape, X_test.shape)
 del df, df_Train, df_Test<import_modules>
params = {
'axes.labelsize': "large",
'xtick.labelsize': 'medium',
'legend.fontsize': 'medium',
'legend.loc': "best",
}
plot.rcParams.update(params)
 train_data['Died'] = 1 - train_data['Survived']
import lightgbm as lgb
from sklearn.metrics import mean_absolute_error<train_model>
title_mapping = {"Mrs": 4, "Miss": 3, "Mr": 0, "Noble": 2,"Crew": 1}
train_data['Title Map'] = train_data['Titles'].map(title_mapping)
 test_data['Title Map'] = test_data['Titles'].map(title_mapping )
%%time
starttime = dt.datetime.now()
d_train1 = lgb.Dataset(X_Train, label=Y_Train.values)
 params = {}
params['learning_rate'] = 0.09
params['boosting_type'] = 'gbdt'
params['objective'] = 'regression'
params['metric'] = 'mae'
params['sub_feature'] = 0.8
params['num_leaves'] = 1000
params['min_data'] = 1
params['max_depth'] = 400
params['min_gain_to_split']= 0.0000001
clf1 = lgb.train(params, d_train1, 1000)
 print("Model created in ", dt.datetime.now() - starttime)
 y_pred=clf1.predict(X_test)
 print(y_pred)
<merge>
train_data["Fare"] = train_data["Fare"].fillna(train_data["Fare"].median())
 test_data["Fare"] = test_data["Fare"].fillna(test_data["Fare"].median() )
del X_Train
X_test = X_test.reset_index()
X_test["winPlacePerc"] = y_pred
X_test = X_test[['Id',"winPlacePerc"]]
df_test = df_test[['Id','groupId']]
df_test= df_test.merge(X_test, on = ['Id'])
 del X_test
df_test["winPlacePerc"]= df_test["winPlacePerc"].clip(lower = 0.0, upper= 1.0)
 print(df_test.head() )<save_to_csv>
train_data['FareGroup'] = pd.cut(train_data['Fare'],3)
 print(train_data[['FareGroup', 'Survived']].groupby('FareGroup', as_index=False ).mean().sort_values('Survived', ascending=False))
df_test[['Id',"winPlacePerc"]].to_csv("submission.csv", index=False )<load_from_csv>
def group_fare(fare):
if fare <= 170: return 0
if fare > 170 and fare <= 340: return 1
if fare > 340: return 2
for i, row in train_data.iterrows() :
train_data.at[i,'Fare Group'] = group_fare(row["Fare"])
 for i, row in test_data.iterrows() :
test_data.at[i,'Fare Group'] = group_fare(row["Fare"] )
print('-' * 80)
 print('train')
 train = import_data('.. /input/train.csv')
 print('-' * 80)
 print('test')
 test = import_data('.. /input/test.csv')
 print('-' * 80)
 print('sample_submission')
 submission = import_data('.. /input/sample_submission.csv' )<count_missing_values>
def calc_age(df, cl, sx, tl):
a = df.groupby(["Pclass", "Sex", "Titles"])["Age"].median()
return a[cl][sx][tl]
age_train = train_data.copy()
age_train.drop('PassengerId', axis=1, inplace=True)
 age_train.drop('Survived',axis=1, inplace=True)
 age_test = test_data.copy()
age_test.drop('PassengerId', axis=1, inplace=True)
 df = pd.concat([age_train, age_test], sort=False ).reset_index(drop=True)
 for i, row in train_data.iterrows() :
if pd.isna(row['Age']):
newage =(calc_age(df, row["Pclass"], row["Sex"], row["Titles"]))
train_data.at[i,'Age'] = newage
else: continue
for i, row in test_data.iterrows() :
if pd.isna(row['Age']):
newage =(calc_age(df, row["Pclass"], row["Sex"], row["Titles"]))
test_data.at[i,'Age'] = newage
else: continue
train.isnull().sum()<count_missing_values>
train_data['AgeGroup'] = pd.cut(train_data['Age'],5)
 print(train_data[['AgeGroup', 'Survived']].groupby('AgeGroup', as_index=False ).mean().sort_values('Survived', ascending=False))
train.isnull().sum()<prepare_x_and_y>
train_data["Family"] = train_data["SibSp"] + train_data["Parch"]
test_data["Family"] = test_data["SibSp"] + test_data["Parch"]
y = train['winPlacePerc']
columns_todrop = ['winPlacePerc']
train_sel = train.drop(columns_todrop,axis=1 )<groupby>
train_data["Embarked"] = train_data["Embarked"].fillna('S' )
train_mean = train_sel.groupby(['matchId','groupId'] ).mean()
test_mean = test.groupby(['matchId','groupId'] ).mean()
train_median = train_sel.groupby(['matchId','groupId'] ).median()
test_median = test.groupby(['matchId','groupId'] ).median()
train_rank = train_mean.groupby('matchId' ).rank(pct=True)
 test_rank = test_mean.groupby('matchId' ).rank(pct=True)
<merge>
print(train_data[['Embarked', 'Survived']].groupby('Embarked', as_index=False ).mean().sort_values('Survived', ascending=False))
train_new = pd.merge(train_sel,train_mean,suffixes=['',"_mean"],how="left",on=['matchId','groupId'])
 test_new = pd.merge(test,test_mean,suffixes=['',"_mean"],how="left",on=['matchId','groupId'])
 train_new = pd.merge(train_new,train_rank,suffixes=['',"_rank"],how="left",on=['matchId','groupId'])
 test_new = pd.merge(test_new,test_rank,suffixes=['',"_rank"],how="left",on=['matchId','groupId'])
 train_new = pd.merge(train_new,train_median,suffixes=['',"_median"],how="left",on=['matchId','groupId'])
 test_new = pd.merge(test_new,test_median,suffixes=['',"_median"],how="left",on=['matchId','groupId'])
 del train_mean
del test_mean
del train_rank
del test_rank
del train_median
del test_median<feature_engineering>
def embarked_rate(embarked_port):
if embarked_port == 'C': return 2
if embarked_port == 'Q': return 1
if embarked_port == 'S': return 0
for i, row in train_data.iterrows() :
train_data.at[i,'Emb Rate'] = embarked_rate(row["Embarked"])
 for i, row in test_data.iterrows() :
test_data.at[i,'Emb Rate'] = embarked_rate(row["Embarked"] )
selected_columns=[]
for each in train_new.columns:
if "_" in each:
selected_columns.append(each)
 train_selected = train_new[selected_columns]
test_selected = test_new[selected_columns]
train_selected['matchId'] = train['matchId']
test_selected['matchId'] = test['matchId']<drop_column>
sex_mapping = {"male": 0, "female": 1}
train_data['Sex Map'] = train_data['Sex'].map(sex_mapping)
 test_data['Sex Map'] = test_data['Sex'].map(sex_mapping )
cols_toDrop = ['Id_rank','Id_mean','Id_median','vehicleDestroys_mean','vehicleDestroys_median','vehicleDestroys_mean']
train_selected.drop(cols_toDrop,axis=1,inplace=True)
 test_selected.drop(cols_toDrop,axis=1,inplace=True )<prepare_x_and_y>
cols_to_drop = ["SibSp", "Parch", "Name", "Age", "Fare", "Embarked", "Cabin", "Ticket", "Sex", "Titles"]
new_train = train_data.drop(cols_to_drop, axis=1)
 new_test = test_data.drop(cols_to_drop, axis=1)
 y = train_data["Survived"]
features = ["Pclass", "Sex Map", "Family", "Title Map", "Age Group", "Fare Group", "Emb Rate"]
X = pd.get_dummies(new_train[features])
 X_test = pd.get_dummies(new_test[features])
train_selected['winPlacePerc'] = y
matchId = train_selected['matchId'].unique()
matchIdTrain = np.random.choice(matchId, int(0.80*len(matchId)))
 df_train2 = df_train[train_selected['matchId'].isin(matchIdTrain)]
df_test = df_train[~train_selected['matchId'].isin(matchIdTrain)]
y_train = df_train2['winPlacePerc']
X_train = df_train2.drop(columns=['winPlacePerc'])
 y_test = df_test['winPlacePerc']
X_test = df_test.drop(columns=['winPlacePerc'])
<train_model>
model1 = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model1.fit(X, y)
 y1_test = model1.predict(X_test)
 model2 = XGBClassifier(max_depth=3, n_estimators=1000, learning_rate=0.05)
 model2.fit(X, y)
 y2_test = model2.predict(X_test)
 model3 = SVC(random_state=1)
 model3.fit(X,y)
 y3_test = model3.predict(X_test)
 model4 = GradientBoostingClassifier(random_state=42)
 model4.fit(X, y)
 y4_test = model4.predict(X_test )
scaler = MinMaxScaler()
scaler.fit(X_train)
 X_train = scaler.transform(X_train)
 X_test = scaler.transform(X_test)
 model = LinearRegression()
regr = make_pipeline(model)
 model.fit(X_train,y_train)
<predict_on_test>
model1_preds = cross_val_predict(model1, X, y, cv=10)
 model1_acc = accuracy_score(y, model1_preds)
 model2_preds = cross_val_predict(model2, X, y, cv=10)
 model2_acc = accuracy_score(y, model2_preds)
 model3_preds = cross_val_predict(model3, X, y, cv=10)
 model3_acc = accuracy_score(y, model3_preds)
 model4_preds = cross_val_predict(model4, X, y, cv=10)
 model4_acc = accuracy_score(y, model4_preds)
 print("Random Forest Accuracy:", model1_acc)
 print("XGBoost Accuracy:", model2_acc)
 print("SVC Accuracy:", model3_acc)
 print("GB Accuracy:", model4_acc )
<predict_on_test><EOS> 
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': y2_test})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was successfully saved!" )