Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
xgb = XGBRegressor(random_state=100, learning_rate=0.1, n_estimators=200, max_depth=3, min_child_weight=5,
gamma = 0, subsample=0.8, colsample_by_tree=0.6,reg_alpha=0.001, reg_lambda=1)
 xgb.fit(X,y)
 y_pred = xgb.predict(X)
 print("Train error: ", root_mean_squared_error(y, y_pred))<train_model>
warnings.filterwarnings("ignore" )
xgb = XGBRegressor(random_state=100, learning_rate=0.05, n_estimators=400, max_depth=3, min_child_weight=5,
gamma = 0, subsample=0.8, colsample_by_tree=0.6,reg_alpha=0.001, reg_lambda=1)
 xgb.fit(X,y)
 y_pred = xgb.predict(X)
 print("Train error: ", root_mean_squared_error(y, y_pred))<train_model>
train_data = pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/train_V2.csv')
 test_data = pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/test_V2.csv')
 train_data.describe().drop('count' ).T
xgb = XGBRegressor(random_state=100, learning_rate=0.01, n_estimators=2000, max_depth=3, min_child_weight=5,
gamma = 0, subsample=0.8, colsample_by_tree=0.6,reg_alpha=0.001, reg_lambda=1)
 xgb.fit(X,y)
 y_pred = xgb.predict(X)
 print("Train error: ", root_mean_squared_error(y, y_pred))<compute_train_metric>
train_data[train_data['winPlacePerc'].isnull() ]
xgb_final = XGBRegressor(random_state=100, learning_rate=0.01, n_estimators=2000, max_depth=3, min_child_weight=5,
gamma = 0, subsample=0.8, colsample_by_tree=0.6,reg_alpha=0.001, reg_lambda=1)
 xgb_final.fit(X,y)
 y_pred = xgb_final.predict(X)
 print("XGBRegressor rmse: ", root_mean_squared_error(y, y_pred))<train_on_grid>

 mapper = lambda x: 'solo' if('solo'in x)else 'duo' if('duo' in x)or('crash'in x)else 'squad'
train_data['matchType'] = train_data['matchType'].apply(mapper)
 match_type_counts=train_data.groupby('matchId')['matchType'].first().value_counts().sort_values(ascending=False )
elastic_net = Pipeline([('scaler',RobustScaler()),('model',ElasticNet(random_state=100, normalize=True)) ])
 hyper_params = [{'model__alpha':[0.0001, 0.001,0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.2, 0.5, 1, 2,
5, 10, 50], 'model__l1_ratio':[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]}]
grid_search_cv(hyper_params, elastic_net, X_train, y_train, X_test, y_test )<compute_train_metric>
all_data = train_data.append(test_data, sort=False ).reset_index(drop=True)
 del train_data, test_data
gc.collect()
elastic_net_final = Pipeline([('scaler',RobustScaler()), 
('model',ElasticNet(random_state=100, normalize=True, alpha=0.0001, l1_ratio=0.9)) ])
 elastic_net_final.fit(X, y)
 y_pred = elastic_net_final.predict(X)
 print("ElasticNet rmse: ", root_mean_squared_error(y, y_pred))<train_on_grid>
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']
all_data['_totalDistance'] = all_data['rideDistance'] + all_data['walkDistance'] + all_data['swimDistance']
grb = GradientBoostingRegressor(random_state=100, learning_rate=0.1,subsample=0.8,min_samples_split=150,
min_samples_leaf=50, max_depth=5,max_features='sqrt')
 hyper_params = {'n_estimators':[50,150,200,250,300,350,400]}
grid_search_cv(hyper_params, grb, X_train, y_train, X_test, y_test )<train_on_grid>
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 )
grb = GradientBoostingRegressor(random_state=100, learning_rate=0.1, n_estimators=350, subsample=0.8,
min_samples_leaf=50, max_features='sqrt')
 hyper_params = {'max_depth':range(3,16,2), 'min_samples_split':range(150,300,50)}
grid_search_cv(hyper_params, grb, X_train, y_train, X_test, y_test )<train_on_grid>
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']
grb = GradientBoostingRegressor(random_state=100, learning_rate=0.1, n_estimators=350, max_depth=5,
min_samples_split=150, subsample=0.8, max_features='sqrt')
 hyper_params = {'min_samples_leaf': range(50,101,10)}
grid_search_cv(hyper_params, grb, X_train, y_train, X_test, y_test )<train_on_grid>
all_data.drop(['boosts','heals','killStreaks','DBNOs'], axis=1, inplace=True)
 all_data.drop(['headshotKills','roadKills','vehicleDestroys'], axis=1, inplace=True)
 all_data.drop(['rideDistance','swimDistance','matchDuration'], axis=1, inplace=True)
 all_data.drop(['rankPoints','killPoints','winPoints'], axis=1, inplace=True )
grb = GradientBoostingRegressor(random_state=100, learning_rate=0.1, n_estimators=350, max_depth=5,
min_samples_split=150, min_samples_leaf=50, max_features='sqrt')
 hyper_params = {'subsample':[0.6,0.7,0.75,0.8,0.85,0.9]}
grid_search_cv(hyper_params, grb, X_train, y_train, X_test, y_test )<train_model>
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)
 sum_col = ['kills','killPlace','damageDealt','walkDistance','_healthItems']
grb = GradientBoostingRegressor(random_state=100, learning_rate=0.1, n_estimators=350, max_depth=5,
min_samples_split=150, min_samples_leaf=50, max_features='sqrt', subsample=0.75)
 grb.fit(X, y)
 y_pred = grb.predict(X)
 print("grb rmse: ", root_mean_squared_error(y, y_pred))<train_model>
