Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<categorify>
train=pd.read_csv('.. /input/train_V2.csv',sep=',',header=0)
 train=train.dropna()
train['num']=train.groupby('matchId')['Id'].transform('count')
 train['max']=train.groupby('matchId')['kills'].transform('max')
 train=train[train['num']>train['max']]
train=train.drop(['num','max'],axis=1 )
combine=pd.get_dummies(columns=['Pclass','Sex','Cabin','Embarked','Title','Age','Fare'],data=combine)
 combine.head()<prepare_x_and_y>
group_rank = train[['matchId','groupId','winPlacePerc','maxPlace','numGroups']].drop_duplicates()
group_rank['rank'] = group_rank.groupby('matchId')['winPlacePerc'].rank(method='min',ascending=False)
 group_rank['gap'] = 1 /(group_rank['numGroups'] - 1)
 group_rank.loc[group_rank['maxPlace']==1,'re_rank'] = 0
group_rank.loc[group_rank['maxPlace']!=1,'re_rank'
] = group_rank['gap'] *(group_rank['numGroups'] - group_rank['rank'])
 train = pd.merge(train, group_rank[['matchId','groupId','re_rank']], how='left', on=['matchId','groupId'] )
x_train=combine.iloc[:trainrow]
x_test=combine.iloc[trainrow:]<import_modules>
train.loc[train.matchType.str.contains('fpp'),'fpp'] = 1
train.loc[~train.matchType.str.contains('fpp'),'fpp'] = 0
train.loc[train.matchType.str.contains('normal'),'normal'] = 1
train.loc[~train.matchType.str.contains('normal'),'normal'] = 0
train.loc[train.matchType.str.contains('squad'),'squad'] = 1
train.loc[~train.matchType.str.contains('squad'),'squad'] = 0
train.loc[train.matchType.str.contains('duo'),'duo'] = 1
train.loc[~train.matchType.str.contains('duo'),'duo'] = 0
train.loc[train.matchType.str.contains('solo'),'solo'] = 1
train.loc[~train.matchType.str.contains('solo'),'solo'] = 0
from sklearn.preprocessing import StandardScaler<normalization>
train['maxGrp_killPoints'] = train.groupby('groupId')['killPoints'].transform('max')
 train['Perc_kills'] = train['kills']/train.groupby('matchId')['kills'].transform('sum')
 train['maxPerc_kills'] = train.groupby('matchId')['Perc_kills'].transform('max')
 train['Rank_kills'] = train.groupby('matchId')['kills'].rank(method='min',ascending=False,pct=False)
 train['maxRank_kills'] = train.groupby('matchId')['Rank_kills'].transform('max')
 train['max_kills'] = train.groupby('matchId')['kills'].transform('max')
 train['kill_Death'] = train['max_kills']/train.groupby('matchId')['Id'].transform('count')
 train['RankPerc_kills'] = train.groupby('matchId')['kills'].rank(method='max',ascending=True,pct=True)
 train['tkills']=train.groupby('groupId')['kills'].transform('sum' )
scaler=StandardScaler()
scaler.fit(x_train)
 x_scaled_train=scaler.transform(x_train)
 x_scaled_train<normalization>
train['Perc_damageDt'] = train['damageDealt']/train.groupby('matchId')['damageDealt'].transform('sum')
 train['maxPerc_damageDt'] = train.groupby('matchId')['Perc_damageDt'].transform('max')
 train['Rank_damageDt'] = train.groupby('matchId')['damageDealt'].rank(method='min',ascending=False,pct=False)
 train['maxRank_damageDt'] = train.groupby('matchId')['Rank_damageDt'].transform('max')
 train['max_damageDt'] = train.groupby('matchId')['damageDealt'].transform('max' )
x_scaled_test=scaler.transform(x_test)
 x_scaled_test<train_model>
train['walkDistance']=np.log(train['walkDistance']+1)
 train['Perc_wkdis'] = train['walkDistance']/train.groupby('matchId')['walkDistance'].transform('sum')
 train['maxPerc_wkdis'] = train.groupby('matchId')['Perc_wkdis'].transform('max')
 train['Rank_wkdis'] = train.groupby('matchId')['walkDistance'].rank(method='min',ascending=False,pct=False)
 train['maxRank_wkdis'] = train.groupby('matchId')['Rank_wkdis'].transform('max')
 train['max_wkdis'] = train.groupby('matchId')['walkDistance'].transform('max')
 train['maxwkdis']=train.groupby('groupId')['walkDistance'].transform('max')
