Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
train = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-3/train.csv")
 test = pd.read_csv("/kaggle/input/covid19-global-forecasting-week-3/test.csv")
 train.tail()<define_variables>
def toTapleList(list1,list2):
return list(itertools.product(list1,list2))
mask = train['Date'].max()
world_cum_confirmed = sum(train[train['Date'] == mask].ConfirmedCases)
 world_cum_fatal = sum(train[train['Date'] == mask].Fatalities )<train_model>
%%time
train = pd.read_csv('.. /input/train_V2.csv')
 train = reduce_mem_usage(train)
 test = pd.read_csv('.. /input/test_V2.csv')
 test = reduce_mem_usage(test)
 print(train.shape, test.shape )
print('Number of Countires are: ', len(train['Country_Region'].unique()))
print('Training dataset ends at: ', mask)
 print('Number of cumulative confirmed cases worldwide are: ', world_cum_confirmed)
 print('Number of cumulative fatal cases worldwide are: ', world_cum_fatal )<sort_values>
null_cnt = train.isnull().sum().sort_values()
print(null_cnt[null_cnt > 0])
 train.dropna(inplace=True )
cum_per_country = train[train['Date'] == mask].groupby(['Date','Country_Region'] ).sum().sort_values(['ConfirmedCases'], ascending=False)
 cum_per_country[:10]<groupby>
for c in ['Id','groupId','matchId']:
print(f'unique [{c}] count:', train[c].nunique() )
date = train['Date'].unique()
cc_us = train[train['Country_Region'] == 'US'].groupby(['Date'] ).sum().ConfirmedCases
ft_us = train[train['Country_Region'] == 'US'].groupby(['Date'] ).sum().Fatalities
cc_ity = train[train['Country_Region'] == 'Italy'].groupby(['Date'] ).sum().ConfirmedCases
ft_ity = train[train['Country_Region'] == 'Italy'].groupby(['Date'] ).sum().Fatalities
cc_spn = train[train['Country_Region'] == 'Spain'].groupby(['Date'] ).sum().ConfirmedCases
ft_spn = train[train['Country_Region'] == 'Spain'].groupby(['Date'] ).sum().Fatalities
cc_gmn = train[train['Country_Region'] == 'Germany'].groupby(['Date'] ).sum().ConfirmedCases
ft_gmn = train[train['Country_Region'] == 'Germany'].groupby(['Date'] ).sum().Fatalities
cc_frc = train[train['Country_Region'] == 'France'].groupby(['Date'] ).sum().ConfirmedCases
ft_frc = train[train['Country_Region'] == 'France'].groupby(['Date'] ).sum().Fatalities
fig = go.Figure()
fig.add_trace(go.Scatter(x=date, y=cc_us, name='US'))
fig.add_trace(go.Scatter(x=date, y=cc_ity, name='Italy'))
fig.add_trace(go.Scatter(x=date, y=cc_spn, name='Spain'))
fig.add_trace(go.Scatter(x=date, y=cc_gmn, name='Germany'))
fig.add_trace(go.Scatter(x=date, y=cc_frc, name='France'))
fig.update_layout(title="Plot of Cumulative Cases for Top 5 countires(except China)",
xaxis_title="Date",
yaxis_title="Cases")
 fig.update_xaxes(nticks=30)
 fig.show()<data_type_conversions>
for q in ['numGroups == maxPlace','numGroups != maxPlace']:
print(q, ':', len(train.query(q)) )
train['Date'] = pd.to_datetime(train['Date'])
 test['Date'] = pd.to_datetime(test['Date'])
 train['Country_Region'] = train['Country_Region'].astype(str)
 test['Country_Region'] = test['Country_Region'].astype(str)
<feature_engineering>
print(group['players in group'].nlargest(5))
del match,group
EMPTY_VAL = "EMPTY_VAL"
def fillState(state, country):
if state == EMPTY_VAL: return country
return state
train['Province_State'].fillna(EMPTY_VAL, inplace=True)
 train['Province_State'] = train.loc[:, ['Province_State', 'Country_Region']].apply(lambda x : fillState(x['Province_State'], x['Country_Region']), axis=1)
 test['Province_State'].fillna(EMPTY_VAL, inplace=True)
 test['Province_State'] = test.loc[:, ['Province_State', 'Country_Region']].apply(lambda x : fillState(x['Province_State'], x['Country_Region']), axis=1 )<categorify>