minKills = all_data.sort_values(['matchId','groupId','kills','killPlace'] ).groupby(
 ['matchId','groupId','kills'] ).first().reset_index().copy()
for n in np.arange(4):
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')
 del minKills, nKills
match_data = reduce_mem_usage(match_data )
grb = GradientBoostingRegressor(random_state=100, learning_rate=0.05, n_estimators=700, max_depth=5,
min_samples_split=150, min_samples_leaf=50, max_features='sqrt', subsample=0.75)
 grb.fit(X, y)
 y_pred = grb.predict(X)
 print("grb rmse: ", root_mean_squared_error(y, y_pred))<train_model>

 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['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 )
grb = GradientBoostingRegressor(random_state=100, learning_rate=0.01, n_estimators=3500, max_depth=5,
min_samples_split=150, min_samples_leaf=50, max_features='sqrt', subsample=0.75)
 grb.fit(X, y)
 y_pred = grb.predict(X)
 print("grb rmse: ", root_mean_squared_error(y, y_pred))<train_model>
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()
grb_final = GradientBoostingRegressor(random_state=100, learning_rate=0.01, n_estimators=3500, max_depth=5,
min_samples_split=150, min_samples_leaf=50, max_features='sqrt', subsample=0.75)
 grb_final.fit(X, y)
 y_pred = grb_final.predict(X)
 print("grb rmse: ", root_mean_squared_error(y, y_pred))<compute_train_metric>
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()
stacked = StackingCVRegressor(regressors=(lasso_final, ridge_final, elastic_net_final, grb_final),
meta_regressor=xgb_final, use_features_in_secondary=True)
 stacked.fit(X, y)
 y_pred = stacked.predict(X)
 print("StackingCVRegressor rmse: ", root_mean_squared_error(y, y_pred))<predict_on_test>
constant_column = [col for col in all_data.columns if all_data[col].nunique() == 1]
all_data.drop(constant_column, axis=1, inplace=True )
def get_blended_predictions(df):
return(
(0.05 * lasso_final.predict(df)) + 
(0.05 * ridge_final.predict(df)) + 
(0.05 * elastic_net_final.predict(df)) + 
(0.15 * grb_final.predict(df)) + 
(0.25 * xgb_final.predict(df)) + 
(0.45 * stacked.predict(df))
)<compute_test_metric>

 all_data['matchType'] = all_data['matchType'].apply(mapper)
 all_data = pd.concat([all_data, pd.get_dummies(all_data['matchType'])], axis=1)
 all_data.drop(['matchType'], axis=1, inplace=True)
 all_data['matchId'] = all_data['matchId'].apply(lambda x: int(x,16))
all_data['groupId'] = all_data['groupId'].apply(lambda x: int(x,16))
y_pred = get_blended_predictions(X)
 print("Blended model rmse: ", root_mean_squared_error(y, y_pred))<save_to_csv>
null_cnt = all_data.isnull().sum().sort_values()
housing_test_df.pop('SalePrice')
 housing_test_pred = get_blended_predictions(housing_test_df)
 housing_test_submission = pd.DataFrame()
housing_test_submission['Id'] = test_Ids
housing_test_submission['SalePrice'] = np.exp(housing_test_pred)
 housing_test_submission.to_csv('./submission.csv', index=False )<set_options>
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 )
%matplotlib inline<load_from_csv>
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()
train_matchId = X_train['matchId']
X_train.drop(['matchId','groupId'], axis=1, inplace=True)
 X_test.drop(['matchId','groupId'], axis=1, inplace=True )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<load_from_csv>
params={'learning_rate': 0.05,
'objective':'mae',
'metric':'mae',
'num_leaves': 128,
'verbose': 1,
'random_state':42,
'bagging_fraction': 0.7,
'feature_fraction': 0.7
}
reg = lgb.LGBMRegressor(**params, n_estimators=10000)
 reg.fit(X_train, Y_train)
 pred = reg.predict(X_test, num_iteration=reg.best_iteration_ )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<prepare_x_and_y>
X_test_grp['_nofit.winPlacePerc'] = pred
group = X_test_grp.groupby(['matchId'])
 X_test_grp['winPlacePerc'] = pred
X_test_grp['_rank.winPlacePerc'] = group['winPlacePerc'].rank(method='min')
 X_test = pd.concat([X_test, X_test_grp], axis=1 )
y_train = train['SalePrice']
test_id = test['Id']
ntrain = train.shape[0]
ntest = test.shape[0]<concatenate>
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 )
all_data = pd.concat(( train, test), sort=True ).reset_index(drop=True)
 all_data['Dataset'] = np.repeat(['Train', 'Test'], [ntrain, ntest], axis=0)
 all_data.drop('Id', axis=1,inplace=True )<sort_values>
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
cols_with_missing_values = all_data.isnull().sum().sort_values(ascending=False)
 display(pd.DataFrame(cols_with_missing_values[cols_with_missing_values[cols_with_missing_values > 0].index],
columns=["Number of Missing Values"]))<define_variables>
test = pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/test_V2.csv')
 test['matchId'] = test['matchId'].apply(lambda x: int(x,16))
test['groupId'] = test['groupId'].apply(lambda x: int(x,16))
submission = pd.merge(test, X_test[['matchId','groupId','winPlacePerc']])
 submission = submission[['Id','winPlacePerc']]
submission.to_csv("submission.csv", index=False )