Kaggle competition: Airbnb New User Bookings
!wget https://raw.githubusercontent.com/Iamsdt/DLProjects/master/utils/TextPre.py<import_modules>
train_users = pd.read_csv('.. /input/airbnb-recruiting-new-user-bookings/train_users_2.csv')
 test_users = pd.read_csv('.. /input/airbnb-recruiting-new-user-bookings/test_users.csv' )
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras.layers import Dense, Input
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Model
from tensorflow.keras.callbacks import ModelCheckpoint
import tensorflow_hub as hub
import tokenization
from TextPre import clean<categorify>
labels = train_users['country_destination'].values
train_users = train_users.drop(['country_destination', 'date_first_booking'], axis=1)
 test_users = test_users.drop(['date_first_booking'], axis=1)
 id_test = test_users['id']
all_users = pd.concat(( train_users, test_users), axis=0, ignore_index=True)
 all_users.drop('id',axis=1, inplace=True)
 all_users.head()
def bert_encode(texts, tokenizer, max_len=512):
all_tokens = []
all_masks = []
all_segments = []
for text in texts:
text = tokenizer.tokenize(text)
 text = text[:max_len-2]
input_sequence = ["[CLS]"] + text + ["[SEP]"]
pad_len = max_len - len(input_sequence)
 tokens = tokenizer.convert_tokens_to_ids(input_sequence)
 tokens += [0] * pad_len
pad_masks = [1] * len(input_sequence)+ [0] * pad_len
segment_ids = [0] * max_len
all_tokens.append(tokens)
 all_masks.append(pad_masks)
 all_segments.append(segment_ids)
 return np.array(all_tokens), np.array(all_masks), np.array(all_segments )<choose_model_class>
all_users['date_account_created'] = pd.to_datetime(all_users['date_account_created'])
 all_users['timestamp_first_active'] = pd.to_datetime(( all_users.timestamp_first_active // 1000000), format='%Y%m%d')
 all_users['date_account_created'] = [datetime.timestamp(d)for d in all_users['date_account_created']]
all_users['timestamp_first_active'] = [datetime.timestamp(d)for d in all_users['timestamp_first_active']]
def build_model(bert_layer, max_len=512):
input_word_ids = Input(shape=(max_len,), dtype=tf.int32, name="input_word_ids")
 input_mask = Input(shape=(max_len,), dtype=tf.int32, name="input_mask")
 segment_ids = Input(shape=(max_len,), dtype=tf.int32, name="segment_ids")
 _, sequence_output = bert_layer([input_word_ids, input_mask, segment_ids])
 clf_output = sequence_output[:, 0, :]
dense = Dense(2048, activation='relu' )(clf_output)
 out = Dense(1, activation='sigmoid' )(dense)
 model = Model(inputs=[input_word_ids, input_mask, segment_ids], outputs=out)
 model.compile(Adam(lr=0.001), loss='binary_crossentropy', metrics=['accuracy'])
 return model<choose_model_class>
categorical_features = [
'affiliate_channel',
'affiliate_provider',
'first_affiliate_tracked',
'first_browser',
'first_device_type',
'gender',
'language',
'signup_app',
'signup_method'
]
for categorical_feature in categorical_features:
all_users_dummies = pd.get_dummies(all_users[categorical_feature], prefix=categorical_feature)
 all_users = all_users.drop([categorical_feature], axis=1)
 all_users = pd.concat(( all_users, all_users_dummies), axis=1 )
%%time
module_url = "https://tfhub.dev/tensorflow/bert_en_uncased_L-24_H-1024_A-16/1"
bert_layer = hub.KerasLayer(module_url, trainable=False )<load_from_csv>
train_users_n = train_users.shape[0]
X_train = all_users.values[:train_users_n]
le = LabelEncoder()
y_train = le.fit_transform(labels)
 X_test = all_users.values[train_users_n:]
train = pd.read_csv("/kaggle/input/nlp-getting-started/train.csv")
 test = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv")
 submission = pd.read_csv("/kaggle/input/nlp-getting-started/sample_submission.csv" )<feature_engineering>
def generate_answer(y_pred, classifer_name):
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(classifer_name+'.csv',index=False)
 return sub
train.text = train.text.apply(clean )<define_variables>
mlp = MLPClassifier()
mlp.fit(X_train, y_train)
 y_pred_mlp = mlp.predict_proba(X_test)
 generate_answer(y_pred_mlp, 'MLP' )
s = "Noches El - Bestia ' @ Alexis_Sanchez : happy to see my teammates and training hard ? ? goodnight gunners.? ? ? ? ? ?"<string_transform>
xgb = XGBClassifier(max_depth=6, learning_rate=0.3, n_estimators=25,
objective='multi:softprob', subsample=0.5, colsample_bytree=0.5, seed=0)
 xgb.fit(X_train, y_train)
 y_pred_xgb = xgb.predict_proba(X_test)
 generate_answer(y_pred_xgb, 'XGB' )