Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<choose_model_class>
%load_ext autoreload
%autoreload 2
%matplotlib inline
KAGGLE_DIR = '.. /input/'
train = pd.read_csv(KAGGLE_DIR + 'train_V2.csv')
 test = pd.read_csv(KAGGLE_DIR + 'test_V2.csv' )
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'] )<train_model>
train.drop(2744604, inplace=True)
 train['playersJoined'] = train.groupby('matchId')['matchId'].transform('count')

train['killsNorm'] = train['kills']*(( 100-train['playersJoined'])/100 + 1)
 train['damageDealtNorm'] = train['damageDealt']*(( 100-train['playersJoined'])/100 + 1)
 train['maxPlaceNorm'] = train['maxPlace']*(( 100-train['playersJoined'])/100 + 1)
 train['matchDurationNorm'] = train['matchDuration']*(( 100-train['playersJoined'])/100 + 1)
 train['DBNOsNorm'] = train['DBNOs']*(( 100-train['playersJoined'])/100 + 1)
 train['healsandboosts'] = train['heals'] + train['boosts']
aug = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
rotation_range=10,
zoom_range = 0.,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 aug.fit(x_train )<train_model>
train['totalDistance'] = train['rideDistance'] + train['walkDistance'] + train['swimDistance']
train['killsWithoutMoving'] =(( train['kills'] > 0)&(train['totalDistance'] == 0))
train['headshot_rate'] = train['headshotKills'] / train['kills']
train['headshot_rate'] = train['headshot_rate'].fillna(0)
 train.drop(train[train['killsWithoutMoving'] == True].index, inplace=True)
 train.drop('killsWithoutMoving', axis=1, inplace=True)
 train.drop(train[train['roadKills'] > 10].index, inplace=True)
 train.drop(train[train['kills'] > 35].index, inplace=True)
 train.drop(train[(train['headshot_rate'] == 1)&(train['kills'] > 11)].index,inplace=True)
 train.drop(train[train['walkDistance'] >= 10000].index, inplace=True)
 train.drop(train[train['rideDistance'] >= 20000].index, inplace=True)
 train.drop(train[train['swimDistance'] >= 2000].index, inplace=True)
 train.drop(train[train['weaponsAcquired'] >= 80].index, inplace=True)
 train.drop(train[train['heals'] >= 40].index, inplace=True )
h = model.fit_generator(
 aug.flow(x_train, y_train, batch_size=64),
validation_data=(x_test, y_test),
steps_per_epoch=len(x_train)// 64,
epochs=20, verbose=1,
callbacks=[learning_rate_reduction,best_model,early_stopping]
)<predict_on_test>
top = ['walkDistance','walkDistance','damageDealt']
bot = ['boosts','kills','kills']
for t,b in zip(top,bot):
train.loc[(train[b]!=0),t+'/'+b] = train[t]/train[b]
train.loc[(train[b]==0),t+'/'+b] = 0
print('a')
 features_to_agg = ['groupId','DBNOs','kills','totalDistance','headshot_rate',
'damageDealt','killPlace','walkDistance/boosts','walkDistance/kills','damageDealt/kills']
aggs = train[features_to_agg].groupby('groupId' ).agg([np.sum, np.mean, np.std])
 train_slice = train[['groupId']]
for col in list(aggs.columns.levels[0]):
for ag in list(aggs.columns.levels[1]):
train_slice = train_slice.join(aggs[col,ag], on='groupId')
 train_slice.columns = train_slice.columns.map(lambda x: x[0]+'_'+x[1] if type(x)==tuple else x)
 train_slice.fillna(0, inplace=True)
 del(aggs )
y_pred = model.predict(x_test)
 y_pred = np.argmax(y_pred,axis = 1)
 accuracy_score(y_test, y_pred )<predict_on_test>
train_slice.drop(columns='groupId', inplace=True)
 train = pd.concat([train, train_slice], axis=1)
 print(len(train))
train.head()
result = model.predict(submit)
 results = np.argmax(result,axis = 1)
 results<save_to_csv>
del(train_slice)
Label = pd.Series(results, name = 'Label')
 ImageId = pd.Series(range(1,28001), name = 'ImageId')
 submission = pd.concat([ImageId,Label], axis = 1)
 submission.to_csv('submission.csv', index = False )<set_options>
