Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
sum(People_on_ticket>Family_members+1 )<count_missing_values>
matchcount = train.matchId.nunique()
print("Number of unique matches: {}".format(train.matchId.nunique()))
print("Train Shape Before: {} Rows, {} Cols".format(*train.shape))
train = train.loc[train.matchId.isin(sorted(train.matchId.unique())[int(matchcount* 0.50):]),:]
print("Train Shape After: {} Rows, {} Cols".format(*train.shape))
print("Average age of lone passenger: ", round(np.nanmean(train_set_lone['Age']),0), sep="")
 print("Number of missing Age values: ", sum(np.isnan(train_set_lone['Age'])) , " - ",
round(( sum(np.isnan(train_set_lone['Age'])) *100/len(train_set_lone['Age'])) ,2), "% of train_set_lone.", sep="")
 print("The number of missing values is ",
round(sum(np.isnan(train_set_lone['Age'])) *100/sum(np.isnan(train_set['Age'])) ,2),
"% of entire missing values in Age variable in train_set.", sep="" )<drop_column>
lbl = preprocessing.LabelEncoder()
for col in ['matchType']:
lbl.fit(train[col])
 train[col] = lbl.transform(train[col])
 test[col] = lbl.transform(test[col])
 id_cols = ["Id","groupId","matchId"]
exclude = ["Id","groupId","matchId"]
trainlen = train.shape[0]
matchcount = train.matchId.nunique()
training = train.loc[train.matchId.isin(sorted(train.matchId.unique())[:int(matchcount* 0.85)]),
[x for x in train.columns if x not in exclude]]
print("Training Shape: {} Rows, {} Cols".format(*training.shape))
validating = train.loc[train.matchId.isin(sorted(train.matchId.unique())[int(matchcount* 0.85):]),
[x for x in train.columns if x not in exclude]]
print("Validating Shape: {} Rows, {} Cols".format(*validating.shape))
train_y = training.winPlacePerc
training.drop("winPlacePerc", axis =1, inplace=True)
 valid_y = validating.winPlacePerc
validating.drop("winPlacePerc", axis =1, inplace=True)
 lgb_train = lgb.Dataset(training, train_y,feature_name = "auto")
 lgb_valid = lgb.Dataset(validating, valid_y, feature_name = "auto")
 del training, validating
outlier_ind = train_set.loc[train_set['Fare']==max(train_set['Fare'])].index
train_set = train_set.drop(outlier_ind)
 train_set = train_set.reset_index(drop=True )<compute_test_metric>
print("Light Gradient Boosting Regressor: ")
 lgbm_params = {
'task': 'train',
'boosting_type': 'gbdt',
'objective': 'regression',
'metric': 'rmse',
'num_boost_round' : 5000
}
print("Minimum price for ticket in first class: ", min(train_set.loc[train_set['Pclass']==1]['Fare']), sep="" )<filter>
pred = gbm.predict(test.loc[:,[x for x in test.columns if x not in id_cols]])
 test['winPlacePercPred'] = np.clip(pred, a_min=0, a_max=1)
 aux = test.groupby(['matchId','groupId'])['winPlacePercPred'].agg('mean' ).groupby('matchId' ).rank(pct=True ).reset_index()
aux.columns = ['matchId','groupId','winPlacePerc']
test_sub = test.merge(aux, how='left', on=['matchId','groupId'])
 submission = test_sub[['Id', 'winPlacePerc']]
submission.to_csv('submissions_PubGG_LGBM.csv', index=False)
 display(submission.head() )
first_class = train_set.loc[train_set['Pclass']==1]
second_class = train_set.loc[train_set['Pclass']==2]
third_class = train_set.loc[train_set['Pclass']==3]
p_class = list(train_set['Pclass'])
 fare = list(train_set['Fare'])
 Fare_class = list()
for i in range(0,len(p_class)) :
if(p_class[i] == 1):
if(fare[i] < statistics.mean(second_class['Fare'])) :
Fare_class.append("cheap")
 else:
Fare_class.append("normal")
 elif(p_class[i] == 2):
if(fare[i] < statistics.mean(third_class['Fare'])) :
Fare_class.append("cheap")
 elif(fare[i] > statistics.mean(first_class['Fare'])) :
Fare_class.append("expensive")
 else:
Fare_class.append("normal")
 else:
if(fare[i] > statistics.mean(second_class['Fare'])) :
Fare_class.append("expensive")
 else:
Fare_class.append("normal")
 Fare_class = pd.Series(Fare_class)
 sns.countplot(Fare_class, hue='Survived', data=train_set)
 plt.legend(['No','Yes'], title="Survived", loc="upper right")
 plt.title("Fare_class: Survived vs Dead")
 plt.show()<count_values>
