Kaggle competition: Airbnb New User Bookings
<SOS> metric: NDCG@{K} Kaggle data source: airbnb-recruiting-new-user-bookings<normalization>
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
import xgboost as xgb
import operator
@tf.function
def _wav_to_spec(x):
mel_power = cfg['data_params']['mel_power']
stfts = tf.signal.stft(x["audio_wav"], frame_length=2048, frame_step=512, fft_length=2048)
 spectrograms = tf.abs(stfts)** mel_power
num_spectrogram_bins = stfts.shape[-1]
lower_edge_hertz, upper_edge_hertz, num_mel_bins = FMIN, FMAX, N_MEL
linear_to_mel_weight_matrix = tf.signal.linear_to_mel_weight_matrix(
 num_mel_bins, num_spectrogram_bins, SR, lower_edge_hertz,
upper_edge_hertz)
 mel_spectrograms = tf.tensordot(
 spectrograms, linear_to_mel_weight_matrix, 1)
 mel_spectrograms.set_shape(spectrograms.shape[:-1].concatenate(
 linear_to_mel_weight_matrix.shape[-1:]))
log_mel_spectrograms = tf.math.log(mel_spectrograms + 1e-6)
 y = {
'audio_spec': tf.transpose(log_mel_spectrograms),
}
y.update(x)
 return y
spec_dataset = parsed_dataset.filter(_filtTP ).map(_cut_wav ).map(_wav_to_spec )<categorify>
df_train = pd.read_csv('.. /input/train_users_2.csv')
 df_test = pd.read_csv('.. /input/test_users.csv')
 labels = df_train['country_destination'].values
df_train = df_train.drop(['country_destination'], axis=1)
 id_test = df_test['id']
piv_train = df_train.shape[0]
@tf.function
def _create_annot(x):
targ = tf.one_hot(x["species_id"], CLASS_N, on_value=x["is_tp"], off_value=0)
 return {
'input': x["audio_spec"],
'target': tf.cast(targ, tf.float32)
 }
annot_dataset = spec_dataset.map(_create_annot )<normalization>
df_all = pd.concat(( df_train, df_test), axis=0, ignore_index=True)
 df_all = df_all.drop(['id', 'date_first_booking'], axis=1)
 df_all = df_all.fillna(-1 )
def _preprocess_img(x, training=False, test=False):
image = tf.expand_dims(x, axis=-1)
 image = tf.image.resize(image, [HEIGHT, WIDTH])
 image = tf.image.per_image_standardization(image)
 @tf.function
