Kaggle competition: Titanic - Machine Learning from Disaster
predict_groupwise = model.predict(test_groupwise )<load_from_csv>
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()
submission = pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/sample_submission_V2.csv')
 submission = submission.astype({'winPlacePerc': 'float64'} )<groupby>
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()
for test_matchId, test_indices in test.groups.items() :
loc = test_groupwise.index.get_loc(test_matchId)
 groups = [index[1] for index in test_groupwise.index.values[loc]]
t_order = np.array(groups)[predict_groupwise[loc].argsort() ].tolist()
p_orders = [kill[1]['groupId'].values[kill[1]['killPlace'].values.argsort() [::-1]].tolist() for kill in test.get_group(test_matchId ).groupby('kills')]
order = rearrange(t_order, p_orders)
 maxPlace = test.get_group(test_matchId ).iloc[0]['maxPlace']
numGroups = test.get_group(test_matchId ).iloc[0]['numGroups']
numVirtual = maxPlace - numGroups
for i in range(numVirtual):
order.insert(int(round(numGroups /(numVirtual + 1)*(i + 1)+ i)) , 'v')
 for i in range(len(test_indices)) :
submission.at[test_indices[i], 'winPlacePerc'] = order.index(test.get_group(test_matchId ).iloc[i]['groupId'])/ float(maxPlace - 1 )<save_to_csv>
train_data.groupby('Sex' ).Survived.mean()
submission.to_csv('submission.csv', index=False )<save_to_csv>
train_data.groupby('Pclass' ).Survived.mean()
submission.to_csv('submission.csv', index=False )<set_options>
train_data['Age'] = train_data[['Age','Pclass']].apply(cal_age,axis=1 )
pd.options.display.float_format = '{:,.3f}'.format<load_from_csv>
train = pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/train_V2.csv')
 train =reduce_mem_usage(train)
 test = pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/test_V2.csv')
 test = reduce_mem_usage(test)
 print(train.shape,test.shape )<filter>
train_data.isnull()
train[train['winPlacePerc'].isnull() ]<drop_column>
train_data.isnull().sum()
train.drop(2744604,inplace=True )<feature_engineering>
train_data.drop(["Name","Cabin"], axis=1, inplace=True )
mapper = lambda x: 'solo' if('solo'in x)else 'duo' if('duo' in x)or('crash'in x)else 'squad'
train['matchType'] = train['matchType'].apply(mapper)
 match_type_counts_2=train.groupby('matchId')['matchType'].first().value_counts().sort_values(ascending=False )<concatenate>
train_data['Embarked'] = train_data['Embarked'].fillna(value=train_data['Embarked'].mode() [0] )
all_data = train.append(test, sort=False ).reset_index(drop=True)
 del train, test
gc.collect()<feature_engineering>
train_data.isnull().sum()
match = all_data.groupby('matchId')
 all_data['killsPerc'] = match['kills'].rank(pct=True ).values
all_data['killPlacePerc'] = match['killPlace'].rank(pct=True ).values
all_data['walkDistancePerc'] = match['walkDistance'].rank(pct=True ).values
all_data['walkPerc_killsPerc'] = all_data['walkDistancePerc'] / all_data['killsPerc']
all_data['_totalDistance'] = all_data['rideDistance'] + all_data['walkDistance'] + all_data['swimDistance']<data_type_conversions>
train_data['Sex'] = train_data['Sex'].replace(['male', 'female'],[1,0])
 train_data.rename(columns = {'Sex' : 'gender'}, inplace = True )
def fillInf(df, val):
numcols = df.select_dtypes(include='number' ).columns
cols = numcols[numcols != 'winPlacePerc']
df[df == np.Inf] = np.NaN
df[df == np.NINF] = np.NaN
for c in cols: df[c].fillna(val, inplace=True )<feature_engineering>
train_data['Embarked'] = train_data['Embarked'].replace(['S','C','Q'],[0,1,2])
 train_data.rename(columns = {'Embarked' : 'port'}, inplace = True )
all_data['_healthItems'] = all_data['heals'] + all_data['boosts']
all_data['_headshotKillRate'] = all_data['headshotKills'] / all_data['kills']
all_data['_killPlaceOverMaxPlace'] = all_data['killPlace'] / all_data['maxPlace']
all_data['_killsOverWalkDistance'] = all_data['kills'] / all_data['walkDistance']<drop_column>
train_data.rename(columns = {'Pclass' : 'passenger_cls'} )
all_data.drop(['boosts','heals','killStreaks','DBNOs'], axis=1, inplace=True)
 all_data.drop(['headshotKills','roadKills','vehicleDestroys'], axis=1, inplace=True)
 all_data.drop(['rideDistance','swimDistance','matchDuration'], axis=1, inplace=True)
 all_data.drop(['rankPoints','killPoints','winPoints'], axis=1, inplace=True )<groupby>
