Kaggle competition: Toxic Comment Classification Challenge
<SOS> metric: MCAUC Kaggle data source: toxic-comment-classification-challenge<concatenate>
import sys, os, re, csv, codecs, numpy as np, pandas as pd
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.layers import Dense, Input, LSTM, Embedding, Dropout, Activation
from keras.layers import Bidirectional, GlobalMaxPool1D
from keras.models import Model
from keras import initializers, regularizers, constraints, optimizers, layers
Train_Master = pd.concat([train,
vendor_train,
passenger_count_train,
store_and_fwd_flag_train,
cluster_pickup_train,
cluster_dropoff_train,
month_train,
dom_train,
hour_test,
dow_train
], axis=1 )<concatenate>
path = '.. /input/'
comp = 'jigsaw-toxic-comment-classification-challenge/'
EMBEDDING_FILE=f'{path}fasttext-crawl-300d-2m/crawl-300d-2M.vec'
TRAIN_DATA_FILE=f'{path}{comp}train.csv'
TEST_DATA_FILE=f'{path}{comp}test.csv'
Test_master = pd.concat([test,
vendor_test,
passenger_count_test,
store_and_fwd_flag_test,
cluster_pickup_test,
cluster_dropoff_test,
month_test,
dom_test,
hour_test,
dow_test], axis=1 )<drop_column>
embed_size = 300
max_features = 20000
maxlen = 100
Train_Master = Train_Master.drop(['pickup_datetime','pickup_date'],axis = 1)
 Test_master = Test_master.drop(['pickup_datetime','pickup_date'],axis = 1 )<split>
train = pd.read_csv(TRAIN_DATA_FILE)
 test = pd.read_csv(TEST_DATA_FILE)
 list_sentences_train = train["comment_text"].fillna("_na_" ).values
list_classes = ["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"]
y = train[list_classes].values
list_sentences_test = test["comment_text"].fillna("_na_" ).values
Train, Test = train_test_split(Train_Master[0:800000], test_size = 0.2 )<prepare_x_and_y>
tokenizer = Tokenizer(num_words=max_features)
 tokenizer.fit_on_texts(list(list_sentences_train))
list_tokenized_train = tokenizer.texts_to_sequences(list_sentences_train)
 list_tokenized_test = tokenizer.texts_to_sequences(list_sentences_test)
 X_t = pad_sequences(list_tokenized_train, maxlen=maxlen)
 X_te = pad_sequences(list_tokenized_test, maxlen=maxlen )
X_train = Train.drop(['log_trip_duration'], axis=1)
 Y_train = Train["log_trip_duration"]
X_test = Test.drop(['log_trip_duration'], axis=1)
 Y_test = Test["log_trip_duration"]
Y_test = Y_test.reset_index().drop('index',axis = 1)
 Y_train = Y_train.reset_index().drop('index',axis = 1 )<prepare_x_and_y>
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.strip().split())for o in open(EMBEDDING_FILE))
dtrain = xgb.DMatrix(X_train, label=Y_train)
 dvalid = xgb.DMatrix(X_test, label=Y_test)
 dtest = xgb.DMatrix(Test_master)
 watchlist = [(dtrain, 'train'),(dvalid, 'valid')]<train_model>
word_index = tokenizer.word_index
nb_words = min(max_features, len(word_index))
embedding_matrix = np.zeros(( nb_words, embed_size))
for word, i in word_index.items() :
if i >= max_features: continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None: embedding_matrix[i] = embedding_vector
xgb_pars = {'min_child_weight': 1, 'eta': 0.09, 'colsample_bytree': 0.9,
'max_depth': 6,
'subsample': 0.9, 'lambda': 1., 'nthread': -1, 'booster' : 'gbtree', 'silent': 1,
'eval_metric': 'rmse', 'objective': 'reg:linear'}
model = xgb.train(xgb_pars, dtrain, 250, watchlist, early_stopping_rounds=250,
maximize=False, verbose_eval=1)
 print('Modeling RMSLE %.5f' % model.best_score )<predict_on_test>
inp = Input(shape=(maxlen,))
x = Embedding(max_features, embed_size, weights=[embedding_matrix] )(inp)
 x = Bidirectional(LSTM(300, return_sequences=True, dropout=0.1, recurrent_dropout=0.1))(x)
 x = GlobalMaxPool1D()(x)
 x = Dense(50, activation="relu" )(x)
 x = Dropout(0.1 )(x)
 x = Dense(6, activation="sigmoid" )(x)
 model = Model(inputs=inp, outputs=x)
 model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'] )
pred = model.predict(dtest)
 pred = np.exp(pred)- 1<save_to_csv>
model.fit(X_t, y, batch_size=32, epochs=2, validation_split=0.1);
<import_modules><EOS> 
y_test = model.predict([X_te], batch_size=1024, verbose=1)
 sample_submission = pd.read_csv(f'{path}{comp}sample_submission.csv')
 sample_submission[list_classes] = y_test
sample_submission.to_csv('submission.csv', index=False )