Kaggle competition: Natural Language Processing with Disaster Tweets
<SOS> metric: meanfscore Kaggle data source: natural-language-processing-with-disaster-tweets<load_from_csv>
train_orig = 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")
 train_orig.shape
submission = pd.read_csv(".. /input/sampleSubmission.csv" )<save_to_csv>
ids_with_target_error = [328,443,513,2619,3640,3900,4342,5781,6552,6554,6570,6701,6702,6729,6861,7226]
train_orig[train_orig['id'].isin(ids_with_target_error)]
submission["count"] = np.expm1(preds1)+ np.expm1(preds2)
 submission.to_csv("result.csv", index=False )<load_from_csv>
train_orig.at[train_orig['id'].isin(ids_with_target_error),'target'] = 0
train_orig[train_orig['id'].isin(ids_with_target_error)]
train = pd.read_csv(".. /input/train.csv" )<load_from_csv>
train_add = pd.read_csv("/kaggle/input/real-or-not-nlp-with-disaster-tweets-addings/train_add.csv")
 train_add
train = pd.read_csv("/kaggle/input/bike-sharing-demand/train.csv",parse_dates=["datetime"])
 train.head()<load_from_csv>
train = train_orig.append(train_add)
 train.shape
test = pd.read_csv("/kaggle/input/bike-sharing-demand/test.csv",parse_dates=["datetime"])
 test.head()<feature_engineering>
!wget --quiet https://raw.githubusercontent.com/tensorflow/models/master/official/nlp/bert/tokenization.py
train["year"] = train["datetime"].dt.year
train["month"] = train["datetime"].dt.month
train["hour"] = train["datetime"].dt.hour
train["dayofweek"] = train["datetime"].dt.dayofweek
train.shape<feature_engineering>
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
test["year"] = test["datetime"].dt.year
test["month"] = test["datetime"].dt.month
test["hour"] = test["datetime"].dt.hour
test["dayofweek"] = test["datetime"].dt.dayofweek
test.shape<define_variables>
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 )
categorical_feature = ["season","holiday","workingday","weather","dayofweek","month","year","hour"]<data_type_conversions>
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, :]
out = Dense(1, activation='sigmoid' )(clf_output)
 model = Model(inputs=[input_word_ids, input_mask, segment_ids], outputs=out)
 model.compile(Adam(lr=1e-5), loss='binary_crossentropy', metrics=['accuracy'])
 return model
for var in categorical_feature:
train[var] = train[var].astype("category")
 test[var] = test[var].astype("category")
 train.info()<define_variables>
%%time
module_url = "https://tfhub.dev/tensorflow/bert_en_uncased_L-24_H-1024_A-16/1"
bert_layer = hub.KerasLayer(module_url, trainable=True )
feature = ["season","holiday","workingday","weather","dayofweek","year","hour","temp","atemp","humidity"]<prepare_x_and_y>
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" )
X_train = train[feature]
X_test = test[feature]
X_train.head()<prepare_x_and_y>
vocab_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()
do_lower_case = bert_layer.resolved_object.do_lower_case.numpy()
tokenizer = tokenization.FullTokenizer(vocab_file, do_lower_case )
Y_train = train["count"]
Y_train.head()<train_model>
train_input = bert_encode(train.text.values, tokenizer, max_len=160)
 test_input = bert_encode(test.text.values, tokenizer, max_len=160)
 train_labels = train.target.values
model = RandomForestRegressor(n_estimators=500)
 Y_train_log = np.log1p(Y_train)
 model.fit(X_train,Y_train_log)
 result = model.predict(X_test )<prepare_output>
checkpoint = ModelCheckpoint('model.h5', monitor='val_accuracy', save_best_only=True)
 train_history = model.fit(
 train_input, train_labels,
validation_split=0.2,
epochs=10,
callbacks=[checkpoint],
batch_size=16
)
np.exp(result )<load_from_csv>
model.load_weights('model.h5')
 test_pred = model.predict(test_input )
sub = pd.read_csv("/kaggle/input/bike-sharing-demand/sampleSubmission.csv")
 sub.head()<feature_engineering>
submission['target'] = test_pred.round().astype(int)
 submission.to_csv('submission.csv', index=False )