Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
import numpy as np
import pandas as pd
import seaborn as sns<load_from_csv>
def toTapleList(list1,list2):
return list(itertools.product(list1,list2))
train_raw_data=pd.read_csv('.. /input/titanic/train.csv')
 test_raw_data=pd.read_csv('.. /input/titanic/test.csv' )<prepare_x_and_y>
%%time
train = pd.read_csv('.. /input/train_V2.csv')
 train = reduce_mem_usage(train)
 test = pd.read_csv('.. /input/test_V2.csv')
 test = reduce_mem_usage(test)
 print(train.shape, test.shape )
trainrow=train_raw_data.shape[0]
testrow=test_raw_data.shape[0]
y_train=train_raw_data['Survived'].copy()
train_raw_data=train_raw_data.drop(['Survived'],1 )<concatenate>
null_cnt = train.isnull().sum().sort_values()
print('null count:', null_cnt[null_cnt > 0])
 train.dropna(inplace=True )
combine=pd.concat([train_raw_data,test_raw_data])
 combine.head()<count_missing_values>
all_data = train.append(test, sort=False ).reset_index(drop=True)
 del train, test
gc.collect()
combine.isnull().sum()<count_values>
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 )
combine['Embarked']=combine['Embarked'].fillna(combine['Embarked'].value_counts().index[0] )<count_values>
all_data['_totalDistance'] = all_data['rideDistance'] + all_data['walkDistance'] + all_data['swimDistance']
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']
fillInf(all_data, 0 )
combine['Cabin']=combine['Cabin'].fillna('U')
 combine['Cabin'].value_counts()
combine['Cabin']=combine['Cabin'].astype(str ).str[0]
combine.head()<filter>
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']
combine.loc[combine['Fare'].isnull() ]<feature_engineering>
all_data.drop(['boosts','heals','revives','assists'], axis=1, inplace=True)
 all_data.drop(['headshotKills','roadKills','vehicleDestroys','teamKills'], axis=1, inplace=True)
 all_data.drop(['rideDistance','swimDistance','matchDuration'], axis=1, inplace=True)
 all_data.drop(['rankPoints','killPoints','winPoints'], axis=1, inplace=True )
combine['Fare']=combine['Fare'].fillna(combine.loc[(combine['Pclass']==3)&(combine['Sex']=="male")&(combine['Age']<65)&(combine['Age']>55)].dropna() ['Fare'].mean() )<drop_column>
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)
 print(agg_col)
 sum_col = ['kills','killPlace','damageDealt','walkDistance','_healthItems']
passengerids=test_raw_data['PassengerId']
combine=combine.drop(['PassengerId','Ticket'],1 )<feature_engineering>

 minKills = all_data.sort_values(['matchId','groupId','kills','killPlace'] ).groupby(
 ['matchId','groupId','kills'] ).first().reset_index().copy()
for n in np.arange(5):
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[c].fillna(0, inplace=True)
 match_data = reduce_mem_usage(match_data)
 del minKills, nKills
print(match_data.shape )
combine['familysize']=combine['SibSp']+combine['Parch']+1
combine.head()<feature_engineering>

 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['enemy.' + c] =(all_data['m.sum.' + c] - all_data['sum.' + c])/ all_data['enemy.players']
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)
 print(all_data.shape )
combine['Title'] = combine.Name.str.extract('([A-Za-z]+)\.', expand=False)
 combine.head()<count_values>

 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()
print(all_data.shape )
combine['Title'].value_counts()<drop_column>

 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()
combine=combine.drop(['Name'],1)
 combine.head()<drop_column>
constant_column = [col for col in all_data.columns if all_data[col].nunique() == 1]
print('drop columns:', constant_column)
 all_data.drop(constant_column, axis=1, inplace=True )
combine=combine.drop(['SibSp','Parch'],1)
 combine.head()<categorify>

 mapper = lambda x: 'solo' if('solo' in x)else 'duo' if('duo' in x)or('crash' in x)else 'squad'
all_data['matchType'] = all_data['matchType'].apply(mapper)
 all_data = pd.concat([all_data, pd.get_dummies(all_data['matchType'], prefix='matchType')], axis=1 )
combine['Sex']=combine['Sex'].map({'male':0,'female':1})
 combine.head()<feature_engineering>
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)
 all_data.head()
for i in range(0,2):
for j in range(0,3):
print(i,j+1)
 temp_dataset=combine[(combine['Sex']==i)&(combine['Pclass']==j+1)]['Age'].dropna()
print(temp_dataset)
 combine.loc[(combine.Age.isnull())&(combine.Sex==i)&(combine.Pclass==j+1),'Age']=int(temp_dataset.median() )<count_missing_values>
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()
X_train.drop(['matchId','groupId'], axis=1, inplace=True)
 X_test.drop(['matchId','groupId'], axis=1, inplace=True)
 X_train_cols = X_train.columns