( train.groupby('matchId')['matchDuration'].nunique() > 1 ).any()
le = preprocessing.LabelEncoder()
train['country_encoder'] = le.fit_transform(train['Country_Region'])
 train['date_int'] = train['Date'].apply(lambda x: datetime.strftime(x, '%m%d')).astype(int)
 test['country_encoder'] = le.transform(test['Country_Region'])
 test['date_int'] = test['Date'].apply(lambda x: datetime.strftime(x, '%m%d')).astype(int )<categorify>
le = preprocessing.LabelEncoder()
train['province_encoder'] = le.fit_transform(train['Province_State'])
 test['province_encoder'] = le.transform(test['Province_State'] )<count_duplicates>
print('match count:', train['matchId'].nunique())
 maxPlacePerc = train.groupby('matchId')['winPlacePerc'].max()
print('match [not contains 1st place]:', len(maxPlacePerc[maxPlacePerc != 1]))
del maxPlacePerc
sub = train[(train['maxPlace'] > 1)&(train['numGroups'] == 1)]
print('match [maxPlace>1 & numGroups==1]:', len(sub.groupby('matchId')))
 print(' - unique winPlacePerc:', sub['winPlacePerc'].unique() )
start_time = time.time()
country = train['Country_Region'].drop_duplicates()
train_df = train.copy()
train_df.rename(columns={'Date': 'date', 'ConfirmedCases': 'cc_cases', 'Fatalities': 'ft_cases', 'Country_Region': 'country', 'Province_State': 'province'}, inplace=True)
 lags = np.arange(1,8,1)
 with tqdm(total = len(list(train_df['date'].unique())))as pbar:
for d in train_df['date'].drop_duplicates() :
for i in country:
province = train_df[train_df['country'] == i]['province'].drop_duplicates()
for j in province:
mask =(train_df['date'] == d)&(train_df['country'] == i)&(train_df['province'] == j)
 for lag in lags:
mask_org =(train_df['date'] ==(d - pd.Timedelta(days=lag)))&(train_df['country'] == i)&(train_df['province'] == j)
 try:
train_df.loc[mask, 'cc_cases_' + str(lag)] = train_df.loc[mask_org, 'cc_cases'].values
except:
train_df.loc[mask, 'cc_cases_' + str(lag)] = 0
try:
train_df.loc[mask, 'ft_cases_' + str(lag)] = train_df.loc[mask_org, 'ft_cases'].values
except:
train_df.loc[mask, 'ft_cases_' + str(lag)] = 0
pbar.update(1)
 print('Time spent for building features is {} minutes'.format(round(( time.time() -start_time)/60,1)) )<load_from_csv>
pd.concat([train[train['winPlacePerc'] == 1].head(5),
train[train['winPlacePerc'] == 0].head(5)],
keys=['winPlacePerc_1', 'winPlacePerc_0'] )
train_df = train_df[train_df['date_int']>=301]
train_df['weekday'] = train_df['date'].dt.weekday
train_df[train_df['country'] == 'Italy'].tail(10 )<split>
grpSolo.nlargest(10, 'kills' )
def split_train_val(df, val_ratio):
val_len = int(len(df)* val_ratio)
 train_set = df[:-val_len]
val_set = df[-val_len:]
return train_set, val_set<prepare_x_and_y>
grpTeam.nlargest(10, 'kills' )
test_fixed_cols = ['ForecastId', 'Province_State', 'Country_Region', 'Date']
fixed_cols = ['Id', 'province', 'country', 'date']
output_cols = ['cc_cases', 'ft_cases']
input_cols = list(set(train_df.columns.to_list())- set(fixed_cols)- set(output_cols))
print('output columns are ', output_cols)
 print('input columns are ', input_cols)
 X = train_df[input_cols]
y = train_df[output_cols]<categorify>
print(pd.DataFrame([[val for val in dir() ], [sys.getsizeof(eval(val)) for val in dir() ]],
index=['name','size'] ).T.sort_values('size', ascending=False ).reset_index(drop=True)[:10] )
cc_input = ['country_encoder', 'province_encoder', 'weekday', 'date_int','cc_cases_1', 'cc_cases_2', 'cc_cases_3', 'cc_cases_4', 'cc_cases_5', 'cc_cases_6', 'cc_cases_7']
ft_input = ['country_encoder', 'province_encoder', 'weekday' , 'date_int', 'ft_cases_1', 'ft_cases_2', 'ft_cases_3', 'ft_cases_4', 'ft_cases_5', 'ft_cases_6', 'ft_cases_7']
cc_output = ['cc_cases']
ft_output = ['ft_cases']
val_ratio = 0.05
X_cc = X[cc_input]
X_ft = X[ft_input]
y_cc = y[cc_output]
y_ft = y[ft_output]
train_X_cc, val_X_cc = split_train_val(df = X_cc, val_ratio = val_ratio)
 train_y_cc, val_y_cc = split_train_val(df = y_cc, val_ratio = val_ratio)
 train_X_ft, val_X_ft = split_train_val(df = X_ft, val_ratio = val_ratio)
 train_y_ft, val_y_ft = split_train_val(df = y_ft, val_ratio = val_ratio )<normalization>
