Kaggle competition: Natural Language Processing with Disaster Tweets
df_all['v18q'].value_counts()<count_missing_values>
!pip install keras-bert
!pip install keras-rectified-adam
df_all.isnull().sum().sort_values()<categorify>
import codecs
import tensorflow as tf
from tqdm import tqdm
from chardet import detect
import keras
from keras_radam import RAdam
from keras import backend as K
from keras_bert import load_trained_model_from_checkpoint
import codecs
from keras_bert import Tokenizer
df_all['SQBmeaned'].fillna(-1, inplace=True)
 df_all['meaneduc'].fillna(-1, inplace=True)
 df_all['rez_esc'].fillna(-1, inplace=True )<define_variables>
!wget -q https://storage.googleapis.com/bert_models/2018_10_18/uncased_L-12_H-768_A-12.zip
!unzip -o uncased_L-12_H-768_A-12.zip
feats = [c for c in df_all.columns if c not in ['Id', 'idhogar', 'Target']]<choose_model_class>
SEQ_LEN = 128
LR = 1e-4
pretrained_path = 'uncased_L-12_H-768_A-12'
config_path = os.path.join(pretrained_path, 'bert_config.json')
 checkpoint_path = os.path.join(pretrained_path, 'bert_model.ckpt')
 vocab_path = os.path.join(pretrained_path, 'vocab.txt')
rf = RandomForestClassifier(n_jobs=-1, n_estimators=200, random_state=42 )<train_model>
token_dict = {}
with codecs.open(vocab_path, 'r', 'utf8')as reader:
for line in reader:
token = line.strip()
token_dict[token] = len(token_dict)
 tokenizer = Tokenizer(token_dict )
rf.fit(train[feats], train['Target'] )<predict_on_test>
data = pd.read_csv('.. /input/nlp-getting-started/train.csv')
 data_eval = pd.read_csv('.. /input/nlp-getting-started/test.csv')
 data.head()
test['Target'] = rf.predict(test[feats] ).astype(int )<count_values>
def remove_URL(text):
url = re.compile(r'https?://\S+|www\.\S+')
 return url.sub(r'',text)
 def remove_html(text):
html=re.compile(r'<.*?>')
 return html.sub(r'',text)
 def remove_emoji(text):
emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F"
u"\U0001F300-\U0001F5FF"
u"\U0001F680-\U0001F6FF"
u"\U0001F1E0-\U0001F1FF"
u"\U00002702-\U000027B0"
u"\U000024C2-\U0001F251"
"]+", flags=re.UNICODE)
 return emoji_pattern.sub(r'', text)
 def remove_punct(text):
table=str.maketrans('','',string.punctuation)
 return text.translate(table)
 def remove_tag(text):
url = re.compile(r'(?<=\@)(.*? )(?=)')
 return url.sub(r'',text)
 def remove_space(text):
return text.replace(r'%20',' ')
 def cleanse_data(data_in):
data = data_in.copy()
data['text'] = data['text'].apply(lambda x:remove_URL(x))
data['text'] = data['text'].apply(lambda x:remove_html(x))
data['text'] = data['text'].apply(lambda x:remove_emoji(x))
data['text'] = data['text'].apply(lambda x:remove_tag(x))
data['text'] = data['text'].apply(lambda x:remove_punct(x))
data['keyword'].fillna('Nothing', inplace=True)
 data['keyword'] = data['keyword'].apply(lambda x:remove_space(x))
data['keyword'] = data['keyword'].apply(lambda x:remove_punct(x))
data['location'].fillna('Nothing', inplace=True)
 data['location'] = data['location'].apply(lambda x:remove_punct(x))
return data
test['Target'].value_counts(normalize=True )<save_to_csv>
data_cleansed = cleanse_data(data )
test[['Id', 'Target']].to_csv('submission.csv', index=False )<count_values>
def get_X(data, column_name = 'text'):
X1 = [tokenizer.encode(text, max_len=SEQ_LEN)[0] for text in data[column_name] ]
X1 = np.array(X1)
 X2 = np.zeros_like(X1)
 X = [X1,X2]
