Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<init_hyperparams>
warnings.filterwarnings('ignore')
 %matplotlib inline
py.init_notebook_mode(connected=True)
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
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>
debug = False
if debug == True:
df_train = pd.read_csv('.. /input/train_V2.csv', nrows=10000)
 df_test = pd.read_csv('.. /input/test_V2.csv')
 else:
df_train = pd.read_csv('.. /input/train_V2.csv')
 df_test = pd.read_csv('.. /input/test_V2.csv' )
batch_size=128
checkpointer = ModelCheckpoint(filepath='mnist.model.best.cnn.aug.hdf5',
verbose=1, save_best_only=True)
 hist = cnn_model.fit_generator(datagen.flow(X_train, Y_train, batch_size=batch_size),
epochs=10, validation_data=(X_val, Y_val), callbacks=[checkpointer],
verbose=2, shuffle=True, steps_per_epoch=X_train.shape[0] // batch_size )<load_pretrained>
df_train[df_train['groupId']=='4d4b580de459be']
cnn_model.load_weights('mnist.model.best.cnn.aug.hdf5' )<compute_test_metric>
len(df_train[df_train['matchId']=='a10357fd1a4a91'] )
score = cnn_model.evaluate(X_valid, Y_valid, verbose=0)
 accuracy = 100 * score[1]
print(f'Test Accuracy {accuracy:.4f}' )<choose_model_class>
df_train['roadKills'].value_counts()
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )<train_model>
df_train['teamKills'].value_counts()
checkpointer = ModelCheckpoint(filepath='mnist.model.best.cnn.aug.ann.hdf5',
verbose=1, save_best_only=True)
 hist = cnn_model.fit_generator(datagen.flow(X_train, Y_train, batch_size=batch_size),
epochs=20, validation_data=(X_val, Y_val), callbacks=[checkpointer, learning_rate_reduction],
verbose=2, shuffle=True, steps_per_epoch=X_train.shape[0] // batch_size)
<load_pretrained>
df_train['headshotKills'].value_counts()
cnn_model.load_weights('mnist.model.best.cnn.aug.ann.hdf5' )<compute_test_metric>
df_train['vehicleDestroys'].value_counts()
score = cnn_model.evaluate(X_valid, Y_valid, verbose=0)
 accuracy = 100 * score[1]
print(f'Test Accuracy {accuracy:.4f}' )<compute_test_metric>
df_train = df_train[df_train['Id']!='f70c74418bb064']
submit_result(cnn_model, X_test )<choose_model_class>
headshot = df_train[['kills','winPlacePerc','headshotKills']]
headshot['headshotrate'] = headshot['kills'] / headshot['headshotKills']
cnn_bn_model = Sequential()
cnn_bn_model.add(Conv2D(filters = 64, kernel_size =(5,5),padding = 'Same', activation ='relu', input_shape =(28,28,1)))
 cnn_bn_model.add(BatchNormalization())
 cnn_bn_model.add(Conv2D(filters = 64, kernel_size =(5,5),padding = 'Same', activation ='relu'))
cnn_bn_model.add(BatchNormalization())
 cnn_bn_model.add(MaxPooling2D(pool_size=(2,2)))
 cnn_bn_model.add(Dropout(0.25))
cnn_bn_model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
cnn_bn_model.add(BatchNormalization())
 cnn_bn_model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
cnn_bn_model.add(BatchNormalization())
 cnn_bn_model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
 cnn_bn_model.add(Dropout(0.25))
cnn_bn_model.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'Same', activation ='relu'))
cnn_bn_model.add(BatchNormalization())
 cnn_bn_model.add(Dropout(0.25))
cnn_bn_model.add(Flatten())
 cnn_bn_model.add(Dense(256, activation = "relu"))
cnn_bn_model.add(BatchNormalization())
 cnn_bn_model.add(Dropout(0.25))