train_data['family_members'] = train_data['SibSp'] + train_data['Parch']
train_data.drop(['SibSp', 'Parch'], axis = 1, inplace=True )
match = all_data.groupby(['matchId'])
 group = all_data.groupby(['matchId','groupId','matchType'])
 agg_col = list(all_data.columns)
 exclude_agg_col = ['Id','matchId','groupId','matchType','maxPlace','numGroups','winPlacePerc']
for c in exclude_agg_col:
agg_col.remove(c)
 sum_col = ['kills','killPlace','damageDealt','walkDistance','_healthItems']<merge>
train_data.loc[ train_data['Age'] <= 21, 'Age'] = 0
train_data.loc[(train_data['Age'] > 21)&(train_data['Age'] <= 34), 'Age'] = 1
train_data.loc[(train_data['Age'] > 34)&(train_data['Age'] <= 54), 'Age'] = 2
train_data.loc[(train_data['Age'] > 60)&(train_data['Age'] <= 75), 'Age'] = 3
train_data.loc[ train_data['Age'] > 75, 'Age'] ;
minKills = all_data.sort_values(['matchId','groupId','kills','killPlace'] ).groupby(
 ['matchId','groupId','kills'] ).first().reset_index().copy()
for n in np.arange(4):
c = 'kills_' + str(n)+ '_Place'
nKills =(minKills['kills'] == n)
 minKills.loc[nKills, c] = minKills[nKills].groupby(['matchId'])['killPlace'].rank().values
match_data = pd.merge(match_data, minKills[nKills][['matchId','groupId',c]], how='left')
 match_data = reduce_mem_usage(match_data)
 del minKills, nKills<merge>
train_data.loc[ train_data['Fare'] <= 7.5, 'Fare'] = 0
train_data.loc[(train_data['Fare'] > 15)&(train_data['Fare'] <= 21.5), 'Fare'] = 1
train_data.loc[(train_data['Fare'] > 21.5)&(train_data['Age'] <= 29), 'Fare'] = 2
train_data.loc[(train_data['Fare'] > 29)&(train_data['Fare'] <= 36.5), 'Fare'] = 3
train_data.loc[ train_data['Fare'] > 36.5, 'Fare'] ;
all_data = pd.merge(all_data, match_data)
 del match_data
gc.collect()
all_data['enemy.players'] = all_data['m.players'] - all_data['players']
for c in sum_col:
all_data['p.max_msum.' + c] = all_data['max.' + c] / all_data['m.sum.' + c]
all_data['p.max_mmax.' + c] = all_data['max.' + c] / all_data['m.max.' + c]
all_data.drop(['m.sum.' + c, 'm.max.' + c], axis=1, inplace=True)
 fillInf(all_data, 0 )<merge>
train_data.drop(['Ticket'], axis = 1, inplace=True )
match = all_data.groupby('matchId')
 matchRank = match[numcols].rank(pct=True ).rename(columns=lambda s: 'rank.' + s)
 all_data = reduce_mem_usage(pd.concat([all_data, matchRank], axis=1))
rank_col = matchRank.columns
del matchRank
gc.collect()
match = all_data.groupby('matchId')
 matchRank = match[rank_col].max().rename(columns=lambda s: 'max.' + s ).reset_index()
all_data = pd.merge(all_data, matchRank)
 for c in numcols:
all_data['rank.' + c] = all_data['rank.' + c] / all_data['max.rank.' + c]
all_data.drop(['max.rank.' + c], axis=1, inplace=True)
 del matchRank
gc.collect()<merge>
test_data.isnull().sum()
killMinorRank = all_data[['matchId','min.kills','max.killPlace']].copy()
group = killMinorRank.groupby(['matchId','min.kills'])
 killMinorRank['rank.minor.maxKillPlace'] = group.rank(pct=True ).values
all_data = pd.merge(all_data, killMinorRank)
 killMinorRank = all_data[['matchId','max.kills','min.killPlace']].copy()
group = killMinorRank.groupby(['matchId','max.kills'])
 killMinorRank['rank.minor.minKillPlace'] = group.rank(pct=True ).values
all_data = pd.merge(all_data, killMinorRank)
 del killMinorRank
gc.collect()<count_unique_values>
test_data.drop(['Name', 'Ticket','Cabin'], axis = 1, inplace = True )
constant_column = [col for col in all_data.columns if all_data[col].nunique() == 1]
all_data.drop(constant_column, axis=1, inplace=True )<feature_engineering>
test_data['Age'] = test_data[['Age','Pclass']].apply(cal_age,axis=1)
all_data['matchType'] = all_data['matchType'].apply(mapper)
 all_data = pd.concat([all_data, pd.get_dummies(all_data['matchType'])], axis=1)
 all_data.drop(['matchType'], axis=1, inplace=True)
 all_data['matchId'] = all_data['matchId'].apply(lambda x: int(x,16))