return X
X_train = get_X(data_cleansed)
 y_train = np.array(data_cleansed.target )
train['Target'].value_counts(normalize=True )<filter>
bert_model = load_trained_model_from_checkpoint(
 config_path,
checkpoint_path,
training=True,
trainable=True,
seq_len=SEQ_LEN,
)
heads = train[train['parentesco1'] == 1]<save_to_csv>
sess = K.get_session()
uninitialized_variables = set([i.decode('ascii')for i in sess.run(tf.report_uninitialized_variables())])
 init_op = tf.variables_initializer(
 [v for v in tf.global_variables() if v.name.split(':')[0] in uninitialized_variables] 
)
 sess.run(init_op )
rf2 = RandomForestClassifier(n_jobs=-1, n_estimators=200, random_state=42)
 rf2.fit(heads[feats], heads['Target'])
 test['Target'] = rf2.predict(test[feats] ).astype(int)
 test[['Id', 'Target']].to_csv('submission.csv', index=False )<count_values>
def get_model(neuron=256):
inputs = bert_model.inputs[:2]
outputs = bert_model.get_layer('NSP-Dense' ).output
outputs = keras.layers.Dense(neuron, activation='relu' )(outputs)
 outputs = keras.layers.Dense(units=1, activation='sigmoid' )(outputs)
 model = keras.models.Model(inputs, outputs)
 for layer in model.layers[:-20]:
layer.trainable = False
model.compile(RAdam(learning_rate =LR),loss='binary_crossentropy',metrics=['accuracy'])
 return model
train['hhsize'].value_counts()<count_values>
train['tamviv'].value_counts()<count_values>
model = get_model(neuron=32)
 model.summary()
history = model.fit(X_train,y_train,
epochs=4,
batch_size=64,
verbose=1,
validation_split=0.1 )
train['tamhog'].value_counts()<feature_engineering>
y_pred = model.predict(X_train, verbose=True ).flatten()
y_pred = np.round(y_pred ).astype(int ).reshape(X_train[0].shape[0])
 pred = pd.DataFrame(data=y_pred, columns=['pred'])
 pred_df = pd.concat([data['text'], data_cleansed['text'], data['target'],pred], axis=1)
 incorrect = pred_df[pred_df.pred!=pred_df.target]
df_all['hhsize-pc'] = df_all['hhsize'] / df_all['tamviv']
df_all['mobile-pc'] = df_all['qmobilephone'] / df_all['tamviv']
df_all['tablet-pc'] = df_all['v18q1'] / df_all['tamviv']
df_all['rooms-pc'] = df_all['rooms'] / df_all['tamviv']<define_variables>
data_eval_cleansed = cleanse_data(data_eval)
 X_eval = get_X(data_eval_cleansed )
feats = [c for c in df_all.columns if c not in ['Id', 'idhogar', 'Target']]<save_to_csv>
y_eval = model.predict(X_eval, verbose=True)
 y_eval = y_eval.flatten()
y_eval = np.round(y_eval ).astype(int ).reshape(X_eval[0].shape[0] )
rf3 = RandomForestClassifier(n_jobs=-1, n_estimators=200, random_state=42)
 rf3.fit(train[feats], train['Target'])
 test['Target'] = rf3.predict(test[feats] ).astype(int)
 test[['Id', 'Target']].to_csv('submission.csv', index=False )<filter>
pred = pd.DataFrame(data=y_eval, columns=['pred'])
 pd.concat([data_eval, data_eval_cleansed['text'], pred], axis=1 )
<save_to_csv><EOS> 
output = pd.DataFrame({'id': data_eval_cleansed.id, 'target': y_eval})
 output.to_csv('my_submission_20200205.csv', index=False)
 print("Your submission was successfully saved!" )