print(X_train.shape, X_test.shape )
combine.isnull().sum()<prepare_output>
def createModel() :
model = Sequential()
model.add(Dense(512, kernel_initializer='he_normal', input_dim=X_train.shape[1], activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(Dense(256, kernel_initializer='he_normal'))
model.add(PReLU(alpha_initializer='zeros', alpha_regularizer=None, alpha_constraint=None, shared_axes=None))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(Dense(128, kernel_initializer='he_normal'))
model.add(PReLU(alpha_initializer='zeros', alpha_regularizer=None, alpha_constraint=None, shared_axes=None))
model.add(BatchNormalization())
 model.add(Dropout(0.1))
model.add(Dense(1, kernel_initializer='normal', activation='sigmoid'))
optimizer = optimizers.Adam(lr=0.005)
 model.compile(optimizer=optimizer, loss='mse', metrics=['mae'])
 return model
combine_checkpoint=combine.copy()
combine.head()<feature_engineering>
np.random.seed(42)
 set_random_seed(1234)
 scaler = preprocessing.StandardScaler().fit(X_train.astype(float))
X_train = scaler.transform(X_train.astype(float))
X_test = scaler.transform(X_test.astype(float))
model = createModel()
history = model.fit(
 X_train, Y_train,
epochs=200,
batch_size=2**15,
validation_split=0.2,
callbacks=[lr_sched, early_stopping],
verbose=2)
 pred = model.predict(X_test ).ravel()
combine['Age_Band']=pd.cut(combine['Age'],5)
 combine['Age_Band'].unique()<feature_engineering>
X_test = pd.read_csv('.. /input/test_V2.csv')
 X_test = X_test.groupby(['matchId','groupId','matchType'] ).first().reset_index()
X_test = X_test[['matchId','groupId','matchType','numGroups','maxPlace','kills','killPlace']]
group = X_test_grp.groupby(['matchId'])
 X_test_grp['winPlacePerc'] = pred
X_test_grp['_rank.winPlacePerc'] = group['winPlacePerc'].rank(method='min')
 X_test = pd.merge(X_test, X_test_grp )
combine.loc[(combine['Age']<=16.136),'Age']=1
combine.loc[(combine['Age']>16.136)&(combine['Age']<=32.102),'Age']=2
combine.loc[(combine['Age']>32.102)&(combine['Age']<=48.068),'Age']=3
combine.loc[(combine['Age']>48.068)&(combine['Age']<=64.034),'Age']=4
combine.loc[(combine['Age']>64.034)&(combine['Age']<=80.) ,'Age']=5
combine['Age'].unique()<drop_column>
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 )
combine=combine.drop(['Age_Band'],1 )<feature_engineering>
print("Checking for anomalies in the winPlacePerc - players with same number of kills should have scores in order of killPlace")
 do_correct = True
iteration_number = 1
while do_correct &(iteration_number <= 1000):
X_test.sort_values(ascending=False, by=["matchId","kills","killPlace","winPlacePerc","groupId"], inplace=True)
 X_test["winPlacePerc_diff"] = X_test["winPlacePerc"].diff()
X_test["kills_diff"] = X_test["kills"].diff()
X_test["prev_matchId"] = X_test["matchId"].shift(1)
 X_test["prev_groupId"] = X_test["groupId"].shift(1)
 X_test["prev_winPlacePerc"] = X_test["winPlacePerc"].shift(1)
 df_sub2 = X_test[(X_test["winPlacePerc_diff"] < 0)
 &(X_test["kills_diff"] == 0)
 &(X_test["matchId"] == X_test["prev_matchId"])]
anomalies_count = len(df_sub2)
 print("Iteration " + str(iteration_number)+ " Anomalies count: " + str(anomalies_count))
changed_groups = list()
if anomalies_count > 0:
print()
print("Looking for pairs to change...")
 df_sub2["new_winPlacePerc"] = df_sub2["winPlacePerc"]
df_sub3 = pd.DataFrame()
for i in tqdm(range(1, min(15001, max(anomalies_count, 2))),
desc="Identifying unique groups", mininterval=10):
row = df_sub2.iloc[i - 1]
id_prev = str(row["prev_matchId"])+ "!" + str(row["prev_groupId"])
 id_cur = str(row["matchId"])+ "!" + str(row["groupId"])
 if(not id_prev in changed_groups)&(not id_cur in changed_groups):
changed_groups.append(id_prev)
 changed_groups.append(id_cur)
 df_sub3 = df_sub3.append({"matchId": row["matchId"], "groupId": row["prev_groupId"],
"new_winPlacePerc": row["winPlacePerc"]},
sort=False, ignore_index=True)
 df_sub3 = df_sub3.append({"matchId": row["matchId"], "groupId": row["groupId"],
"new_winPlacePerc": row["prev_winPlacePerc"]},
sort=False, ignore_index=True)
 df_sub3.drop_duplicates(inplace=True)
 X_test = X_test.merge(df_sub3, on=["matchId", "groupId"], how="left")
 notna = X_test["new_winPlacePerc"].notna()
X_test.loc[notna, "winPlacePerc"] = X_test.loc[notna]["new_winPlacePerc"]
X_test.drop(labels="new_winPlacePerc", axis=1, inplace=True)
 del df_sub2
del df_sub3
df_sub2 = None
df_sub3 = None
gc.collect()
else:
do_correct = False
iteration_number = iteration_number + 1
if do_correct:
print("Limit of iterations reached...")
 print("Finished correcting winPlacePerc" )
combine['Fare_Band']=pd.cut(combine['Fare'],3)
 combine['Fare_Band'].unique()<feature_engineering>
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
X_test['winPlacePerc'].describe()
<count_values><EOS> 
test = pd.read_csv('.. /input/test_V2.csv')
 submission = pd.merge(test, X_test[['matchId','groupId','winPlacePerc']])
 submission = submission[['Id','winPlacePerc']]
submission.to_csv("submission.csv", index=False )