all_data['groupId'] = all_data['groupId'].apply(lambda x: int(x,16))<count_missing_values>
null_cnt = all_data.isnull().sum().sort_values()<categorify>
test_data['Fare'].fillna(value=test_data['Fare'].mean() , inplace=True )
cols = [col for col in all_data.columns if col not in ['Id','matchId','groupId']]
for i, t in all_data.loc[:, cols].dtypes.iteritems() :
if t == object:
all_data[i] = pd.factorize(all_data[i])[0]
all_data = reduce_mem_usage(all_data )<prepare_x_and_y>
test_data.isnull().sum()
X_train = all_data[all_data['winPlacePerc'].notnull() ].reset_index(drop=True)
 X_test = all_data[all_data['winPlacePerc'].isnull() ].drop(['winPlacePerc'], axis=1 ).reset_index(drop=True)
 del all_data
gc.collect()
Y_train = X_train.pop('winPlacePerc')
 X_test_grp = X_test[['matchId','groupId']].copy()
train_matchId = X_train['matchId']
X_train.drop(['matchId','groupId'], axis=1, inplace=True)
 X_test.drop(['matchId','groupId'], axis=1, inplace=True )<train_model>
test_data['Sex'] = test_data['Sex'].replace(['male', 'female'],[1,0])
 test_data.rename(columns = {'Sex' : 'gender'}, inplace = True )
params={'learning_rate': 0.05,
'objective':'mae',
'metric':'mae',
'num_leaves': 128,
'verbose': 1,
'random_state':42,
'bagging_fraction': 0.7,
'feature_fraction': 0.7
}
reg = lgb.LGBMRegressor(**params, n_estimators=10000)
 reg.fit(X_train, Y_train)
 pred = reg.predict(X_test, num_iteration=reg.best_iteration_ )<concatenate>
test_data['Embarked'] = test_data['Embarked'].replace(['S','C','Q'],[0,1,2])
 test_data.rename(columns = {'Embarked' : 'port'}, inplace = True )
X_test_grp['_nofit.winPlacePerc'] = pred
group = X_test_grp.groupby(['matchId'])
 X_test_grp['winPlacePerc'] = pred
X_test_grp['_rank.winPlacePerc'] = group['winPlacePerc'].rank(method='min')
 X_test = pd.concat([X_test, X_test_grp], axis=1 )<feature_engineering>
test_data.rename(columns = {'Pclass' : 'passenger cls'} )
fullgroup =(X_test['numGroups'] == X_test['maxPlace'])
 subset = X_test.loc[fullgroup]
X_test.loc[fullgroup, 'winPlacePerc'] =(subset['_rank.winPlacePerc'].values - 1)/(subset['maxPlace'].values - 1)
 subset = X_test.loc[~fullgroup]
gap = 1.0 /(subset['maxPlace'].values - 1)
 new_perc = np.around(subset['winPlacePerc'].values / gap)* gap
X_test.loc[~fullgroup, 'winPlacePerc'] = new_perc
X_test['winPlacePerc'] = X_test['winPlacePerc'].clip(lower=0,upper=1 )<feature_engineering>
test_data['family_members'] = test_data['SibSp'] + test_data['Parch']
test_data.drop(['SibSp', 'Parch'], axis = 1, inplace=True )
X_test.loc[X_test['maxPlace'] == 0, 'winPlacePerc'] = 0
X_test.loc[X_test['maxPlace'] == 1, 'winPlacePerc'] = 1
X_test.loc[(X_test['maxPlace'] > 1)&(X_test['numGroups'] == 1), 'winPlacePerc'] = 0<save_to_csv>
test_data.loc[ test_data['Age'] <= 21, 'Age'] = 0
test_data.loc[(test_data['Age'] > 21)&(test_data['Age'] <= 34), 'Age'] = 1
test_data.loc[(test_data['Age'] > 34)&(test_data['Age'] <= 54), 'Age'] = 2
test_data.loc[(test_data['Age'] > 60)&(test_data['Age'] <= 75), 'Age'] = 3
test_data.loc[ test_data['Age'] > 75, 'Age'] ;
test = pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/test_V2.csv')
 test['matchId'] = test['matchId'].apply(lambda x: int(x,16))
test['groupId'] = test['groupId'].apply(lambda x: int(x,16))
submission = pd.merge(test, X_test[['matchId','groupId','winPlacePerc']])
 submission = submission[['Id','winPlacePerc']]