notcat = ["assists","boosts","damageDealt","DBNOs","heals","headshotKills","heals","killPlace","killPoints","kills",
"killStreaks","longestKill","maxPlace","numGroups","revives","rideDistance","roadKills","swimDistance",
"teamKills","vehicleDestroys","walkDistance","weaponsAcquired","winPoints"]
y = train.winPlacePerc.copy()
train.drop("winPlacePerc",axis =1, inplace=True)
 def agg_dataset(dataset):
for id_col in id_cols:
agg_features = dataset.groupby(id_col ).agg({k:["sum","mean","std"] for k in
["killPlace","walkDistance","numGroups","maxPlace","kills","longestKill","weaponsAcquired"]})
 agg_features.columns = pd.Index(["{}_agg_".format(id_col)+ e[0] +"_"+ e[1] for e in agg_features.columns.tolist() ])
 dataset = pd.merge(dataset,agg_features, on = id_col, how= "left")
 return dataset
train = agg_dataset(train)
 test = agg_dataset(test)
 missing = round(train.isnull().sum() / train.shape[0]*100 ).reset_index().rename({"index":"columns",0:"missing"}, axis =1)
 high_missing_columns = missing.loc[missing.missing > 65, "columns"]
print("Columns to remove(65% missing Values and Over)
 ", list(high_missing_columns))
train.drop(high_missing_columns,axis =1, inplace= True)
 test.drop(high_missing_columns,axis =1, inplace= True)
 train = pd.concat([train.reset_index(drop=True), y.reset_index(drop=True)], axis=1)
 training = train.loc[train.matchId.isin(sorted(train.matchId.unique())[:int(matchcount* 0.85)]),
[x for x in train.columns if x not in exclude]]
print("Training Shape: {} Rows, {} Cols".format(*training.shape))
validating = train.loc[train.matchId.isin(sorted(train.matchId.unique())[int(matchcount* 0.85):]),
[x for x in train.columns if x not in exclude]]
print("Validating Shape: {} Rows, {} Cols".format(*validating.shape))
train_y = training.winPlacePerc
training.drop("winPlacePerc", axis =1, inplace=True)
 valid_y = validating.winPlacePerc
validating.drop("winPlacePerc", axis =1, inplace=True)
 lgb_train = lgb.Dataset(training, train_y,feature_name = "auto")
 lgb_valid = lgb.Dataset(validating, valid_y, feature_name = "auto" )
train_set_no_cabins = train_set.loc[np.where(pd.isnull(train_set['Cabin'])) ]
train_set_cabins = train_set.loc[~train_set.index.isin(train_set_no_cabins.index)]
train_set_cabins['Pclass'].value_counts()<feature_engineering>
print("Light Gradient Boosting Regressor: ")
 lgbm_params = {
'task': 'train',
'boosting_type': 'gbdt',
'objective': 'regression',
'metric': 'rmse',
'num_boost_round' : 5000
}
stage = 'model training'
gbm = lgb.train(lgbm_params,
lgb_train,
num_boost_round=10000,
valid_sets=[lgb_train, lgb_valid],
feature_name='auto',
early_stopping_rounds=50,
verbose_eval=250 
)
 f, ax = plt.subplots(figsize=[7,10])
 lgb.plot_importance(gbm, max_num_features=25, ax=ax)
 plt.title("Light GBM Feature Importance
")
 plt.show()
print("Average age of Southampton passenger:", round(np.nanmean(train_set[train_set['Embarked'] == 'S']['Age'])))
 print("Average age of Queenstown passenger:", round(np.nanmean(train_set[train_set['Embarked'] == 'Q']['Age'])))
 print("Average age of Cherbourg passenger:", round(np.nanmean(train_set[train_set['Embarked'] == 'C']['Age'])) )<feature_engineering>
pred = gbm.predict(test.loc[:,[x for x in test.columns if x not in id_cols]])
 test['winPlacePercPred'] = np.clip(pred, a_min=0, a_max=1)
 aux = test.groupby(['matchId','groupId'])['winPlacePercPred'].agg('mean' ).groupby('matchId' ).rank(pct=True ).reset_index()
aux.columns = ['matchId','groupId','winPlacePerc']
test_sub = test.merge(aux, how='left', on=['matchId','groupId'])
 submission = test_sub[['Id', 'winPlacePerc']]
submission.to_csv('submissions_AGG_PubGG_LGBM.csv', index=False)
 print("Notebook Runtime: %0.2f Minutes"%(( time.time() - notebookstart)/60))
submission.head()