Kaggle competition: Natural Language Processing with Disaster Tweets
X_test_scaled = scaler.transform(X_test)
 print(X_test_scaled.shape )<train_model>
!pip install transformers
input_dim = x_train.shape[1]
activation = 'tanh'
classes = 1
history = dict()
prediction_scores = dict()
for i in np.arange(0, 10):
optim = optimizers.Adam(lr = 0.001)
 ensemble_model = Model(input_dim = input_dim, activation = activation, classes = classes)
 ensemble_model.compile(loss = 'mean_absolute_error', optimizer = optim, metrics = ['mean_absolute_error'])
 print('TRAINING MODEL NO : {}'.format(i))
H = ensemble_model.fit(x_train_scaled, y_train_flatten,
batch_size = 64,
epochs = 200,
verbose = 1)
 history[i] = H
ensemble_model.save('MODEL_{}.model'.format(i))
predictions = ensemble_model.predict(X_test_scaled, verbose = 1, batch_size = 64)
 prediction_scores[i] = predictions<prepare_output>
tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
 model = TFDistilBertForSequenceClassification.from_pretrained('distilbert-base-uncased')
prediction1 = np.hstack([p.reshape(-1,1)for p in prediction_scores.values() ])
 prediction1 = np.mean(prediction1, axis = 1)
 print(prediction1.shape )<import_modules>
train = pd.read_csv("/kaggle/input/nlp-getting-started/train.csv")
 test = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv" )
import lightgbm as lgb
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import KFold
from sklearn.model_selection import GridSearchCV<init_hyperparams>
train_text_df = train[['text', 'target']]
train_text_df = train_text_df.dropna()
train_X = train_text_df['text']
train_y = train_text_df['target'].to_numpy()
params = {'boosting_type': 'gbdt',
'objective': 'regression_l1',
'nthread': 4,
'num_leaves': 64,
'learning_rate': 0.05,
'max_bin': 512,
'colsample_bytree': 1,
'reg_alpha': 0.1,
'reg_lambda': 0.1,
'min_split_gain': 0.1,
'min_child_weight': 0.1,
'min_child_samples': 20,
'metric' : 'mae'}<choose_model_class>
train_x = tokenizer.batch_encode_plus(train_X, pad_to_max_length=True, return_tensors="tf" )
gbm = lgb.LGBMRegressor(boosting_type= 'gbdt',
objective = 'regression_l1',
n_jobs = 4,
silent = True,
max_bin = params['max_bin'],
min_split_gain = params['min_split_gain'],
min_child_weight = params['min_child_weight'],
min_child_samples = params['min_child_samples'] )<train_model>
optimizer = tf.keras.optimizers.Adam(learning_rate=3e-5, epsilon=1e-08, clipnorm=1.0)
 loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
 bce = tf.keras.losses.BinaryCrossentropy()
metric = tf.keras.metrics.SparseCategoricalAccuracy('accuracy')
 model.compile(optimizer=optimizer, loss=loss, metrics=[metric])
 model.fit(x=train_x['input_ids'], y=train_y, epochs=15, batch_size=128, verbose=1 )
folds = KFold(n_splits = 12, shuffle = True, random_state = 101)
 for fold_n,(train_index, valid_index)in enumerate(folds.split(x_train)) :
print('Fold', fold_n)
 X_train_f, X_valid = x_train.iloc[train_index], x_train.iloc[valid_index]
y_train_f, y_valid = y_train.iloc[train_index], y_train.iloc[valid_index]
gbm.fit(X_train_f, y_train_f.values.flatten() ,
eval_set=[(X_valid, y_valid.values.flatten())],
eval_metric = 'mae',
early_stopping_rounds = 200)
 print('Starting predicting...')
 y_pred = gbm.predict(X_valid, num_iteration = gbm.best_iteration_)
 print('The mae of prediction is:', mean_absolute_error(y_valid.values.flatten() , y_pred))
print('Feature importances:', list(gbm.feature_importances_))<save_to_csv>
test_x = test['text'].to_numpy()
submission['time_to_failure'] = prediction1
submission.to_csv('ensemble_submission.csv' )<predict_on_test>
test_x = tokenizer.batch_encode_plus(test_x, pad_to_max_length=True, return_tensors="tf" )
lgbm_predictions = gbm.predict(X_test )<save_to_csv>
predictions = model.predict(test_x['input_ids'] )
submission['time_to_failure'] = lgbm_predictions
print(submission.head())
 submission.to_csv('lgbm_submission.csv' )<save_to_csv>
predictions_label = [ np.argmax(x)for x in predictions[0]]
<save_to_csv><EOS> 
submission = pd.DataFrame({'id': test['id'], 'target': predictions_label})
 submission['target'] = submission['target'].astype('int')
 submission.to_csv('submission.csv', index=False )