all_data = train.append(test, sort=False ).reset_index(drop=True)
 del train, test
gc.collect()
<normalization>
match = all_data.groupby('matchId')
 all_data['killPlacePerc'] = match['kills'].rank(pct=True ).values
all_data['walkDistancePerc'] = match['walkDistance'].rank(pct=True ).values
all_data['damageDealtPerc'] = match['damageDealt'].rank(pct=True ).values
<data_type_conversions>
_=
X_train_cc = train_X_cc.to_numpy()
X_val_cc = val_X_cc.to_numpy()
X_train_ft = train_X_ft.to_numpy()
X_val_ft = val_X_ft.to_numpy()
y_train_cc = train_y_cc.to_numpy()
y_val_cc = val_y_cc.to_numpy()
y_train_ft = train_y_ft.to_numpy()
y_val_ft = val_y_ft.to_numpy()<compute_test_metric>
all_data.drop(['rankPoints','killPoints','winPoints'], axis=1, inplace=True )
def root_mean_squared_log_error(y_true, y_pred):
return K.sqrt(K.mean(K.square(K.log(y_pred + 1)- K.log(y_true + 1))))<choose_model_class>
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 )
def LSTM_model(n_1, input_dim, output_dim):
model = Sequential()
model.add(LSTM(n_1,input_shape=(1, input_dim), activation='relu'))
model.add(Dense(output_dim, activation='relu'))
model.compile(loss=root_mean_squared_log_error, optimizer='adam')
 return model<choose_model_class>
all_data['_totalDistance'] = all_data['rideDistance'] + all_data["walkDistance"] + all_data["swimDistance"]
all_data['_healthItems'] = all_data['heals'] + all_data['boosts']
all_data['_headshotKillRate'] = all_data['headshotKills'] / all_data['kills']
all_data['_killsOverWalkDistance'] = all_data['kills'] / all_data['walkDistance']
all_data['_killsOverDistance'] = all_data['kills'] / all_data['_totalDistance']
all_data['_killPlacePerc'] = all_data['killPlace'] / all_data['maxPlace']
fillInf(all_data, 0 )
K.clear_session()
model_cc = LSTM_model(4, X_train_cc.shape[-1], y_train_cc.shape[-1])
 model_ft = LSTM_model(4, X_train_ft.shape[-1], y_train_ft.shape[-1])
 early_stop_cc = EarlyStopping(monitor='val_loss', patience=5, verbose=0, mode='min')
 early_stop_ft = EarlyStopping(monitor='val_loss', patience=5, verbose=0, mode='min' )<train_model>
agg_col = list(all_data.columns)
 exclude_agg_col = ['Id','matchId','groupId','matchType','matchDuration','maxPlace','numGroups','winPlacePerc']
for c in exclude_agg_col:
agg_col.remove(c)
 print(agg_col )
print('Start model training')
 start_time = time.time()
history_cc = model_cc.fit(X_train_cc, y_train_cc, batch_size = 16, epochs = 100,validation_data =(X_val_cc, y_val_cc), verbose = 2, callbacks=[early_stop_cc])
 model_cc.save("model_cc.h5")
 print('Time spent for model training is {} minutes'.format(round(( time.time() -start_time)/60,1)) )<train_model>
def grouping(df):
group = df.groupby(['matchId','groupId','matchType'])
 gCount = group.size().to_frame('players')
 gMean = group.mean()
gMax = group[agg_col].max().rename(columns=lambda s: '_max.' + s)
 gMin = group[agg_col].min().rename(columns=lambda s: '_min.' + s)
 return pd.concat([gCount, gMean, gMax, gMin], axis=1 ).reset_index()
all_data = grouping(all_data)
 all_data = reduce_mem_usage(all_data)
 print(all_data.shape )
print('Start model training')
 start_time = time.time()
history_ft = model_ft.fit(X_train_ft, y_train_ft, batch_size = 16, epochs = 8,validation_data =(X_val_ft, y_val_ft), verbose = 2, callbacks=[early_stop_ft])
 model_ft.save("model_ft.h5")
 print('Time spent for model training is {} minutes'.format(round(( time.time() -start_time)/60,1)) )<predict_on_test>

 group = all_data[np.r_[cols,['groupId']]].groupby(['matchId','groupId'])
 gcol = ['kills','teamKills','revives','damageDealt','walkDistance']