train = pd.concat([train, ranked], axis=1)
 del(ranked)
%reload_ext autoreload
%autoreload 2
%matplotlib inline
rn_seed=1
np.random.seed(rn_seed)
<define_variables>
train.drop(columns=['matchType', 'groupId','matchId', 'Id'], inplace=True)
train_path = Path('/tmp/train')
 test_path = Path('/tmp/test' )<define_variables>
sample = 3200000
df_sample = train.sample(sample)
 scaler = StandardScaler()
root = Path('.. /input' )<load_from_csv>
df = df_sample.drop(columns = ['winPlacePerc','winPlacePerc_rank'])
 y = df_sample['winPlacePerc']
del(df_sample )
id_lookup = pd.read_csv(root/'IdLookupTable.csv')
 train_csv = pd.read_csv(root/'training/training.csv')
 test_csv = pd.read_csv(root/'test/test.csv' )<data_type_conversions>
def split_vals(a, n : int):
return a[:n].copy() , a[n:].copy()
val_perc = 0.15
n_valid = int(val_perc * sample)
 n_trn = len(df)-n_valid
X_train, X_valid = split_vals(df, n_trn)
 y_train, y_valid = split_vals(y, n_trn)
 train_columns = list(X_train.columns)
 X_train = scaler.fit_transform(X_train)
 X_valid = scaler.transform(X_valid)
 print('Sample train shape: ', X_train.shape,
'Sample target shape: ', y_train.shape,
'Sample validation shape: ', X_valid.shape)
for c in train_csv.columns:
if(train_csv[c].dtype!='object'):
train_csv[c]=train_csv[c].fillna(train_csv[c].median() )<train_model>
def print_score(m : RandomForestRegressor):
res = ['mae train: ', mae(m.predict(X_train), y_train),
'mae val: ', mae(m.predict(X_valid), y_valid)]
if hasattr(m, 'oob_score_'): res.append(m.oob_score_)
 print(res )
def save_str_img(strimg,w,h,flpath):
px=255-np.array(strimg.split() ,dtype=int)
 if(len(px)==w*h and len(px)%w==0 and len(px)%h==0):
cpx = list(px.reshape(w,h))
img = Image(Tensor([cpx,cpx,cpx]))
img.save(flpath)
 return img
else:
raise Exception("Invalid height and width" )<load_from_csv>
pi_model = RandomForestRegressor(n_estimators=10, n_jobs=-1 ).fit(X_train, y_train)
 perm = PermutationImportance(pi_model, random_state=1 ).fit(X_valid, y_valid)
for index, train_row in train_csv.iterrows() :
save_str_img(train_row.Image,96,96,train_path/(str(index)+'.jpg'))<train_model>
fi_pi = pd.DataFrame({'importance':perm.feature_importances_, 'column':train_columns})
 to_keep = fi_pi.sort_values('importance', ascending=False)[0:80]['column']
to_keep = list(to_keep)
for index, test_row in test_csv.iterrows() :
save_str_img(test_row.Image,96,96,test_path/(str(index)+'.jpg'))<define_variables>
val_perc = 0.15
n_valid = int(val_perc * sample)
 n_trn = len(df)-n_valid
X_train, X_valid = split_vals(df, n_trn)
 y_train, y_valid = split_vals(y, n_trn)
 X_train = X_train[to_keep]
X_valid = X_valid[to_keep]
train_columns = list(X_train.columns)
 X_train = scaler.fit_transform(X_train)
 X_valid = scaler.transform(X_valid)
 print('Sample train shape: ', X_train.shape,
'Sample target shape: ', y_train.shape,
'Sample validation shape: ', X_valid.shape )
def get_locs(flname):
index = int(flname.name[:-4])
 plist=[]
