Kaggle competition: Natural Language Processing with Disaster Tweets
<SOS> metric: meanfscore Kaggle data source: natural-language-processing-with-disaster-tweets<save_to_csv>
!wget --quiet https://raw.githubusercontent.com/tensorflow/models/master/official/nlp/bert/tokenization.py
submission.to_csv('submission.csv', index=False )<save_to_csv>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
from wordcloud import WordCloud
from nltk.corpus import stopwords
from tqdm.notebook import tqdm
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 sklearn.model_selection import train_test_split
submission.to_csv('submission.csv', index=False )<import_modules>
pd.set_option('display.max_rows', 500)
 pd.set_option('display.max_columns', 500)
 pd.set_option('display.width', 1000)
 plt.style.use('fivethirtyeight' )
import numpy as np
import pandas as pd<load_from_csv>
train_data = pd.read_csv("/kaggle/input/nlp-getting-started/train.csv")
 test_data = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv" )
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 test_data = pd.read_csv("/kaggle/input/titanic/test.csv" )<count_missing_values>
print("Shape of the training dataset: {}.".format(train_data.shape))
print("Shape of the testing dataset: {}".format(test_data.shape))
for col in train_data.columns:
nan_vals = train_data[col].isna().sum()
pcent =(train_data[col].isna().sum() / train_data[col].count())* 100
print("Total NaN values in column '{}' are: {}, which is {:.2f}% of the data in that column".format(col, nan_vals, pcent))
train_data.isnull().sum()<filter>
def bert_encode(texts, tokenizer, max_len=512):
all_tokens, all_masks, all_segments = [], [], []
for text in tqdm(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 )
train_data[train_data['Age'].isnull() ]<count_missing_values>
%%time
url = "https://tfhub.dev/tensorflow/bert_en_uncased_L-24_H-1024_A-16/1"
bert_layer = hub.KerasLayer(url, trainable=True )
test_data.isnull().sum()<filter>
vocab_fl = bert_layer.resolved_object.vocab_file.asset_path.numpy()
lower_case = bert_layer.resolved_object.do_lower_case.numpy()
tokenizer = tokenization.FullTokenizer(vocab_fl, lower_case )
train_data[train_data['Embarked'].isnull() ]<filter>
%%time
train_input = bert_encode(train_data['text'].values, tokenizer, max_len=160)
 test_input = bert_encode(test_data['text'].values, tokenizer, max_len=160)
 train_labels = train_data['target'].values
train_data[train_data['Ticket'] == '113572']<drop_column>
def build_model(transformer, 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')
 _, seq_op = transformer([input_word_ids, input_mask, segment_ids])
 class_tkn = seq_op[:, 0, :]
op = Dense(1, activation='sigmoid' )(class_tkn)
 model = Model(inputs=[input_word_ids, input_mask, segment_ids], outputs=op)
 model.compile(optimizer=Adam(1e-5), loss='binary_crossentropy', metrics=['accuracy'])
 return model
<categorify>
model = build_model(bert_layer, max_len=160)
 model.summary()
train_data['Age'] = train_data['Age'].fillna(train_data.groupby(['Pclass','Sex','Survived'])['Age'].transform('median'))
test_data['Age'] = test_data['Age'].fillna(test_data.groupby(['Pclass','Sex'])['Age'].transform('median'))<feature_engineering>
checkpoint = ModelCheckpoint('model.h5', monitor='val_loss', save_best_only=True)
 train_history = model.fit(
 train_input, train_labels,
validation_split=0.1,
epochs=3,
callbacks=[checkpoint],
batch_size=16
)
train_data['IsChild'] = np.where(train_data['Age'] <= 10, 'Yes', 'No')
 test_data['IsChild'] = np.where(test_data['Age'] <= 10, 'Yes', 'No' )<drop_column>
preds = model.predict(test_input )
train_data = train_data[train_data['Ticket'] != '113572']<save_to_csv>
sub_fl = pd.read_csv("/kaggle/input/nlp-getting-started/sample_submission.csv")
 sub_fl['target'] = preds.round().astype(int)
 sub_fl.to_csv("submission.csv", index=False )