gSum = group[gcol].sum().rename(columns=lambda s: '_gsum.' + s)
 all_data = reduce_mem_usage(pd.merge(all_data, gSum.reset_index()))
for c in gcol:
all_data['_perc.gmean_gmax.' + c] = all_data[c] / all_data['_max.' + c]
del gSum
gc.collect()
print(all_data.shape )
yhat_val_cc = model_cc.predict(X_val_cc)
 print(yhat_val_cc[50:70] )<predict_on_test>

 match = all_data[cols].groupby('matchId')
 matchRank = match.rank(pct=True ).rename(columns=lambda s: '_rank.' + s)
 all_data = reduce_mem_usage(pd.concat([all_data, matchRank], axis=1))
del matchRank
gc.collect()
print(all_data.shape )
yhat_val_ft = model_ft.predict(X_val_ft)
 print(yhat_val_ft[60:70] )<create_dataframe>
cols = np.r_[agg_col,['matchId','players']]
match = all_data[cols].groupby('matchId' )
submission = pd.DataFrame()
submission['ForecastId'] = test_df['ForecastId']
submission['ConfirmedCases'] = test_df['cc_cases']
submission['Fatalities'] = test_df['ft_cases']<save_to_csv>

 matchSum = match.sum().rename(columns=lambda s: '_msum.' + s ).reset_index()
all_data = reduce_mem_usage(pd.merge(all_data, matchSum))
del matchSum
gc.collect()
all_data['_enemy.sum.players'] = all_data['_msum.players'] - all_data['players']
all_data['_enemy.kills'] =(all_data['_msum.kills'] - all_data['_gsum.kills'])/ all_data['_enemy.sum.players']
all_data['_enemy.damageDealt'] =(all_data['_msum.damageDealt'] - all_data['_gsum.damageDealt'])/ all_data['_enemy.sum.players']
for c in agg_col:
all_data['_perc.gmax_msum.' + c] = all_data['_max.' + c] / all_data['_msum.' + c]
if c in gcol:
all_data['_perc.gsum_msum.' + c] = all_data['_gsum.' + c] / all_data['_msum.' + c]
fillInf(all_data, 0)
 print(all_data.shape )
submission.to_csv("submission.csv",index=False )<set_options>

 matchMean = match.max().rename(columns=lambda s: '_mmax.' + s ).reset_index()
all_data = reduce_mem_usage(pd.merge(all_data, matchMean))
del matchMean
gc.collect()
for c in agg_col:
all_data['_perc.gmax_mmax.' + c] = all_data['_max.' + c] / all_data['_mmax.' + c]
all_data.drop(['_mmax.' + c], axis=1, inplace=True)
 fillInf(all_data, 0)
 print(all_data.shape )
%matplotlib inline
if not sys.warnoptions:
warnings.simplefilter("ignore" )<load_from_csv>
killMinorRank = all_data[['matchId','_min.kills','_max.killPlace']].copy()
group = killMinorRank.groupby(['matchId','_min.kills'])
 killMinorRank['_rank.minor.maxKillPlace'] = group.rank().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().values
all_data = pd.merge(all_data, killMinorRank)
 del killMinorRank
gc.collect()
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<load_from_csv>
mapper = lambda x: 'solo' if('solo' in x)else 'duo' if('duo' in x)or('crash' in x)else 'squad'
all_data['matchTypeCat'] = all_data['matchType'].map(mapper)
 print(all_data['matchTypeCat'].value_counts() )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<drop_column>
constant_column = [col for col in all_data.columns if all_data[col].nunique() == 1]
print('drop columns:', constant_column)
 all_data.drop(constant_column, axis=1, inplace=True )
train = train.drop(train[(train['GrLivArea']>4500)&(train['SalePrice']<300000)].index )<feature_engineering>
null_cnt = all_data.isnull().sum().sort_values()
print(null_cnt[null_cnt > 0])
 all_data.head()
train['SalePrice'] = np.log1p(train['SalePrice'] )<prepare_x_and_y>
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]
train_corr = train
train_features = train.drop('SalePrice', axis = 1)
 y = train.SalePrice.reset_index(drop=True)
 test_features = test
features = pd.concat([train_features, test_features] )<sort_values>
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()
X_train.drop(['matchId','groupId'], axis=1, inplace=True)
 X_test.drop(['matchId','groupId'], axis=1, inplace=True)
 print(X_train.shape, X_test.shape )
