Kaggle competition: Natural Language Processing with Disaster Tweets
bjoined = None
gc.collect()
bbgrouped = bflattened.groupby('SK_ID_CURR' ).sum(min_count=1)
 bbgrouped.columns = ['bbpv_sum_' + hdr.replace("('", "_" ).replace("',", "_" ).replace(")", "")\
for hdr in bbgrouped.columns]
bbgrouped.head()
data = data.merge(right=bbgrouped.reset_index() , how='left', on='SK_ID_CURR')
 data.head()

 data.head(2)
<merge>
!wget --quiet https://raw.githubusercontent.com/tensorflow/models/master/official/nlp/bert/tokenization.py
bflattened = None
bbgrouped = None
gc.collect()
bjoined = bureau.merge(right=app_train[['AMT_CREDIT','SK_ID_CURR']].reset_index() , how='inner', on='SK_ID_CURR')
 bjoined['AMT_WEIGHT'] = bjoined['AMT_CREDIT_SUM'] / bjoined['AMT_CREDIT']
bjoined.head() 
<create_dataframe>
import tokenization
pv = pd.pivot_table(bjoined, index='SK_ID_CURR', columns=['CREDIT_ACTIVE','YEAR'], \
values=['AMT_WEIGHT', \
'AMT_CREDIT_MAX_OVERDUE', \
'DAYS_CREDIT_ENDDATE'],aggfunc='sum')

flattened = pd.DataFrame(pv.to_records() ).set_index('SK_ID_CURR')
 flattened.columns = ['bpv_' + hdr.replace("('", "_" ).replace("',", "_" ).replace(")", "")\
for hdr in flattened.columns]
pv = None
flattened.head(10)
<merge>
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 )
bjoined = None
gc.collect()
data.columns
data.set_index('SK_ID_CURR')
 data = data.merge(right=flattened.reset_index() , how='left', on='SK_ID_CURR')
 data.head()<groupby>
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=2e-6), loss='binary_crossentropy', metrics=['accuracy'])
 return model
bureau_avg = bureau.groupby('SK_ID_CURR' ).mean()
bureau_avg['buro_count'] = bureau[['SK_ID_BUREAU','SK_ID_CURR']].groupby('SK_ID_CURR' ).count() ['SK_ID_BUREAU']
fields = ['AMT_CREDIT_SUM','AMT_CREDIT_SUM_DEBT','CREDIT_DAY_OVERDUE','DAYS_CREDIT_ENDDATE','DAYS_CREDIT']
dates = [300,2000]
for f in fields:
lasti = 0
for i in dates:
bureau_avg['buro_wind_'+ f + str(i)] =(bureau[(bureau.DAYS_CREDIT_ENDDATE < i)&(bureau.DAYS_CREDIT_ENDDATE > lasti)])[[f,'SK_ID_CURR']].groupby('SK_ID_CURR' ).sum()
bureau_avg['buro_'+ f + str(i)] =(bureau[bureau.DAYS_CREDIT_ENDDATE < i])[[f,'SK_ID_CURR']].groupby('SK_ID_CURR' ).sum()
if lasti > 0:
bureau_avg['buro_derivitive_' + f + str(i)] = bureau_avg['buro_' + f + str(lasti)] /(bureau_avg['buro_' + f + str(i)]+ 0.0001)
 lasti = i
bureau_avg.columns = ['b_' + f_ for f_ in bureau_avg.columns]
bureau_avg.head()
data = data.merge(right=bureau_avg.reset_index() , how='left', on='SK_ID_CURR')
 for f in fields:
for i in dates:
data['b_buro_weighted_' + f + str(i)] =(data['b_buro_' + f + str(i)] / data['AMT_CREDIT_x'])
 data.head(10)
<groupby>
%%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 )
flattened = None
bureau_avg = None
gc.collect()
cnt_inst = installments_payments[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
installments_payments['SK_ID_PREV'] = installments_payments['SK_ID_CURR'].map(cnt_inst['SK_ID_PREV'])
 avg_inst = installments_payments.groupby('SK_ID_CURR' ).mean()

 avg_inst.columns = ['i_' + f_ for f_ in avg_inst.columns]
data = data.merge(right=avg_inst.reset_index() , how='left', on='SK_ID_CURR')
<groupby>
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" )
cnt_inst = None
avg_inst = None
gc.collect()
pcb_count = pcb[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
pcb['SK_ID_PREV'] = pcb['SK_ID_CURR'].map(pcb_count['SK_ID_PREV'])
 pcb_avg = pcb.groupby('SK_ID_CURR' ).mean()
fields = ['CNT_INSTALMENT_FUTURE']
dates = [-12,-36]
for f in fields:
lasti = 0
for i in dates:
pcb_avg['s_'+ f + str(i)] =(pcb[(pcb.MONTHS_BALANCE < i)])[[f,'SK_ID_CURR']].groupby('SK_ID_CURR' ).sum()
data = data.merge(right=pcb_avg.reset_index() , how='left', on='SK_ID_CURR')
 for f in fields:
for i in dates:
data['pcb' + '_wg_' + f + str(i)] =(data['s_' + f + str(i)] / data['AMT_CREDIT_x'])
 data.head() 
<groupby>
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 )
pcb_avg = None
gc.collect()
nb_prevs = credit_card_balance[['SK_ID_CURR', 'SK_ID_PREV']].groupby('SK_ID_CURR' ).count()
credit_card_balance['SK_ID_PREV'] = credit_card_balance['SK_ID_CURR'].map(nb_prevs['SK_ID_PREV'])
 avg_cc_bal = credit_card_balance.groupby('SK_ID_CURR' ).mean()
fields = ['AMT_BALANCE','AMT_DRAWINGS_ATM_CURRENT']
dates = [-12,-36]
for f in fields:
lasti = 0
for i in dates:
avg_cc_bal['s_'+ f + str(i)] =(credit_card_balance[(credit_card_balance.MONTHS_BALANCE < i)])[[f,'SK_ID_CURR']].groupby('SK_ID_CURR' ).sum()
avg_cc_bal.columns = ['cc_bal_' + f_ for f_ in avg_cc_bal.columns]
data = data.merge(right=avg_cc_bal.reset_index() , how='left', on='SK_ID_CURR')
 for f in ['AMT_BALANCE']:
for p in ['cc_bal_s_']:
for i in dates:
data[p + '_wg_' + f + str(i)] =(data[p + f + str(i)] / data['AMT_CREDIT_x'])
 data.head(10)
 avg_cc_bal.head() 
<set_options>
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
nb_prevs = None
avg_cc_bal = None
gc.collect() 
<split>
train_history = model.fit(
 train_input, train_labels,
validation_split=0.2,
epochs=3,
batch_size=16 
)
 model.save('model.h5' )
ignore_features = ['SK_ID_CURR', 'is_train', 'is_test']
relevant_features = [col for col in data.columns if col not in ignore_features]
trainX = data[data['is_train'] == 1][relevant_features]
testX = data[data['is_test'] == 1][relevant_features]
x_train, x_val, y_train, y_val = train_test_split(trainX, Y, test_size=0.2, random_state=18)
 lgb_train = lgb.Dataset(data=x_train, label=y_train)
 lgb_eval = lgb.Dataset(data=x_val, label=y_val )<train_model>
test_pred = model.predict(test_input )
<save_to_csv><EOS> 
submission['target'] = test_pred.round().astype(int)
 submission.to_csv('submission.csv', index=False )