Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
sns.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 2.5} )<load_from_csv>
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))
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head()<load_from_csv>
lbl = preprocessing.LabelEncoder()
for col in ['matchType']:
lbl.fit(train[col])
 train[col] = lbl.transform(train[col])
 test[col] = lbl.transform(test[col] )
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()<count_missing_values>
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
train_data.isna().sum()<count_unique_values>
print("Light Gradient Boosting Regressor: ")
 lgbm_params = {
'task': 'train',
'boosting_type': 'gbdt',
'objective': 'regression',
'metric': 'rmse',
'num_boost_round' : 5000
}
train_data.nunique()<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('PubGG_LGBM.csv', index=False)
 display(submission.head() )
train_data['Cabin'] = train_data['Cabin'].apply(lambda i: i[0] if pd.notnull(i)else 'Z')
 test_data['Cabin'] = test_data['Cabin'].apply(lambda i: i[0] if pd.notnull(i)else 'Z' )<sort_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"]
train_data[train_data['Cabin']=='T'].index.values<sort_values>
y = train.winPlacePerc.copy()
train.drop("winPlacePerc",axis =1, inplace=True )
test_data[test_data['Cabin']=='T'].index.values<filter>
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_data.iloc[339]<feature_engineering>
train = agg_dataset(train)
 test = agg_dataset(test )
index = train_data[train_data['Cabin'] == 'T'].index
train_data.loc[index, 'Cabin'] = 'A'<categorify>
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_data['Cabin'] = train_data['Cabin'].replace(['A', 'B', 'C'], 'ABC')
 train_data['Cabin'] = train_data['Cabin'].replace(['D', 'E'], 'DE')
 train_data['Cabin'] = train_data['Cabin'].replace(['F', 'G'], 'FG')
 test_data['Cabin'] = test_data['Cabin'].replace(['A', 'B', 'C'], 'ABC')
 test_data['Cabin'] = test_data['Cabin'].replace(['D', 'E'], 'DE')
 test_data['Cabin'] = test_data['Cabin'].replace(['F', 'G'], 'FG' )<data_type_conversions>
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_data.drop(["Ticket", "Name", "PassengerId"], axis=1, inplace=True)
 test_data.drop(["Ticket", "Name", "PassengerId"], axis=1, inplace=True)
 train_data["Age"].fillna(train_data["Age"].median(skipna=True), inplace=True)
 test_data["Age"].fillna(test_data["Age"].median(skipna=True), inplace=True)
 test_data["Fare"].fillna(test_data["Fare"].median(skipna=True), inplace=True)
 train_data["Embarked"].fillna('S', inplace=True)
 test_data["Embarked"].fillna('S', inplace=True )<categorify>
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()
gender = {'male': 0, 'female': 1}
train_data.Sex = [gender[item] for item in train_data.Sex]
test_data.Sex = [gender[item] for item in test_data.Sex]
embarked = {'S': 0, 'C': 1, 'Q':2}
train_data.Embarked = [embarked[item] for item in train_data.Embarked]
test_data.Embarked = [embarked[item] for item in test_data.Embarked]
train_data['Cabin'] = LabelEncoder().fit_transform(train_data['Cabin'])
 test_data['Cabin'] = LabelEncoder().fit_transform(test_data['Cabin'] )<load_from_csv>
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('AGG_PubGG_LGBM.csv', index=False )