reg=LogisticRegression()
reg.fit(x_scaled_train,y_train)
 print(reg.score(x_scaled_train,y_train))
y_pred=reg.predict(x_scaled_test)
 y_pred<train_model>
train['Perc_ridedis'] = train['rideDistance']/train.groupby('matchId')['rideDistance'].transform('sum')
 train['maxPerc_ridedis'] = train.groupby('matchId')['Perc_ridedis'].transform('max')
 train['Rank_ridedis'] = train.groupby('matchId')['rideDistance'].rank(method='min',ascending=False,pct=False)
 train['maxRank_ridedis'] = train.groupby('matchId')['Rank_ridedis'].transform('max')
 train['max_ridedis'] = train.groupby('matchId')['rideDistance'].transform('max')
 train['maxGrp_ridedis']=train.groupby('groupId')['rideDistance'].transform('max' )
xgb=XGBClassifier()
xgb.fit(x_scaled_train,y_train,early_stopping_rounds=5,
eval_set=[(x_scaled_train, y_train)],
verbose=False)
 print(xgb.score(x_scaled_train,y_train))
y_pred=xgb.predict(x_scaled_test )<train_model>
train['Perc_boo'] = train['boosts']/train.groupby('matchId')['boosts'].transform('sum')
 train['max_boo'] = train.groupby('matchId')['boosts'].transform('max')
 train['Perc_heals'] = train['heals']/train.groupby('matchId')['heals'].transform('sum')
 train['max_heals'] = train.groupby('matchId')['heals'].transform('max')
 train['assReviv'] = train['assists']+train['revives']
train = train.drop(columns=['assists','revives'],axis=1)
 train['Perc_assReviv'] = train['assReviv']/train.groupby('matchId')['assReviv'].transform('sum')
 train['max_assReviv'] = train.groupby('matchId')['assReviv'].transform('max' )
rfc=RandomForestClassifier(random_state=4,n_estimators=500,warm_start=True,max_depth=6,min_samples_leaf=2,max_features='sqrt')
 rfc.fit(x_scaled_train,y_train)
 print(rfc.score(x_scaled_train,y_train))
y_pred=rfc.predict(x_scaled_test )<create_dataframe>
train['teamSize'] = train.groupby('groupId')['Id'].transform('count')
 train['numEnemies'] = train.groupby('matchId')['Id'].transform('count')-train['teamSize']
train['pace'] = train.groupby('matchId')['kills'].transform('sum')/train['matchDuration']
submission = pd.DataFrame({
"PassengerId": passengerids,
"Survived": y_pred
})
 submission<save_to_csv>
train = train.drop('matchType',axis = 1)
 train = train.drop(columns=['vehicleDestroys','roadKills'],axis=1 )
submission.to_csv('submission1.csv', index=False )<set_options>
match_ids = train['matchId'].drop_duplicates()
msk=np.random.rand(len(match_ids)) <0.8
valid = train[train['matchId'].isin(match_ids[~msk])]
train = train[train['matchId'].isin(match_ids[msk])]
train = train.sort_values(by=['matchId','re_rank'])
 y_train = train['re_rank'].values
group_train = train.groupby('matchId',as_index=False)['Id'].agg('count')['Id'].tolist()
x_train = scipy.sparse.csr_matrix(train.drop(columns=['matchId','groupId','Id','re_rank','winPlacePerc'],axis=1))
print(os.listdir(".. /input"))
warnings.filterwarnings('ignore')
 plt.rcParams['figure.figsize'] =(16,9)
 sns.set_palette('gist_earth' )<load_from_csv>
valid = valid.sort_values(by=['matchId','re_rank'])
 y_valid = valid['re_rank'].values
group_valid = valid.groupby('matchId',as_index=False)['Id'].agg('count')['Id'].tolist()
x_valid = scipy.sparse.csr_matrix(valid.drop(columns=['matchId','groupId','Id','re_rank','winPlacePerc'],axis=1))
df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv')
 full = pd.concat([df_train, df_test], axis = 0, sort=True)
 full.set_index('PassengerId', drop = False, inplace=True)
 train = full[:891]