def _specaugment(image):
ERASE_TIME = 50
ERASE_MEL = 16
image = tf.expand_dims(image, axis=0)
 xoff = tf.random.uniform([2], minval=ERASE_TIME//2, maxval=WIDTH-ERASE_TIME//2, dtype=tf.int32)
 xsize = tf.random.uniform([2], minval=ERASE_TIME//2, maxval=ERASE_TIME, dtype=tf.int32)
 yoff = tf.random.uniform([2], minval=ERASE_MEL//2, maxval=HEIGHT-ERASE_MEL//2, dtype=tf.int32)
 ysize = tf.random.uniform([2], minval=ERASE_MEL//2, maxval=ERASE_MEL, dtype=tf.int32)
 image = tf.squeeze(image, axis=0)
 return image
if training:
gau = tf.keras.layers.GaussianNoise(0.3)
 image = tf.cond(tf.random.uniform([])< 0.5, lambda: gau(image, training=True), lambda: image)
 image = tf.image.random_brightness(image, 0.2)
 image = tf.image.random_flip_left_right(image)
 image = tf.cond(tf.random.uniform([])< 0.5, lambda: _specaugment(image), lambda: image)
 if test:
image = tf.cond(tf.random.uniform([])< 0.5, lambda: _specaugment(image), lambda: image)
 image =(image - tf.reduce_min(image)) /(tf.reduce_max(image)- tf.reduce_min(image)) * 255.0
image = tf.image.grayscale_to_rgb(image)
 image = cfg['model_params']['arch_preprocess'](image)
 return image
@tf.function
def _preprocess(x):
image = _preprocess_img(x['input'], training=True, test=False)
 return(image, x["target"])
 @tf.function
def _preprocess_val(x):
image = _preprocess_img(x['input'], training=False, test=False)
 return(image, x["target"])
 @tf.function
def _preprocess_test(x):
image = _preprocess_img(x['audio_spec'], training=False, test=True)
 return(image, x["recording_id"] )<choose_model_class>
dac = np.vstack(
 df_all.date_account_created.astype(str ).apply(
 lambda x: list(map(int, x.split('-')))
 ).values 
)
 df_all['dac_year'] = dac[:,0]
df_all['dac_month'] = dac[:,1]
df_all['dac_day'] = dac[:,2]
df_all = df_all.drop(['date_account_created'], axis=1 )
def create_model() :
def Classifier(shape_):
backbone = cfg['model_params']['arch'](( shape_), include_top=False, weights='imagenet')
 def cbr(x, out_layer, kernel, stride, dilation):
x = Conv2D(out_layer, kernel_size=kernel, dilation_rate=dilation, strides=stride, padding="same" )(x)
 x = BatchNormalization()(x)
 x = Activation("relu" )(x)
 return x
def wave_block(x, filters, kernel_size, n):
dilation_rates = [2**i for i in range(n)]
x = Conv2D(filters = filters,
kernel_size = 1,
padding = 'same' )(x)
 res_x = x
for dilation_rate in dilation_rates:
tanh_out = Conv2D(filters = filters,
kernel_size = kernel_size,
padding = 'same',
activation = 'tanh',
dilation_rate = dilation_rate )(x)
 sigm_out = Conv2D(filters = filters,
kernel_size = kernel_size,
padding = 'same',
activation = 'sigmoid',
dilation_rate = dilation_rate )(x)
 x = Multiply()([tanh_out, sigm_out])
 x = Conv2D(filters = filters,
kernel_size = 1,
padding = 'same' )(x)
 res_x = Add()([res_x, x])
 return res_x
def wavenet(layer):
x = cbr(layer, 192, 7, 1, 1)
 x = BatchNormalization()(x)
 x = wave_block(x, 192, 3, 1)
 x = cbr(x, 96, 7, 1, 1)
 x = BatchNormalization()(x)
 x = wave_block(x, 96, 3, 1)
 x = cbr(x, 48, 5, 1, 1)
 x = BatchNormalization()(x)
 x = wave_block(x, 48, 3, 1)
 return x
def wavenet1(layer):
x = cbr(layer, 4, 7, 1, 1)
 x = BatchNormalization()(x)
 x = wave_block(x, 3, 3, 1)
 x = cbr(x, 3, 7, 1, 1)
 x = BatchNormalization()(x)
 x = wave_block(x, 16, 3, 1)
 x = cbr(x, 3, 5, 1, 1)
 return x
x0 = backbone
print('1')
 x1 = tf.keras.layers.GlobalAveragePooling2D()(x0.layers[-1].output)
 x3 = tf.keras.layers.GlobalAveragePooling2D()(x0.layers[-7].output)
 x5 = tf.keras.layers.GlobalAveragePooling2D()(x0.layers[-18].output)
 print('2')
 x1=wavenet(x0.layers[-1].output)
 x3=wavenet(x0.layers[-7].output)
 x5=wavenet(x0.layers[-18].output)
 x1 = tf.keras.layers.GlobalAveragePooling2D()(x1)
 x3 = tf.keras.layers.GlobalAveragePooling2D()(x3)
 x5 = tf.keras.layers.GlobalAveragePooling2D()(x5)
 print('4')
 x = tf.concat([x1,x3,x5],axis = 1)
 x = tf.keras.layers.Dropout(0.7 )(x)
 x = tf.keras.layers.Dense(192 )(x)
 x = tf.keras.layers.Dropout(0.4 )(x)
 output = tf.keras.layers.Softmax(dtype='float32' )(x)
 output =tf.keras.layers.Dense(CLASS_N )(x)
 print('5')
 model = tf.keras.models.Model(inputs = x0.input, outputs = output)
 return model
return Classifier([HEIGHT,WIDTH,3])
 model = create_model()
model.summary() 
<prepare_x_and_y>
tfa = np.vstack(
 df_all.timestamp_first_active.astype(str ).apply(
 lambda x: list(map(int, [x[:4], x[4:6], x[6:8],
x[8:10], x[10:12],
x[12:14]]))
).values 
)
 df_all['tfa_year'] = tfa[:,0]
df_all['tfa_month'] = tfa[:,1]
df_all['tfa_day'] = tfa[:,2]
df_all = df_all.drop(['timestamp_first_active'], axis=1 )
@tf.function
def _mixup(inp, targ):
indice = tf.range(len(inp))
indice = tf.random.shuffle(indice)
 sinp = tf.gather(inp, indice, axis=0)
 starg = tf.gather(targ, indice, axis=0)
 alpha = 0.2
t = tf.compat.v1.distributions.Beta(alpha, alpha ).sample([len(inp)])
 tx = tf.reshape(t, [-1, 1, 1, 1])
 ty = tf.reshape(t, [-1, 1])
 x = inp * tx + sinp *(1-tx)
 y = targ * ty + starg *(1-ty)
 return x, y<create_dataframe>
ohe_feats = ['gender', 'signup_method', 'signup_flow', 'language',
'affiliate_channel', 'affiliate_provider',
'first_affiliate_tracked', 'signup_app',
'first_device_type', 'first_browser']
for f in ohe_feats:
df_all_dummy = pd.get_dummies(df_all[f], prefix=f)
 df_all = df_all.drop([f], axis=1)
 df_all = pd.concat(( df_all, df_all_dummy), axis=1 )
tfrecs = sorted(tf.io.gfile.glob(TRAIN_TFREC + '/*.tfrec'))
parsed_trainval =(tf.data.TFRecordDataset(tfrecs, num_parallel_reads=AUTOTUNE)
.map(_parse_function, num_parallel_calls=AUTOTUNE ).unbatch() 
.filter(_filtTP ).enumerate() )<data_type_conversions>
X = df_all.iloc[:piv_train,:]
le = LabelEncoder()
y = le.fit_transform(labels)
 X_test = df_all.iloc[piv_train:,:]
indices = []
spid = []
recid = []
for i, sample in tqdm(parsed_trainval.prefetch(AUTOTUNE)) :
indices.append(i.numpy())
 spid.append(sample['species_id'].numpy())
 recid.append(sample['recording_id'].numpy().decode() )<create_dataframe>
params = {'eta': 0.1,
'max_depth': 8,
'nround': 100,
'subsample': 0.7,
'colsample_bytree': 0.8,
'seed': 1,
'objective': 'multi:softprob',
'eval_metric':'ndcg',
'num_class': 12,
'nthread':3}
num_boost_round = 10
dtrain = xgb.DMatrix(X, y)
 clf1 = xgb.train(params=params, dtrain=dtrain, num_boost_round=num_boost_round )
table = pd.DataFrame({'indices': indices, 'species_id': spid, 'recording_id': recid})
 table<categorify>
importance = clf1.get_fscore()
importance_df = pd.DataFrame(
 sorted(importance.items() , key=operator.itemgetter(1)) ,
columns=['feature','fscore']
)
<categorify><EOS> 
ids = []
cts = []
for i in range(len(id_test)) :
idx = id_test[i]
ids += [idx] * 5
cts += le.inverse_transform(np.argsort(y_pred[i])[::-1])[:5].tolist()
sub = pd.DataFrame(np.column_stack(( ids, cts)) , columns=['id', 'country'])
 sub.to_csv('sub.csv',index=False )