submission.to_csv("submission.csv", index=False )<set_options>
test_data.loc[ test_data['Fare'] <= 7.5, 'Fare'] = 0
test_data.loc[(test_data['Fare'] > 15)&(test_data['Fare'] <= 21.5), 'Fare'] = 1
test_data.loc[(test_data['Fare'] > 21.5)&(test_data['Age'] <= 29), 'Fare'] = 2
test_data.loc[(test_data['Fare'] > 29)&(test_data['Fare'] <= 36.5), 'Fare'] = 3
test_data.loc[ test_data['Fare'] > 36.5, 'Fare'] ;
gc.enable()<load_from_csv>
x = train_data.drop("Survived", axis = 1 )
def feature_engineering(is_train=True):
if is_train:
print("processing train.csv")
 df = pd.read_csv(".. /input/pubg-finish-placement-prediction/train_V2.csv")
 df = df[df['maxPlace'] > 1]
else:
print("processing test.csv")
 df = pd.read_csv(".. /input/pubg-finish-placement-prediction/test_V2.csv")
 df['totalDistance'] = df['rideDistance'] + df["walkDistance"] + df["swimDistance"]
print("remove some columns")
 target = 'winPlacePerc'
features = list(df.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchType")
 y = None
print("get target")
 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 sum feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('sum')
 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=["_sum", "_sum_rank"], how='left', on=['matchId', 'groupId'])
 df_out=reduce_mem_usage(df_out)
 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'])
 df_out=reduce_mem_usage(df_out)
 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=reduce_mem_usage(df_out)
 df_out.drop(["matchId", "groupId"], axis=1, inplace=True)
 X = np.array(df_out)
 feature_names = list(df_out.columns)
 del df, df_out, agg, agg_rank
gc.collect()
return X, y, feature_names<prepare_x_and_y>
y = train_data["Survived"]
x_train, y, feature_names = feature_engineering(True )<split>
from sklearn.model_selection import train_test_split
X_train, X_val, y_train, y_val = train_test_split(x_train, y, test_size=0.33, random_state=42 )<set_options>
from sklearn.model_selection import train_test_split
warnings.filterwarnings("ignore")
<train_model>
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.4, random_state = 12 )
train_data=lgb.Dataset(X_train, label=y_train)
 val_data= lgb.Dataset(X_val, label=y_val)
 params = {
'num_leaves': 144,
'learning_rate': 0.1,
'n_estimators': 1500,
'max_depth':12,
'max_bin':55,
'bagging_fraction':0.8,
'bagging_freq':5,
'feature_fraction':0.9,
'verbose':50,
'early_stopping_rounds':100
}
params['metric'] = 'auc'
lgb_model= lgb.train(params, train_data, valid_sets=val_data, num_boost_round=3000, early_stopping_rounds=100)
 y_pred=lgb_model.predict(X_val)
 print(mean_absolute_error(y_val,y_pred))<predict_on_test>
dtree = DecisionTreeClassifier()
dtree.fit(x_train, y_train)
 y_pred = dtree.predict(x_test)
 dtree_accuracy = round(accuracy_score(y_pred, y_test)* 100, 2)
 print(dtree_accuracy )
y_pred=lgb_model.predict(X_val)
 print(mean_absolute_error(y_val,y_pred))
del X_val
del y_val<prepare_x_and_y>
rf = RandomForestClassifier(n_estimators=50, max_depth = 8)
 rf.fit(x_train, y_train)
 y_pred = rf.predict(x_test)
 acc_rf = round(accuracy_score(y_pred, y_test)* 100, 2)
 print(acc_rf )
x_test, y_test, feature_names = feature_engineering(False )<predict_on_test>
lr = LogisticRegression(solver='liblinear', dual = False)
 lr.fit(x_train, y_train)
 y_pred = lr.predict(x_test)
 acc_log = round(lr.score(x_train, y_train)* 100, 2)
 acc_log
accuracy_score(y_test, y_pred )
y_test_pred=lgb_model.predict(x_test )<save_to_csv>
rf = RandomForestClassifier(n_estimators=50, max_depth = 8 )
df=pd.read_csv(".. /input/pubg-finish-placement-prediction/test_V2.csv")
 var=pd.DataFrame(columns=['Id','winPlacePerc'])
 var['Id']= df['Id']
var['winPlacePerc'] = y_test_pred
submission = var[['Id', 'winPlacePerc']]
submission.to_csv('submission.csv', index=False )<load_from_csv>
rf.fit(x_train, y_train )
train=pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/train_V2.csv')
 test=pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/test_V2.csv')
 sample_submission=pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/sample_submission_V2.csv')
<count_missing_values>
x_test = test_data
train.isnull().sum()<count_missing_values>
y_test_predict = rf.predict(x_test )
train.isnull().sum()<correct_missing_values>
output_data = pd.DataFrame({'PassengerId' : test_data.PassengerId, 'Survived' : y_test_predict})
 output_data.to_csv('Titanic_Survival.csv', index = False)
 print("Submission is successfully" )