coords=list(train_csv.loc[index])
 for i in range(len(coords)//2):
plist.append([coords[i*2+1],coords[i*2]])
 return tensor(plist)
<define_variables>
lgb_train = lgb.Dataset(X_train, y_train)
 lgb_eval = lgb.Dataset(X_valid, y_valid, reference=lgb_train)
 params = {
'boosting_type': 'goss',
'objective': 'regression',
'metric': 'mae',
'learning_rate': 0.05,
'feature_fraction': 0.9,
'verbose': 0,
'n_estimators':10000,
}
params = {"objective" : "regression", "metric" : "mae", 'n_estimators':15000,
'early_stopping_rounds':100, "num_leaves" : 40, "learning_rate" : 0.05,
"bagging_fraction" : 0.9, "bagging_seed" : 0, "num_threads" : 4,"colsample_bytree" : 0.7
}
gbm = lgb.train(params,
lgb_train,
num_boost_round=1000,
valid_sets=lgb_eval,
verbose_eval=1000)
 display(mae(gbm.predict(X_valid, num_iteration=gbm.best_iteration),y_valid))
data.show_batch(3,figsize=(6,6))<compute_test_metric>
( imp_df['importance_gain']<100 ).index
def mloss(y_true, y_pred):
y_true=y_true.view(-1,15,2)
 y_true[:,:,0]=y_true[:,:,0].clone() -y_pred[:,:,0]
y_true[:,:,1]=y_true[:,:,1].clone() -y_pred[:,:,1]
y_true[:,:,0]=y_true[:,:,0].clone() **2
y_true[:,:,1]=y_true[:,:,1].clone() **2
return y_true.sum(dim=2 ).sum(dim=1 ).sum()<choose_model_class>
def sub(test):
test['headshot_rate'] = test['headshotKills'] / test['kills']
test['headshot_rate'] = test['headshot_rate'].fillna(0)
 test['totalDistance'] = test['rideDistance'] + test['walkDistance'] + test['swimDistance']
test['playersJoined'] = test.groupby('matchId')['matchId'].transform('count')
 test['killsNorm'] = test['kills']*(( 100-test['playersJoined'])/100 + 1)
 test['damageDealtNorm'] = test['damageDealt']*(( 100-test['playersJoined'])/100 + 1)
 test['maxPlaceNorm'] = test['maxPlace']*(( 100-test['playersJoined'])/100 + 1)
 test['matchDurationNorm'] = test['matchDuration']*(( 100-test['playersJoined'])/100 + 1)
 test['DBNOsNorm'] = test['DBNOs']*(( 100-test['playersJoined'])/100 + 1)
 test['healsandboosts'] = test['heals'] + test['boosts']
top = ['walkDistance','walkDistance','damageDealt']
bot = ['boosts','kills','kills']
for t,b in zip(top,bot):
test.loc[(test[b]!=0),t+'/'+b] = test[t]/test[b]
test.loc[(test[b]==0),t+'/'+b] = 0
features_to_agg = ['groupId','DBNOs','kills','totalDistance','headshot_rate',
'damageDealt','killPlace','walkDistance/boosts','walkDistance/kills','damageDealt/kills']
aggs = test[features_to_agg].groupby('groupId' ).agg([np.sum, np.mean, np.std])
 test_slice = test[['groupId']]
for col in list(aggs.columns.levels[0]):
for ag in list(aggs.columns.levels[1]):
test_slice = test_slice.join(aggs[col,ag], on='groupId')
 test_slice.columns = test_slice.columns.map(lambda x: x[0]+'_'+x[1] if type(x)==tuple else x)
 test_slice.fillna(0, inplace=True)
 del(aggs)
 test_slice.drop(columns='groupId', inplace=True)
 test = pd.concat([test, test_slice], axis=1)
 del(test_slice)
 features = list(test.columns)
 features.remove('Id')
 features.remove('matchType')
 features.remove('groupId')
 print(features)
 ranked = test.groupby('matchId')[features].rank(pct=True)
 ranked.columns = ranked.columns.map(lambda x: x + '_rank')
 test = pd.concat([test, ranked], axis=1)
 del(ranked)
 test.drop(columns=['matchType', 'groupId','matchId', 'Id'], inplace=True)
 test = test[train_columns]
test.fillna(0, inplace=True)
 test = scaler.fit_transform(test)
 return test
test = pd.read_csv(KAGGLE_DIR + 'test_V2.csv')
 id_ = test['Id']
test = sub(test )
learn = cnn_learner(data,models.resnet152,loss_func=mloss )<train_model>
predictions = np.clip(a = gbm.predict(test, num_iteration=gbm.best_iteration), a_min=0.0, a_max=1.0)
 pred_df = pd.DataFrame({'Id' : id_, 'winPlacePerc' : predictions})
 pred_df
<save_model><EOS> 
pred_df.to_csv("submission_2.csv", index=False )