Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<load_from_csv>
import numpy as np
import pandas as pd
import os
season_result = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MRegularSeasonCompactResults.csv')
 season_result = season_result[season_result['Season'] < 2015]
season_win_result = season_result[['Season', 'WTeamID', 'WScore']]
season_lose_result = season_result[['Season', 'LTeamID', 'LScore']]
season_win_result.rename(columns={'WTeamID':'TeamID', 'WScore':'Score'}, inplace=True)
 season_lose_result.rename(columns={'LTeamID':'TeamID', 'LScore':'Score'}, inplace=True)
 season_result = pd.concat(( season_win_result, season_lose_result)).reset_index(drop=True)
 season_score = season_result.groupby(['Season', 'TeamID'])['Score'].sum().reset_index()<merge>
mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False
%matplotlib inline
sns.set_style('darkgrid')
 sns.set_palette('bone')
 warnings.filterwarnings('ignore')
 gc.enable()
INPUT_DIR = ".. /input/"
tourney_result = pd.merge(tourney_result, season_score, left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'], how='left')
 tourney_result.rename(columns={'Score':'WScoreT'}, inplace=True)
 tourney_result = tourney_result.drop('TeamID', axis=1)
 tourney_result = pd.merge(tourney_result, season_score, left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'], how='left')
 tourney_result.rename(columns={'Score':'LScoreT'}, inplace=True)
 tourney_result = tourney_result.drop('TeamID', axis=1 )<drop_column>
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 )
tourney_win_result = tourney_result.drop(['Season', 'WTeamID', 'LTeamID'], axis=1)
 tourney_win_result.rename(columns={'WSeed':'Seed1', 'LSeed':'Seed2', 'WScoreT':'ScoreT1', 'LScoreT':'ScoreT2'}, inplace=True )<prepare_output>
def feature_engineering(is_train=True):
if is_train:
print("processing train.csv")
 df = pd.read_csv(INPUT_DIR + 'train_V2.csv')
 df = df[df['maxPlace'] > 1]
else:
print("processing test.csv")
 df = pd.read_csv(INPUT_DIR + 'test_V2.csv')
 df.dropna(inplace=True)
 df['totalDistance'] = df['rideDistance'] + df["walkDistance"] + df["swimDistance"]
match = df.groupby('matchId')
 df['killPlacePerc'] = match['kills'].rank(pct=True ).values
df['walkDistancePerc'] = match['walkDistance'].rank(pct=True ).values
df['_totalDistance'] = df['rideDistance'] + df['walkDistance'] + df['swimDistance']
df['zombi'] =(( df['_totalDistance'] == 0)|(df['kills'] == 0)
 |(df['weaponsAcquired'] == 0)
 |(df['matchType'].str.contains('solo')) ).astype(int)
 df['cheater'] =(( df['kills'] / df['_totalDistance'] >= 1)
 |(df['kills'] > 30)|(df['roadKills'] > 10)).astype(int)
 pd.concat([df['zombi'].value_counts() , df['cheater'].value_counts() ], axis=1 ).T
df['_healthAndBoosts'] = df['heals'] + df['boosts']
df['_killDamage'] = df['kills'] * 100 + df['damageDealt']
df['_killPlaceOverMaxPlace'] = df['killPlace'] / df['maxPlace']
df['_killsOverWalkDistance'] = df['kills'] / df['walkDistance']
df['_walkDistancePerSec'] = df['walkDistance'] / df['matchDuration']
fillInf(df, 0)
 mapper = lambda x: 'solo' if('solo' in x)else 'duo' if('duo' in x)or('crash' in x)else 'squad'
df['matchType'] = df['matchType'].map(mapper)
 df['matchType'] = df['matchType'].map(mapper)
 a = pd.get_dummies(df['matchType'], prefix='matchType')
 df = pd.concat([df, a], axis=1)
 df.drop(['headshotKills','teamKills','roadKills','vehicleDestroys'], axis=1, inplace=True)
 df.drop(['rideDistance','swimDistance','matchDuration'], axis=1, inplace=True)
 df.drop(['rankPoints','killPoints','winPoints'], axis=1, inplace=True)
 df.drop(['matchType'], axis=1, inplace=True)
 print("remove some columns")
 target = 'winPlacePerc'
features = list(df.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 y = None
print("get target")
 if is_train:
y = np.array(df.groupby(['matchId','groupId'])[target].agg('mean'), dtype=np.float64)
 features.remove(target)
 print("get group mean feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('mean')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
if is_train: df_out = agg.reset_index() [['matchId','groupId']]
else: df_out = df[['matchId','groupId']]
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_mean", "_mean_rank"], how='left', on=['matchId', 'groupId'])
 del agg, agg_rank
gc.collect()
print("get group max feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('max')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_max", "_max_rank"], how='left', on=['matchId', 'groupId'])
 del agg, agg_rank
gc.collect()
print("get group min feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('min')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_min", "_min_rank"], how='left', on=['matchId', 'groupId'])
 del agg, agg_rank
gc.collect()
print("get group size feature")
 agg = df.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_out = df_out.merge(agg, how='left', on=['matchId', 'groupId'])
 print("get match mean feature")
 agg = df.groupby(['matchId'])[features].agg('mean' ).reset_index()
df_out = df_out.merge(agg, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 del agg
gc.collect()
print("get match size feature")
 agg = df.groupby(['matchId'] ).size().reset_index(name='match_size')
 df_out = df_out.merge(agg, how='left', on=['matchId'])
 gc.collect()
df_out.drop(["matchId", "groupId"], axis=1, inplace=True)
 X = np.array(df_out, dtype=np.float64)
 feature_names = list(df_out.columns)
 del df, df_out, agg
gc.collect()
return X, y, feature_names
tourney_lose_result = tourney_win_result.copy()
tourney_lose_result['Seed1'] = tourney_win_result['Seed2']
tourney_lose_result['Seed2'] = tourney_win_result['Seed1']
tourney_lose_result['ScoreT1'] = tourney_win_result['ScoreT2']
tourney_lose_result['ScoreT2'] = tourney_win_result['ScoreT1']<feature_engineering>
x_train, y, feature_names = feature_engineering(True)
 scaler = MinMaxScaler(feature_range=(-1, 1), copy=False ).fit(x_train)
 scaler.transform(x_train )
tourney_win_result['Seed_diff'] = tourney_win_result['Seed1'] - tourney_win_result['Seed2']
tourney_win_result['ScoreT_diff'] = tourney_win_result['ScoreT1'] - tourney_win_result['ScoreT2']
tourney_lose_result['Seed_diff'] = tourney_lose_result['Seed1'] - tourney_lose_result['Seed2']
tourney_lose_result['ScoreT_diff'] = tourney_lose_result['ScoreT1'] - tourney_lose_result['ScoreT2']<concatenate>
x_prediction, _, _ = feature_engineering(False)
 scaler = MinMaxScaler(feature_range=(-1, 1), copy=False ).fit(x_prediction)
 scaler.transform(x_prediction )
tourney_win_result['result'] = 1
tourney_lose_result['result'] = 0
train_df = pd.concat(( tourney_win_result, tourney_lose_result)).reset_index(drop=True)
 train_df<load_from_csv>
X_train,X_test, y_train, y_test =train_test_split(x_train,y,test_size=0.3, random_state=0 )
test_df = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MSampleSubmissionStage1_2020.csv')
 sub = test_df.copy()<feature_engineering>
%%time
linreg = LinearRegression()
linreg.fit(X_train, y_train)
 print(linreg.intercept_)
 print(linreg.coef_ )
test_df['Season'] = test_df['ID'].map(lambda x: int(x[:4]))
test_df['WTeamID'] = test_df['ID'].map(lambda x: int(x[5:9]))
test_df['LTeamID'] = test_df['ID'].map(lambda x: int(x[10:14]))<load_from_csv>
tourney_seed = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MNCAATourneySeeds.csv')
 tourney_seed = tourney_seed[tourney_seed['Season'] > 2014]<load_from_csv>
season_result = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament/MDataFiles_Stage1/MRegularSeasonCompactResults.csv')
 season_result = season_result[season_result['Season'] > 2014]
season_win_result = season_result[['Season', 'WTeamID', 'WScore']]
season_lose_result = season_result[['Season', 'LTeamID', 'LScore']]
season_win_result.rename(columns={'WTeamID':'TeamID', 'WScore':'Score'}, inplace=True)
 season_lose_result.rename(columns={'LTeamID':'TeamID', 'LScore':'Score'}, inplace=True)
 season_result = pd.concat(( season_win_result, season_lose_result)).reset_index(drop=True)
 season_score = season_result.groupby(['Season', 'TeamID'])['Score'].sum().reset_index()<merge>
%%time
result = linreg.predict(x_prediction )
test_df = pd.merge(test_df, tourney_seed, left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'], how='left')
 test_df.rename(columns={'Seed':'Seed1'}, inplace=True)
 test_df = test_df.drop('TeamID', axis=1)
 test_df = pd.merge(test_df, tourney_seed, left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'], how='left')
 test_df.rename(columns={'Seed':'Seed2'}, inplace=True)
 test_df = test_df.drop('TeamID', axis=1)
 test_df = pd.merge(test_df, season_score, left_on=['Season', 'WTeamID'], right_on=['Season', 'TeamID'], how='left')
 test_df.rename(columns={'Score':'ScoreT1'}, inplace=True)
 test_df = test_df.drop('TeamID', axis=1)
 test_df = pd.merge(test_df, season_score, left_on=['Season', 'LTeamID'], right_on=['Season', 'TeamID'], how='left')
 test_df.rename(columns={'Score':'ScoreT2'}, inplace=True)
 test_df = test_df.drop('TeamID', axis=1 )<feature_engineering>
%%time
test_data = pd.read_csv(INPUT_DIR+'test_V2.csv')
 print("fix winPlacePerc")
 for i in range(len(test_data)) :
winPlacePerc = result[i]
maxPlace = int(test_data.iloc[i]['maxPlace'])
 if maxPlace == 0:
winPlacePerc = 0.0
elif maxPlace == 1:
winPlacePerc = 1.0
else:
gap = 1.0 /(maxPlace - 1)
 winPlacePerc = round(winPlacePerc / gap)* gap
if winPlacePerc < 0: winPlacePerc = 0.0
if winPlacePerc > 1: winPlacePerc = 1.0
result[i] = winPlacePerc
test_df['Seed1'] = test_df['Seed1'].map(lambda x: get_seed(x))
test_df['Seed2'] = test_df['Seed2'].map(lambda x: get_seed(x))
test_df['Seed_diff'] = test_df['Seed1'] - test_df['Seed2']
test_df['ScoreT_diff'] = test_df['ScoreT1'] - test_df['ScoreT2']
test_df = test_df.drop(['ID', 'Pred', 'Season', 'WTeamID', 'LTeamID'], axis=1)
 test_df<prepare_x_and_y>
f3=open(INPUT_DIR+'sample_submission_V2.csv')
 submit=pd.read_csv(f3)
 sample_result = pd.DataFrame(result,columns = ['winPlacePerc'])
 submit['winPlacePerc'] = sample_result
submit.to_csv(r'sample_submission_lineregression.csv', index=False)
 del f3,result,submit
gc.collect()
X = train_df.drop('result', axis=1)
 y = train_df.result<init_hyperparams>
%%time
model_lasso = Lasso(alpha=0.001)
 model_lasso.fit(X_train, y_train)
 print(model_lasso.intercept_)
 print(model_lasso.coef_ )
params_lgb = {'num_leaves': lgb_num_leaves_max,
'min_data_in_leaf': lgb_in_leaf,
'objective': 'binary',
'max_depth': -1,
'learning_rate': lgb_lr,
"boosting_type": "gbdt",
"bagging_seed": lgb_bagging,
"metric": 'logloss',
"verbosity": -1,
'random_state': 42,
}<prepare_x_and_y>
%%time
predicted_lasso = model_lasso.predict(x_prediction )
NFOLDS = 10
folds = KFold(n_splits=NFOLDS)
 columns = X.columns
splits = folds.split(X, y)
 y_preds_lgb = np.zeros(test_df.shape[0])
 y_train_lgb = np.zeros(X.shape[0])
 y_oof = np.zeros(X.shape[0])
 feature_importances = pd.DataFrame()
feature_importances['feature'] = columns
for fold_n,(train_index, valid_index)in enumerate(splits):
print('Fold:',fold_n+1)
 X_train, X_valid = X[columns].iloc[train_index], X[columns].iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
dtrain = lgb.Dataset(X_train, label=y_train)
 dvalid = lgb.Dataset(X_valid, label=y_valid)
 clf = lgb.train(params_lgb, dtrain, 10000, valid_sets = [dtrain, dvalid], verbose_eval=200)
 feature_importances[f'fold_{fold_n + 1}'] = clf.feature_importance()
y_pred_valid = clf.predict(X_valid)
 y_oof[valid_index] = y_pred_valid
y_train_lgb += clf.predict(X)/ NFOLDS
y_preds_lgb += clf.predict(test_df)/ NFOLDS
del X_train, X_valid, y_train, y_valid
gc.collect()<init_hyperparams>
%%time
test_data = pd.read_csv(INPUT_DIR+'test_V2.csv')
 print("fix winPlacePerc")
 for i in range(len(test_data)) :
winPlacePerc = predicted_lasso[i]
maxPlace = int(test_data.iloc[i]['maxPlace'])
 if maxPlace == 0:
winPlacePerc = 0.0
elif maxPlace == 1:
winPlacePerc = 1.0
else:
gap = 1.0 /(maxPlace - 1)
 winPlacePerc = round(winPlacePerc / gap)* gap
if winPlacePerc < 0: winPlacePerc = 0.0
if winPlacePerc > 1: winPlacePerc = 1.0
predicted_lasso[i] = winPlacePerc
params_xgb = {'max_depth': xgb_max_depth,
'objective': 'binary:logistic',
'min_child_weight': xgb_min_child_weight,
'learning_rate': xgb_lr,
'eta' : 0.3,
'subsample': 0.8,
'lambda ' : 4,
'eval_metric': 'logloss',
'colsample_bytree ': 0.9,
'colsample_bylevel': 1
}<prepare_x_and_y>
f4=open(INPUT_DIR+'sample_submission_V2.csv')
 submit_lasso=pd.read_csv(f4)
 sample_result_lasso = pd.DataFrame(predicted_lasso,columns = ['winPlacePerc'])
 submit_lasso['winPlacePerc'] = sample_result_lasso
submit_lasso.to_csv(r'sample_submission_lasso.csv', index=False)
 del f4,submit_lasso,sample_result_lasso
gc.collect()
NFOLDS = 10
folds = KFold(n_splits=NFOLDS)
 columns = X.columns
splits = folds.split(X, y)
 y_preds_xgb = np.zeros(test_df.shape[0])
 y_train_xgb = np.zeros(X.shape[0])
 y_oof_xgb = np.zeros(X.shape[0])
 train_df_set = xgb.DMatrix(X)
 test_set = xgb.DMatrix(test_df)
 for fold_n,(train_index, valid_index)in enumerate(splits):
print('Fold:',fold_n+1)
 X_train, X_valid = X[columns].iloc[train_index], X[columns].iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
train_set = xgb.DMatrix(X_train, y_train)
 val_set = xgb.DMatrix(X_valid, y_valid)
 clf = xgb.train(params_xgb, train_set, num_boost_round=xgb_num_boost_round_max, evals=[(train_set, 'train'),(val_set, 'val')], verbose_eval=100)
 y_train_xgb += clf.predict(train_df_set)/ NFOLDS
y_preds_xgb += clf.predict(test_set)/ NFOLDS
del X_train, X_valid, y_train, y_valid
gc.collect()<normalization>
%%time
model_ridge = Ridge(alpha=0.5994842503189409)
 model_ridge.fit(X_train, y_train)
 print(model_ridge.intercept_)
 print(model_ridge.coef_ )
%%time
scaler = StandardScaler()
train_log = pd.DataFrame(
 scaler.fit_transform(X),
columns=X.columns,
index=X.index 
)
 test_log = pd.DataFrame(
 scaler.transform(test_df),
columns=test_df.columns,
index=test_df.index
)<train_on_grid>
%%time
predicted_ridge = model_ridge.predict(x_prediction )
logreg = LogisticRegression()
logreg.fit(train_log, y)
 coeff_logreg = pd.DataFrame(train_log.columns.delete(0))
coeff_logreg.columns = ['feature']
coeff_logreg["score_logreg"] = pd.Series(logreg.coef_[0])
 coeff_logreg.sort_values(by='score_logreg', ascending=False )<load_pretrained>
%%time
test_data = pd.read_csv(INPUT_DIR+'test_V2.csv')
 print("fix winPlacePerc")
 for i in range(len(test_data)) :
winPlacePerc = predicted_ridge[i]
maxPlace = int(test_data.iloc[i]['maxPlace'])
 if maxPlace == 0:
winPlacePerc = 0.0
elif maxPlace == 1:
winPlacePerc = 1.0
else:
gap = 1.0 /(maxPlace - 1)
 winPlacePerc = round(winPlacePerc / gap)* gap
if winPlacePerc < 0: winPlacePerc = 0.0
if winPlacePerc > 1: winPlacePerc = 1.0
predicted_ridge[i] = winPlacePerc
eli5.show_weights(logreg )<predict_on_test>
f5=open(INPUT_DIR+'sample_submission_V2.csv')
 submit_ridge=pd.read_csv(f5)
 sample_result_ridge = pd.DataFrame(predicted_ridge,columns = ['winPlacePerc'])
 submit_ridge['winPlacePerc'] = sample_result_ridge
submit_ridge.to_csv(r'sample_submission_ridge.csv', index=False)
 del f5,submit_ridge,sample_result_ridge
gc.collect()
y_logreg_train = logreg.predict(train_log)
 y_logreg_pred = logreg.predict(test_log )<compute_test_metric>
%%time
model_elasticnet = ElasticNet(alpha=1.6152516038498196e-06, copy_X=True, fit_intercept=True, l1_ratio=0.5,
max_iter=1000, normalize=False, positive=False, precompute=False,
random_state=0, selection='cyclic', tol=0.0001, warm_start=False)
 model_elasticnet.fit(X_train, y_train)
 print(model_elasticnet.intercept_)
 print(model_elasticnet.coef_ )
def plot_cm(y_true, y_pred, title, figsize=(7,6)) :
y_pred = y_pred.round().astype(int)
 cm = confusion_matrix(y_true, y_pred, labels=np.unique(y_true))
cm_sum = np.sum(cm, axis=1, keepdims=True)
 cm_perc = cm / cm_sum.astype(float)* 100
annot = np.empty_like(cm ).astype(str)
 nrows, ncols = cm.shape
for i in range(nrows):
for j in range(ncols):
c = cm[i, j]
p = cm_perc[i, j]
if i == j:
s = cm_sum[i]
annot[i, j] = '%.1f%%
%d/%d' %(p, c, s)
 elif c == 0:
annot[i, j] = ''
else:
annot[i, j] = '%.1f%%
%d' %(p, c)
 cm = pd.DataFrame(cm, index=np.unique(y_true), columns=np.unique(y_true))
cm.index.name = 'Actual'
cm.columns.name = 'Predicted'
fig, ax = plt.subplots(figsize=figsize)
 plt.title(title)
 sns.heatmap(cm, cmap= "YlGnBu", annot=annot, fmt='', ax=ax )<prepare_output>
%%time
predicted_elasticNet = model_elasticnet.predict(x_prediction )
y_preds = w_lgb*y_preds_lgb + w_xgb*y_preds_xgb + w_logreg*y_logreg_pred<prepare_output>
%%time
test_data = pd.read_csv(INPUT_DIR+'test_V2.csv')
 print("fix winPlacePerc")
 for i in range(len(test_data)) :
winPlacePerc = predicted_elasticNet[i]
maxPlace = int(test_data.iloc[i]['maxPlace'])
 if maxPlace == 0:
winPlacePerc = 0.0
elif maxPlace == 1:
winPlacePerc = 1.0
else:
gap = 1.0 /(maxPlace - 1)
 winPlacePerc = round(winPlacePerc / gap)* gap
if winPlacePerc < 0: winPlacePerc = 0.0
if winPlacePerc > 1: winPlacePerc = 1.0
predicted_elasticNet[i] = winPlacePerc
<save_to_csv><EOS> 
f6=open(INPUT_DIR+'sample_submission_V2.csv')
 submit_elasticNet=pd.read_csv(f6)
 sample_result_elasticNet = pd.DataFrame(predicted_elasticNet,columns = ['winPlacePerc'])
 submit_elasticNet['winPlacePerc'] = sample_result_elasticNet
submit_elasticNet.to_csv(r'sample_submission_elasticNet.csv', index=False)
 del f6,submit_elasticNet,sample_result_elasticNet
gc.collect()