Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
train_history = model.fit(
 train_input, train_labels,
validation_split=0.2,
epochs=1,
batch_size=16 
)
 model.save('model.h5' )<choose_model_class>
def toTapleList(list1,list2):
return list(itertools.product(list1,list2))
cls_layer_model = Model(model.input, outputs=model.get_layer('tf_op_layer_strided_slice' ).output )<predict_on_test>
sns.set_style('darkgrid')
 sns.set_palette('bone')
 pd.options.display.float_format = '{:,.3f}'.format
print(os.listdir(".. /input"))
X_train = cls_layer_model.predict(train_input)
 X_test = cls_layer_model.predict(test_input)
 y_train = train.target.values<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 )
model_svm = SVC(C=15, kernel='rbf',gamma=0.001)
 model_svm.fit(X_train,y_train )<predict_on_test>
null_cnt = train.isnull().sum().sort_values()
print('null count:', null_cnt[null_cnt > 0])
 train.dropna(inplace=True )
test_pred = model_svm.predict(X_test )<save_to_csv>
for c in ['Id','groupId','matchId']:
print(f'unique [{c}] count:', train[c].nunique() )
submission['target'] = test_pred.round().astype(int)
 submission.to_csv('submission.csv', index=False )<load_from_csv>
for q in ['numGroups == maxPlace','numGroups != maxPlace']:
print(q, ':', len(train.query(q)) )
sample_submission = pd.read_csv(".. /input/nlp-getting-started/sample_submission.csv")
 test = pd.read_csv(".. /input/nlp-getting-started/test.csv")
 train = pd.read_csv(".. /input/nlp-getting-started/train.csv" )<import_modules>
print(group['players in group'].nlargest(5))
del match,group
import PIL
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import keras
from matplotlib import pyplot
from sklearn import preprocessing<define_variables>
( train.groupby('matchId')['matchDuration'].nunique() > 1 ).any()
run_model1 = False
run_model2 = False
run_model3 = False
run_model_adv = True<load_from_csv>
train = pd.read_csv('.. /input/train.csv', delimiter=',')
 test = pd.read_csv('.. /input/test.csv', delimiter=',' )<categorify>
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() )
train_size = train.shape[0]
test_size = test.shape[0]
X_train = train.iloc[:, 1:].values.astype('uint8')
 Y_train = train.iloc[:, 0]
X_test = test.iloc[:, :].values.astype('uint8')
 img_dimension = np.int32(np.sqrt(X_train.shape[1]))
img_rows, img_cols = img_dimension, img_dimension
nb_of_color_channels = 1
if(keras.backend.image_dim_ordering() =="th"):
X_train = X_train.reshape(train.shape[0], nb_of_color_channels, img_rows, img_cols)
 X_test = X_test.reshape(test.shape[0], nb_of_color_channels, img_rows, img_cols)
 in_shape =(nb_of_color_channels, img_rows, img_cols)
 else:
