Kaggle competition: Natural Language Processing with Disaster Tweets
<SOS> metric: meanfscore Kaggle data source: natural-language-processing-with-disaster-tweets<feature_engineering>
!wget --quiet https://raw.githubusercontent.com/tensorflow/models/master/official/nlp/bert/tokenization.py
!pip install keras
!pip install tensorflow-hub
!pip install tensorflow
df['log_trip_duration'] = np.log(df['trip_duration'] )<define_variables>
import tokenization
import warnings
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.metrics import classification_report
from keras.callbacks import ModelCheckpoint
import tensorflow as tf
import tensorflow_hub as hub
from tensorflow.keras.layers import Dense, Input, Dropout
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Model
NUM_VARS = ['pickup_pca0', 'pickup_pca1', 'dropoff_pca0', 'dropoff_pca1', 'month', 'hour', 'day', 'distance2']
TARGET = 'log_trip_duration'<define_variables>
plt.style.use('fivethirtyeight')
 warnings.filterwarnings("ignore" )
num_features = NUM_VARS<prepare_x_and_y>
dataset = pd.read_csv('.. /input/nlp-getting-started/train.csv')
 test = pd.read_csv('.. /input/nlp-getting-started/test.csv')
 submission = pd.read_csv('.. /input/nlp-getting-started/sample_submission.csv' )
X_train = df.loc[:, num_features]
y_train = df[TARGET]
X_test = df_test.loc[:, num_features]
X_train.shape, y_train.shape, X_test.shape<import_modules>
bert_layer = \
hub.KerasLayer("https://tfhub.dev/tensorflow/bert_en_uncased_L-24_H-1024_A-16/2",
trainable=True )
from sklearn.ensemble import RandomForestRegressor<import_modules>
max_len = max([len(x.split())for x in dataset.text])+ 1
from sklearn.ensemble import RandomForestRegressor<train_model>
def bert_encode(texts, tokenizer, max_len=None):
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 )
m = RandomForestRegressor(n_estimators=17, min_samples_leaf=10, min_samples_split=15, max_features='auto', max_depth=80, bootstrap=True)
 m.fit(X_train, y_train )<import_modules>
vocab_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()
do_lower_case = bert_layer.resolved_object.do_lower_case.numpy()
from sklearn.model_selection import cross_val_score<compute_train_metric>
tokenizer = tokenization.FullTokenizer(vocab_file, do_lower_case )
cv_scores = cross_val_score(m, X_train, y_train, cv=5, scoring='neg_mean_squared_log_error')
 cv_scores<predict_on_test>
train_input = bert_encode(dataset.text.values, tokenizer, max_len=max_len)
 train_labels = dataset.target.values
test_input = bert_encode(test.text, tokenizer, max_len=max_len )
y_test_pred = m.predict(X_test)
 y_test_pred[:5]<save_to_csv>
all_inputs = [
Input(shape=(max_len,), dtype=tf.int32),
Input(shape=(max_len,), dtype=tf.int32),
Input(shape=(max_len,), dtype=tf.int32)
 ]
__, sequence_output = bert_layer(all_inputs)
 x = sequence_output[:, 0, :]
x = Dropout(0.5 )(x)
 x = Dense(units=32, activation='relu' )(x)
 x = Dense(1, activation='sigmoid' )(x)
 model = Model(all_inputs, outputs=x)
 model.compile(Adam(lr= 0.00001),
loss='binary_crossentropy',
metrics=['accuracy'])
 model.summary()
my_submission = pd.DataFrame({'id': df_test.id, 'trip_duration': np.exp(y_test_pred)})
 my_submission.to_csv('submission.csv', index=False )<load_from_csv>
checkpoint = \
ModelCheckpoint('model.h5',
monitor='val_loss',
save_best_only=True,
verbose=1)
 train_history = \
model.fit(train_input,
train_labels,
validation_split=0.2,
epochs=5,
callbacks=[checkpoint],
batch_size=16 )
df = pd.read_csv("/kaggle/input/best-csv/best.csv" )<save_to_csv>
model.load_weights('model.h5' )
<set_options><EOS> 
submission.target = \
model\ 
.predict(test_input)\ 
.round() \ 
.astype(int)
 submission.to_csv("submission.csv", index=False )