display(full.head(3))
print(f"Dataset contains {full.shape[0]} records, with {full.shape[1]} variables." )<feature_engineering>
test=pd.read_csv('.. /input/test_V2.csv',sep=',',header=0)
 test.loc[test.matchType.str.contains('fpp'),'fpp'] = 1
test.loc[~test.matchType.str.contains('fpp'),'fpp'] = 0
test.loc[test.matchType.str.contains('normal'),'normal'] = 1
test.loc[~test.matchType.str.contains('normal'),'normal'] = 0
test.loc[test.matchType.str.contains('squad'),'squad'] = 1
test.loc[~test.matchType.str.contains('squad'),'squad'] = 0
test.loc[test.matchType.str.contains('duo'),'duo'] = 1
test.loc[~test.matchType.str.contains('duo'),'duo'] = 0
test.loc[test.matchType.str.contains('solo'),'solo'] = 1
test.loc[~test.matchType.str.contains('solo'),'solo'] = 0
def parse_Cabin(cabin):
if type(cabin)== str:
m = re.search(r'([A-Z])+', cabin)
 return m.group(1)
 else:
return 'X'
full['Cabin_short'] = full['Cabin'].map(parse_Cabin )<categorify>
test['maxGrp_killPoints'] = test.groupby('groupId')['killPoints'].transform('max')
 test['Perc_kills'] = test['kills']/test.groupby('matchId')['kills'].transform('sum')
 test['maxPerc_kills'] = test.groupby('matchId')['Perc_kills'].transform('max')
 test['Rank_kills'] = test.groupby('matchId')['kills'].rank(method='min',ascending=False,pct=False)
 test['maxRank_kills'] = test.groupby('matchId')['Rank_kills'].transform('max')
 test['max_kills'] = test.groupby('matchId')['kills'].transform('max')
 test['kill_Death'] = test['max_kills']/test.groupby('matchId')['Id'].transform('count')
 test['RankPerc_kills'] = test.groupby('matchId')['kills'].rank(method='max',ascending=True,pct=True)
 test['tkills']=test.groupby('groupId')['kills'].transform('sum')
 test['Perc_damageDt'] = test['damageDealt']/test.groupby('matchId')['damageDealt'].transform('sum')
 test['maxPerc_damageDt'] = test.groupby('matchId')['Perc_damageDt'].transform('max')
 test['Rank_damageDt'] = test.groupby('matchId')['damageDealt'].rank(method='min',ascending=False,pct=False)
 test['maxRank_damageDt'] = test.groupby('matchId')['Rank_damageDt'].transform('max')
 test['max_damageDt'] = test.groupby('matchId')['damageDealt'].transform('max')
 test['walkDistance']=np.log(test['walkDistance']+1)
 test['Perc_wkdis'] = test['walkDistance']/test.groupby('matchId')['walkDistance'].transform('sum')
 test['maxPerc_wkdis'] = test.groupby('matchId')['Perc_wkdis'].transform('max')
 test['Rank_wkdis'] = test.groupby('matchId')['walkDistance'].rank(method='min',ascending=False,pct=False)
 test['maxRank_wkdis'] = test.groupby('matchId')['Rank_wkdis'].transform('max')
 test['max_wkdis'] = test.groupby('matchId')['walkDistance'].transform('max')
 test['maxwkdis']=test.groupby('groupId')['walkDistance'].transform('max')
 test['Perc_ridedis'] = test['rideDistance']/test.groupby('matchId')['rideDistance'].transform('sum')
 test['maxPerc_ridedis'] = test.groupby('matchId')['Perc_ridedis'].transform('max')
 test['Rank_ridedis'] = test.groupby('matchId')['rideDistance'].rank(method='min',ascending=False,pct=False)
 test['maxRank_ridedis'] = test.groupby('matchId')['Rank_ridedis'].transform('max')
 test['max_ridedis'] = test.groupby('matchId')['rideDistance'].transform('max')
 test['maxGrp_ridedis']=test.groupby('groupId')['rideDistance'].transform('max')
 test['Perc_boo'] = test['boosts']/test.groupby('matchId')['boosts'].transform('sum')
 test['max_boo'] = test.groupby('matchId')['boosts'].transform('max')
 test['Perc_heals'] = test['heals']/test.groupby('matchId')['heals'].transform('sum')
 test['max_heals'] = test.groupby('matchId')['heals'].transform('max')
 test['assReviv'] = test['assists']+test['revives']