X_train = X_train.reshape(train.shape[0], img_rows, img_cols, nb_of_color_channels)
 X_test = X_test.reshape(test.shape[0], img_rows, img_cols, nb_of_color_channels)
 in_shape =(img_rows, img_cols, nb_of_color_channels)
 print('Data Information
')
 print('Training set size: {}
Testing set size: {}'.format(train_size, test_size))
print('Image dimension: {0}*{0}'.format(img_dimension)) 
<data_type_conversions>
pd.concat([train[train['winPlacePerc'] == 1].head(5),
train[train['winPlacePerc'] == 0].head(5)],
keys=['winPlacePerc_1', 'winPlacePerc_0'] )
X_train = X_train.astype('float32')
 X_test = X_test.astype('float32')
 X_train_nor = X_train / 255
X_test_nor= X_test / 255<categorify>
grpSolo.nlargest(5, 'kills' )
oh_encoder = preprocessing.OneHotEncoder(categories='auto')
 oh_encoder.fit(Y_train.values.reshape(-1,1))
Y_train_oh = oh_encoder.transform(Y_train.values.reshape(-1,1)).toarray()<categorify>
grpTeam.nlargest(5, 'kills' )
print('One-hot:')
 print(Y_train_oh[:5])
 print('
Label:')
 print(Y_train[:5] )<categorify>
del grpSolo, grpTeam
to_categorical(Y_train, Y_train.unique().shape[0])[:5] 
<import_modules>
def printMatchStats(matchIds):
for mid in matchIds:
subMatch = train[train['matchId'] == mid]
print('matchType:', subMatch['matchType'].values[0])
 grp1st = subMatch[subMatch['winPlacePerc'] == 1]
grpOther = subMatch[subMatch['winPlacePerc'] != 1]
print('players'.ljust(10), ' total:{:>3} 1st:{:>3} other:{:>3}'.format(len(subMatch), len(grp1st), len(grpOther)))
 for c in ['kills','teamKills','roadKills','DBNOs','revives','assists']:
print(c.ljust(10), ' total:{:>3} 1st:{:>3} other:{:>3}'.format(subMatch[c].sum() , grp1st[c].sum() , grpOther[c].sum()))
print('-' * 30 )
from keras.layers import Activation,Dropout,Dense,Conv2D,AveragePooling2D,Flatten,ZeroPadding2D,MaxPooling2D
from keras.models import Sequential
from keras import optimizers
from keras.callbacks import ReduceLROnPlateau<choose_model_class>
sampleMid = train['matchId'].unique() [0:5]
printMatchStats(sampleMid )
def build_lenet5(model, input_shape=X_train.shape[1:], dropout=0):
S = [1,2,1,2,1]
N_input = [28,28,14,10,5]
P = [2,0,0,0,0]
N = [28,14,10,5,1]
F = [i[0] + 2*i[1] - i[3]*(i[2] - 1)for i in zip(N_input, P, N, S)]
model.add(Conv2D(filters=6, kernel_size=(F[0],F[0]), padding='same', strides=S[0],
activation='relu', input_shape=input_shape))
model.add(MaxPooling2D(pool_size=F[1], strides=S[1]))
model.add(Conv2D(filters=16, kernel_size=(F[2],F[2]), padding='valid', strides=S[2],
activation='relu'))
model.add(MaxPooling2D(pool_size=F[3], strides=S[3]))
model.add(Conv2D(filters=120, kernel_size=(F[4],F[4]), padding='valid', strides=S[4],
activation='relu'))
model.add(Dropout(dropout))
model.add(Flatten())
 model.add(Dense(84, activation='relu'))
model.add(Dense(10, activation='softmax'))
if __name__ == '__main__' and run_model1:
model = Sequential()
build_lenet5(model, input_shape=X_train.shape[1:], dropout=0)
 model.summary() 
<train_model>
match = train.groupby(['matchId'])['Id'].count()
fullplayer = match[match == 100].reset_index()
sampleMid = fullplayer['matchId'][0:5]
printMatchStats(sampleMid )
hist_dict = {}
if __name__ == '__main__' and run_model1:
adam = optimizers.Adam()
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer=adam)
 hist_dict['run_model1'] = model.fit(X_train, Y_train_oh, batch_size=64, epochs=20,
shuffle=True, validation_split=0.2, verbose=2)
<save_to_csv>
all_data = train.append(test, sort=False ).reset_index(drop=True)
 del train, test
gc.collect()
def model_predict(model):
print("Generating test predictions...")
 predictions = model.predict_classes(X_test, verbose=1)
 print("OK.")
 return predictions
def model_predict_val(model, set_check):
print("Generating set predictions...")
 predictions = model.predict_classes(set_check, verbose=1)
 print("OK.")
 return predictions
def write_preds(preds, filename):
pd.DataFrame({"ImageId": list(range(1,len(preds)+1)) , "Label": preds} ).to_csv(filename, index=False, header=True)
 if __name__ == '__main__' and run_model1:
predictions = model_predict(model)
 print(predictions[:5])
 write_preds(predictions, "keras-lenet5-basic.csv")
<train_model>
match = all_data.groupby('matchId')
 all_data['killPlacePerc'] = match['kills'].rank(pct=True ).values
all_data['walkDistancePerc'] = match['walkDistance'].rank(pct=True ).values
if __name__ == '__main__' and run_model2:
model = Sequential()
build_lenet5(model, input_shape=X_train.shape[1:], dropout=0.3)
 model.summary()
adam = optimizers.Adam()
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer=adam)
 hist_dict['run_model2'] = model.fit(X_train, Y_train_oh, batch_size=64, epochs=20, shuffle=True, validation_split=0.2, verbose=2 )<save_to_csv>
all_data['_totalDistance'] = all_data['rideDistance'] + all_data['walkDistance'] + all_data['swimDistance']
if __name__ == '__main__' and run_model2:
predictions = model_predict(model)
 print(predictions[:5])
 write_preds(predictions, "keras-lenet5-basic-droupout.csv" )<init_hyperparams>
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 )
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=15,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train )<train_model>
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']
fillInf(all_data, 0 )
for x_batch, y_batch in datagen.flow(X_train, Y_train_oh, batch_size=9, shuffle = False):
print(x_batch.shape)
 print(y_batch.shape)
 break<split>
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 )
if __name__ == '__main__' and run_model3:
X_train_s, X_val, Y_train_s, Y_val = train_test_split(X_train, Y_train_oh, test_size=0.13, random_state=42)
 model = Sequential()
build_lenet5(model, input_shape=X_train_s.shape[1:], dropout=0.15)
 model.summary()
adam = optimizers.Adam()
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer=adam)
 epochs = 45
batch_size = 72
Train_gen_batch = datagen.flow(X_train_s, Y_train_s, batch_size=batch_size)
 datagen_no_aug = ImageDataGenerator()