def missing_vals(df):
total = df.isnull().sum().sort_values(ascending = False)
 percent =(total/len(df)*100)
 missing = percent[percent>0]
return pd.DataFrame(missing, columns = ['Percent'])
 missing_vals(features )<categorify>
print(pd.DataFrame([[val for val in dir() ], [sys.getsizeof(eval(val)) for val in dir() ]],
index=['name','size'] ).T.sort_values('size', ascending=False ).reset_index(drop=True)[:10] )
features = features.dropna(thresh = len(features)*0.75, axis=1)
 for i in 'MSSubClass YrSold MoSold GarageYrBlt'.split() :
features[i] = features[i].apply(lambda x: str(x))
features['LotFrontage'] = features.groupby('Neighborhood')['LotFrontage'].transform(lambda x: x.fillna(x.median()))
for i in 'Utilities Functional Electrical KitchenQual MSZoning'.split() :
features[i] = features[i].fillna(features[i].mode() [0])
 for i in 'GarageType GarageFinish GarageQual GarageCond'.split() :
features[i] = features[i].fillna('None')
 for i in 'GarageYrBlt GarageArea GarageCars'.split() :
features[i] = features[i].fillna(0)
 for i in 'BsmtFinSF1 BsmtFinSF2 BsmtUnfSF TotalBsmtSF BsmtFullBath BsmtHalfBath'.split() :
features[i] = features[i].fillna(0)
 for i in 'BsmtQual BsmtCond BsmtExposure BsmtFinType1 BsmtFinType2'.split() :
features[i] = features[i].fillna('None')
 features["MasVnrType"] = features["MasVnrType"].fillna("None")
 features["MasVnrArea"] = features["MasVnrArea"].fillna(0)
 for i in 'Exterior1st Exterior2nd SaleType'.split() :
features[i] = features[i].fillna(features[i].mode() [0])
 features['MSSubClass'] = features['MSSubClass'].fillna("None" )<count_missing_values>
params={'learning_rate': 0.05,
'objective':'mae',
'metric':'mae',
'num_leaves': 31,
'verbose': 0,
'random_state':42,
'bagging_fraction': 0.7,
'feature_fraction': 0.7
}
mts = list()
fis = list()
pred = np.zeros(X_test.shape[0])
 for mt in X_train['matchTypeCat'].unique() :
idx = X_train[X_train['matchTypeCat'] == mt].index
reg = lgb.LGBMRegressor(**params, n_estimators=1000)
 reg.fit(X_train.loc[idx], Y_train.loc[idx])
 idx = X_test[X_test['matchTypeCat'] == mt].index
pred[idx] = reg.predict(X_test.loc[idx], num_iteration=reg.best_iteration_)
 mts.append(mt)
 fis.append(reg.feature_importances_ )
missing_vals(features )<feature_engineering>
X_test_grp['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)
 sub_match = X_test_grp[['matchId','_rank.winPlacePerc']].groupby(['matchId'])
 sub_group = group.count().reset_index() ['matchId'].to_frame()
X_test = pd.merge(X_test, sub_group )
features['TotSqrSF'] = features['TotalBsmtSF'] + features['1stFlrSF'] + features['2ndFlrSF']<feature_engineering>
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
feature_en = 'PoolArea Fireplaces GarageArea TotalBsmtSF 2ndFlrSF'.split()
for i in feature_en:
features['Has'+i] = features[i].apply(lambda x: 1 if x> 0 else 0 )<feature_engineering>
X_test.loc[~fullgroup, '_pred.winPlace'] = np.around(X_test.loc[~fullgroup, 'winPlacePerc'].values / gap)+ 1
X_test.loc[~fullgroup &(X_test['matchId'] == '000b598b79aa5e'),
['matchId','groupId','winPlacePerc','maxPlace','numGroups','_pred.winPlace','_rank.winPlacePerc']
].sort_values(['matchId','_pred.winPlace'] )
skewed_features = skewness.index
for feat in skewed_features:
features[feat] = boxcox1p(features[feat],0.5 )<create_dataframe>
_=
features_cat = features.copy()<categorify>
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
X_test['winPlacePerc'].describe()
features = pd.get_dummies(features, drop_first = True)
 features.shape<split>
test = pd.read_csv('.. /input/test_V2.csv')
 submission = pd.merge(test, X_test[['matchId','groupId','winPlacePerc']])
 submission = submission[['Id','winPlacePerc']]
submission.to_csv("submission.csv", index=False )