test = test.drop(columns=['assists','revives'],axis=1)
 test['Perc_assReviv'] = test['assReviv']/test.groupby('matchId')['assReviv'].transform('sum')
 test['max_assReviv'] = test.groupby('matchId')['assReviv'].transform('max')
 test['teamSize'] = test.groupby('groupId')['Id'].transform('count')
 test['numEnemies'] = test.groupby('matchId')['Id'].transform('count')-test['teamSize']
test['pace'] = test.groupby('matchId')['kills'].transform('sum')/test['matchDuration']
test = test.drop('matchType',axis = 1)
 test = test.drop(columns=['vehicleDestroys','roadKills'],axis=1)
 test = test.sort_values(by=['matchId'])
 group_test = test.groupby('matchId',as_index=False)['Id'].agg('count')['Id'].tolist()
x_test = scipy.sparse.csr_matrix(test.drop(axis=1,columns=['Id','groupId','matchId']))
dict_fare_by_Pclass = dict(full.groupby('Pclass' ).Fare.mean())
 missing_fare = full.loc[full.Fare.isnull() ,'Pclass'].map(dict_fare_by_Pclass)
 full.loc[full.Fare.isnull() ,'Fare'] = missing_fare<create_dataframe>
params = {'objective': 'rank:pairwise', 'learning_rate': 0.1,
'gamma': 0, 'min_child_weight': 60,
'max_depth': 14, 'n_estimators': 6,
'colsample_bytree': 1,'subsample': 1,
'eval_metric': 'mae'}
model = xgb.sklearn.XGBRanker(**params)
 model.fit(x_train, y_train, group_train,
eval_set=[(x_valid, y_valid)], eval_group=[group_valid])
 pred = model.predict(x_test )
features = pd.DataFrame()
features['Pclass'] = full['Pclass']
features['Fare'] = full['Fare']
features['Sex'] = full['Sex'] 
<data_type_conversions>
test['pred'] = pred
test['mean_pred'] = test.groupby('groupId')['pred'].transform('mean')
 group_rank = test[['matchId','groupId','mean_pred','maxPlace','numGroups']].drop_duplicates()
group_rank['prank'] = group_rank.groupby('matchId')['mean_pred'].rank(method='min',ascending=False )
features['A5'] =(full['Ticket_short'] == 'A5' ).astype(int)
 features['PC'] =(full['Ticket_short'] == 'PC' ).astype(int )<define_variables>
group_rank['emp_ranks'] = group_rank['maxPlace'] - group_rank['numGroups']
group_rank['gap'] = 1 /(group_rank['maxPlace'] - 1)
 group_rank.loc[group_rank['maxPlace']==1,'winPlacePerc'] = 0
group_rank.loc[(group_rank['maxPlace']!=1)&(group_rank['prank']<(group_rank['numGroups']/2 ).astype(int)) ,'winPlacePerc'
] = group_rank['gap'] *(group_rank['maxPlace'] - group_rank['prank'])
 group_rank.loc[(group_rank['maxPlace']!=1)&(group_rank['prank']>(group_rank['numGroups']/2 ).astype(int)) ,'winPlacePerc'
] = group_rank['gap'] *(group_rank['maxPlace'] - group_rank['prank'] - group_rank['emp_ranks'])
 group_rank.loc[(group_rank['maxPlace']!=1)&(group_rank['prank']==(group_rank['numGroups']/2 ).astype(int)) ,'winPlacePerc'
] = group_rank['gap'] *(group_rank['maxPlace'] - group_rank['prank'] -(group_rank['emp_ranks']/2 ).astype(int))
<categorify><EOS> 
test = pd.merge(test, group_rank[['matchId','groupId','winPlacePerc']], on=['matchId','groupId'], how='left')
 test[['Id','winPlacePerc']].to_csv('submission2_0120.csv',index=False, header=True )