Kaggle competition: Indoor Location & Navigation
def create_model(num_columns):
model = tf.keras.Sequential([
tf.keras.layers.Input(num_columns),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.2),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(2048, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.5),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(1048, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.5),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(206, activation="sigmoid"))
])
 model.compile(optimizer=tfa.optimizers.Lookahead(tf.optimizers.Adam() , sync_period=10),
loss='binary_crossentropy', 
)
 return model<define_variables>
test_buildings = [
'5a0546857ecc773753327266',
'5c3c44b80379370013e0fd2b',
'5d27075f03f801723c2e360f',
'5d27096c03f801723c31e5e0',
'5d27097f03f801723c320d97',
'5d27099f03f801723c32511d',
'5d2709a003f801723c3251bf',
'5d2709b303f801723c327472',
'5d2709bb03f801723c32852c',
'5d2709c303f801723c3299ee',
'5d2709d403f801723c32bd39',
'5d2709e003f801723c32d896',
'5da138274db8ce0c98bbd3d2',
'5da1382d4db8ce0c98bbe92e',
'5da138314db8ce0c98bbf3a0',
'5da138364db8ce0c98bc00f1',
'5da1383b4db8ce0c98bc11ab',
'5da138754db8ce0c98bca82f',
'5da138764db8ce0c98bcaa46',
'5da1389e4db8ce0c98bd0547',
'5da138b74db8ce0c98bd4774',
'5da958dd46f8266d0737457b',
'5dbc1d84c1eb61796cf7c010',
'5dc8cea7659e181adb076a3f'
]
res = y_train.copy()
ss.loc[:, y_train.columns] = 0
res.loc[:, y_train.columns] = 0
control_mask = test_features['cp_type']!='ctl_vehicle'<split>
sub1 = split_col(pd.read_csv('.. /input/indoor-location-train-waypoints/6.771LB_submission.csv'))
sub2 = split_col(pd.read_csv('.. /input/indoor-location-train-waypoints/7.274LB_submission.csv'))
sub3 = split_col(pd.read_csv('.. /input/indoor-location-train-waypoints/7.518LB_submission_LSTM.csv'))
sub4 = split_col(pd.read_csv('.. /input/indoor-location-train-waypoints/7.661LB_LSTM_submission.csv'))
sub5 = split_col(pd.read_csv('.. /input/indoor-location-train-waypoints/7.745LB_submission.csv'))
sub = sub1.merge(sub2[['site_path_timestamp','floor','x','y']],
on=['site_path_timestamp','floor'],
how='left',
suffixes=('_s1','_s2')).copy()
sub['x'] = sub1['x']
sub['y'] = sub1['y']
sub = sub.merge(sub3[['site_path_timestamp','floor','x','y']],
on=['site_path_timestamp','floor'],
how='left',
suffixes=('','_s3')).copy()
sub = sub.merge(sub4[['site_path_timestamp','floor','x','y']],
on=['site_path_timestamp','floor'],
how='left',
suffixes=('','_s4')).copy()
sub = sub.merge(sub5[['site_path_timestamp','floor','x','y']],
on=['site_path_timestamp','floor'],
how='left',
suffixes=('','_s5')).copy()
sub['x'] = sub[['x_s1','x_s2','x_s3','x_s4','x_s5']].mean(axis=1)
 sub['y'] = sub[['y_s1','y_s2','y_s3','y_s4','y_s5']].mean(axis=1 )
for seed in range(NSEEDS):
mskf = MultilabelStratifiedKFold(n_splits=NFOLDS, random_state=seed, shuffle=True)
 for n,(tr, te)in enumerate(mskf.split(X_train, y_train)) :
print(f'Fold {n}')
 X_trn, X_val = X_train.iloc[tr,:], X_train.iloc[te,:]
y_trn, y_val = y_train.iloc[tr,:], y_train.iloc[te,:]
model = create_model(num_columns)
 checkpoint_path = f'repeat:{seed}_Fold:{n}.hdf5'
reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, verbose=1, epsilon=1e-4, mode='min')
 cb_checkpt = ModelCheckpoint(checkpoint_path, monitor = 'val_loss', verbose = 0, save_best_only = True,
save_weights_only = True, mode = 'min')
 model.fit(X_trn.values,
y_trn.values,
validation_data=(X_val.values, y_val.values),
epochs=35, batch_size=128,
callbacks=[reduce_lr_loss, cb_checkpt], verbose=2 
)
 model.load_weights(checkpoint_path)
 test_predict = model.predict(X_test.values)
 val_predict = model.predict(X_val.values)
 ss.loc[control_mask, y_train.columns] += test_predict
res.loc[te, y_train.columns] += val_predict
print('')
 ss.loc[:, y_train.columns] /=(NFOLDS * NSEEDS)
 res.loc[:, y_train.columns] /= NSEEDS<compute_test_metric>
train_waypoints = pd.read_csv('.. /input/indoor-location-train-waypoints/train_waypoints.csv')
 sub = sub_process(pd.read_csv('.. /input/indoor-location-post-processing/submission.csv'), train_waypoints)
 example_site = '5dbc1d84c1eb61796cf7c010'
example_floorNo = 'F3'
plot_preds(example_site, example_floorNo, sub,
train_waypoints, show_preds=False)
 plt.show()
def metric(y_true, y_pred):
metrics = []
for _target in y_train.columns:
metrics.append(log_loss(y_true.loc[:, _target], y_pred.loc[:, _target].astype(float), labels=[0,1]))
return np.mean(metrics )<compute_test_metric>
def add_xy(df):
df['xy'] = [(x, y)for x,y in zip(df['x'], df['y'])]
return df
def closest_point(point, points):

 return points[cdist([point], points ).argmin() ]
sub = add_xy(sub)
 train_waypoints = add_xy(train_waypoints)
 ds = []
for(site, myfloor), d in sub.groupby(['site','floor']):
true_floor_locs = train_waypoints.loc[(train_waypoints['floor'] == myfloor)& 
(train_waypoints['site'] == site)] \ 
.reset_index(drop=True)
 if len(true_floor_locs)== 0:
print(f'Skipping {site} {myfloor}')
 continue
d['matched_point'] = [closest_point(x, list(true_floor_locs['xy'])) for x in d['xy']]
d['x_'] = d['matched_point'].apply(lambda x: x[0])
 d['y_'] = d['matched_point'].apply(lambda x: x[1])
 ds.append(d)
 sub = pd.concat(ds )
print(f'OOF Metric: {metric(y_train, res)}' )<save_to_csv>
def snap_to_grid(sub, threshold):

 sub['_x_'] = sub['x']
sub['_y_'] = sub['y']
sub.loc[sub['dist'] < threshold, '_x_'] = sub.loc[sub['dist'] < threshold]['x_']
sub.loc[sub['dist'] < threshold, '_y_'] = sub.loc[sub['dist'] < threshold]['y_']
return sub.copy()
sub['dist'] = np.sqrt(( sub.x-sub.x_)**2 +(sub.y-sub.y_)**2)
 sub_pp = snap_to_grid(sub, threshold=5)
 sub_pp = sub_pp[['site_path_timestamp','floor','_x_','_y_','site','path','floorNo']] \ 
.rename(columns={'_x_':'x', '_y_':'y'} )
ss.to_csv('submission.csv', index=False )<set_options>
sub_pp[['site_path_timestamp','floor','x','y']] \ 
.to_csv('submission_snap_to_grid.csv', index=False )
warnings.filterwarnings('ignore' )<load_from_csv>
<define_variables><EOS>