cnn_bn_model.add(Dense(10, activation = "softmax"))
cnn_bn_model.summary()<choose_model_class>
del headshot
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )<choose_model_class>
df_train['headshotrate'] = df_train['kills']/df_train['headshotKills']
df_test['headshotrate'] = df_test['kills']/df_test['headshotKills']
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 cnn_bn_model.compile(optimizer=optimizer, loss="categorical_crossentropy", metrics=['accuracy'] )<train_model>
killStreak = df_train[['kills','winPlacePerc','killStreaks']]
killStreak['killStreakrate'] = killStreak['killStreaks']/killStreak['kills']
killStreak.corr()
checkpointer = ModelCheckpoint(filepath='final.model.hdf5',
verbose=1, save_best_only=True)
 hist = cnn_bn_model.fit_generator(datagen.flow(X_train, Y_train, batch_size=batch_size),
epochs=20, validation_data=(X_val, Y_val), callbacks=[checkpointer, learning_rate_reduction],
verbose=2, shuffle=True, steps_per_epoch=X_train.shape[0] // batch_size)
<load_pretrained>
del healthitems
cnn_bn_model.load_weights('final.model.hdf5' )<compute_test_metric>
kills = df_train[['assists','winPlacePerc','kills']]
kills['kills_assists'] =(kills['kills'] + kills['assists'])
 kills.corr()
score = cnn_bn_model.evaluate(X_valid, Y_valid, verbose=0)
 accuracy = 100 * score[1]
print(f'Test Accuracy {accuracy:.4f}' )<compute_test_metric>
del df_train,df_test;
gc.collect()
submit_result(cnn_bn_model, X_test )<define_variables>
def feature_engineering(is_train=True,debug=True):
test_idx = None
if is_train:
print("processing train.csv")
 if debug == True:
df = pd.read_csv('.. /input/train_V2.csv', nrows=10000)
 else:
df = pd.read_csv('.. /input/train_V2.csv')
 df = df[df['maxPlace'] > 1]
else:
print("processing test.csv")
 df = pd.read_csv('.. /input/test_V2.csv')
 test_idx = df.Id
print("remove some columns")
 target = 'winPlacePerc'
features = list(df.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchType")
 y = None
if is_train:
print("get target")
 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'])
 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'])
 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'])
 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'])
 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'])
 df_out.drop(["matchId", "groupId"], axis=1, inplace=True)
 X = df_out
feature_names = list(df_out.columns)
 del df, df_out, agg, agg_rank
gc.collect()
return X, y, feature_names, test_idx
x_train, y_train, train_columns, _ = feature_engineering(True,False)
 x_test, _, _ , test_idx = feature_engineering(False,True )
sub_path = ".. /input/ieeesubmissions"
all_files = os.listdir(sub_path)
 all_files<load_from_csv>
x_train['headshotrate'] = x_train['kills']/x_train['headshotKills']
x_test['headshotrate'] = x_test['kills']/x_test['headshotKills']
x_train['killStreakrate'] = x_train['killStreaks']/x_train['kills']
x_test['killStreakrate'] = x_test['killStreaks']/x_test['kills']
x_train['healthitems'] = x_train['heals'] + x_train['boosts']
x_test['healthitems'] = x_test['heals'] + x_test['boosts']
del x_train['heals'];del x_test['heals']
train_columns.append('headshotrate')
 train_columns.append('killStreakrate')
 train_columns.append('healthitems')
 train_columns.remove('heals' )
outs = [pd.read_csv(os.path.join(sub_path, f), index_col=0)for f in all_files]
concat_sub = pd.concat(outs, axis=1)
 cols = list(map(lambda x: "ieee" + str(x), range(len(concat_sub.columns))))
concat_sub.columns = cols
concat_sub.reset_index(inplace=True)
 concat_sub.head()
ncol = concat_sub.shape[1]<feature_engineering>
x_train = reduce_mem_usage(x_train)
 x_test = reduce_mem_usage(x_test )
concat_sub['ieee_max'] = concat_sub.iloc[:, 1:ncol].max(axis=1)
 concat_sub['ieee_min'] = concat_sub.iloc[:, 1:ncol].min(axis=1)
 concat_sub['ieee_mean'] = concat_sub.iloc[:, 1:ncol].mean(axis=1)
 concat_sub['ieee_median'] = concat_sub.iloc[:, 1:ncol].median(axis=1 )<define_variables>
warnings.filterwarnings("ignore")
cutoff_lo = 0.90
cutoff_hi = 0.30<save_to_csv>
folds = KFold(n_splits=3,random_state=6)
 oof_preds = np.zeros(x_train.shape[0])
 sub_preds = np.zeros(x_test.shape[0])
 start = time.time()
valid_score = 0
feature_importance_df = pd.DataFrame()
for n_fold,(trn_idx, val_idx)in enumerate(folds.split(x_train, y_train)) :
trn_x, trn_y = x_train.iloc[trn_idx], y_train[trn_idx]
val_x, val_y = x_train.iloc[val_idx], y_train[val_idx]
train_data = lgb.Dataset(data=trn_x, label=trn_y)
 valid_data = lgb.Dataset(data=val_x, label=val_y)
 params = {"objective" : "regression", "metric" : "mae", 'n_estimators':15000, 'early_stopping_rounds':100,
"num_leaves" : 31, "learning_rate" : 0.05, "bagging_fraction" : 0.9,
"bagging_seed" : 0, "num_threads" : 4,"colsample_bytree" : 0.7
}
lgb_model = lgb.train(params, train_data, valid_sets=[train_data, valid_data], verbose_eval=1000)
 oof_preds[val_idx] = lgb_model.predict(val_x, num_iteration=lgb_model.best_iteration)
 oof_preds[oof_preds>1] = 1
oof_preds[oof_preds<0] = 0
sub_pred = lgb_model.predict(x_test, num_iteration=lgb_model.best_iteration)
 sub_pred[sub_pred>1] = 1
sub_pred[sub_pred<0] = 0
sub_preds += sub_pred/ folds.n_splits
fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = train_columns
fold_importance_df["importance"] = lgb_model.feature_importance()
fold_importance_df["fold"] = n_fold + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 gc.collect()
end = time.time()
print("Take Time :",(end-start))
concat_sub['isFraud'] = concat_sub['ieee_median']
concat_sub[['TransactionID', 'isFraud']].to_csv('ieee.csv',
index=False, float_format='%.6f' )<set_options>
df_test = pd.read_csv('.. /input/' + 'test_V2.csv')
 pred = sub_preds
print("fix winPlacePerc")
 for i in range(len(df_test)) :
winPlacePerc = pred[i]
maxPlace = int(df_test.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
pred[i] = winPlacePerc
if(i + 1)% 100000 == 0:
print(i, flush=True, end=" ")
 df_test['winPlacePerc'] = pred
submission = df_test[['Id', 'winPlacePerc']]
submission.to_csv('submission.csv', index=False )