Val_gen_batch = datagen_no_aug.flow(X_val, Y_val, batch_size=batch_size)
 hist_dict['run_model3'] = model.fit_generator(Train_gen_batch, epochs = epochs, verbose = 2,
steps_per_epoch = X_train.shape[0] // batch_size,
validation_data = Val_gen_batch,
validation_steps = X_val.shape[0] // batch_size,
callbacks=[learning_rate_reduction])
<save_to_csv>
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)
 print(agg_col)
 sum_col = ['kills','killPlace','damageDealt','walkDistance','_healthItems']
if __name__ == '__main__' and run_model3:
predictions = model_predict(model)
 print(predictions[:5])
 write_preds(predictions, "keras-lenet5-aug.csv")
<save_to_csv>

 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')
 match_data = reduce_mem_usage(match_data)
 del minKills, nKills
print(match_data.shape )
<choose_model_class>
def build_net_advanced(model, input_shape=X_train.shape[1:], dropout=0.25):
model.add(Conv2D(filters=32, kernel_size=(5,5), padding='same', strides=1,
activation='relu', input_shape=input_shape))
model.add(Conv2D(filters=32, kernel_size=(5,5), padding='valid', strides=2,
activation='relu'))
model.add(MaxPooling2D(pool_size=(3,3), strides=1))
model.add(Dropout(dropout))
model.add(Conv2D(filters=64, kernel_size=(3,3), padding='same', strides=1,
activation='relu'))
model.add(Conv2D(filters=64, kernel_size=(3,3), padding='valid', strides=2,
activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=1))
model.add(Dropout(dropout))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(Dropout(dropout))
model.add(Dense(128, activation='relu'))
model.add(Dropout(dropout))
model.add(Dense(10, activation='softmax'))<split>

 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)
 print(all_data.shape )
if __name__ == '__main__' and run_model_adv:
X_train_s, X_val, Y_train_s, Y_val = train_test_split(X_train, Y_train_oh, test_size=0.15, random_state=42)
 model = Sequential()
build_net_advanced(model, input_shape=X_train_s.shape[1:], dropout=0.3)
 model.summary()
adam = optimizers.Adam()
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer=adam)
 epochs = 35
batch_size = 84
Train_gen_batch = datagen.flow(X_train_s, Y_train_s, batch_size=batch_size)
 datagen_no_aug = ImageDataGenerator()
Val_gen_batch = datagen_no_aug.flow(X_val, Y_val, batch_size=batch_size)
 hist_dict['run_model_adv'] = model.fit_generator(Train_gen_batch, epochs = epochs, verbose = 2,
steps_per_epoch = X_train.shape[0] // batch_size,
validation_data = Val_gen_batch,
validation_steps = X_val.shape[0] // batch_size,
callbacks=[learning_rate_reduction] )<save_to_csv>

 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()
print(all_data.shape )
if __name__ == '__main__' and run_model_adv:
predictions = model_predict(model)
 print(predictions[:5])
 write_preds(predictions, "keras-adv-net.csv")
<split>

 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()
_, X_val_check, _, Y_val_check = train_test_split(X_train, Y_train, test_size=0.1, random_state=1)
 Ypred_val_check = model_predict_val(model, set_check=X_val_check)
<compute_test_metric>
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 )
cm = confusion_matrix(Y_val_check.values, Ypred_val_check)
 cm<feature_engineering>

 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))
X_val_check[Ypred_val_check != Y_val_check.values].shape[0] / X_val_check.shape[0]<split>
null_cnt = all_data.isnull().sum().sort_values()
print(null_cnt[null_cnt > 0])
display_digits(dim =(2,3),
X = X_val_check[Ypred_val_check != Y_val_check.values],
Y_true = Y_val_check.values[Ypred_val_check != Y_val_check.values],
pred = Ypred_val_check[Ypred_val_check != Y_val_check.values] )<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)
 all_data.head()
sns.set()
%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)
 print(X_train.shape, X_test.shape )
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv' )<count_missing_values>
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] )
train_df.isnull().sum()<count_missing_values>
params={'learning_rate': 0.1,
'objective':'mae',
'metric':'mae',
'num_leaves': 31,
'verbose': 1,
'random_state':42,
'bagging_fraction': 0.7,
'feature_fraction': 0.7
}
reg = lgb.LGBMRegressor(**params, n_estimators=5000)
 reg.fit(X_train, Y_train)
 pred = reg.predict(X_test, num_iteration=reg.best_iteration_ )
test_df.isnull().sum()<count_values>
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)
 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 )
train_df['Survived'].value_counts()<sort_values>
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 )
train_df[["Sex", "Survived"]].groupby(['Sex'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<sort_values>
_=
train_df[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False ).mean().sort_values(by='Survived', ascending=False )<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
X_test['winPlacePerc'].describe()
<sort_values><EOS> 
test = pd.read_csv('.. /input/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 )