Kaggle competition: Toxic Comment Classification Challenge
<SOS> metric: MCAUC Kaggle data source: toxic-comment-classification-challenge<load_pretrained>
import sys, os, re, csv, codecs, numpy as np, pandas as pd
import tensorflow as tf
from keras import backend as K
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import train_test_split
from keras.callbacks import Callback
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.layers import Dense, Input, LSTM, Embedding, Dropout, Activation, GRU, concatenate, MaxPooling1D
from keras.layers import Bidirectional, GlobalMaxPooling1D, Flatten, TimeDistributed, Conv1D, SpatialDropout1D, GlobalAveragePooling1D
from keras.models import Model
from keras import initializers, regularizers, constraints, optimizers, layers
nett = make_model(Z.shape[2])
 nett.load_weights("w.h5" )<predict_on_test>
path = '.. /input/'
comp = 'jigsaw-toxic-comment-classification-challenge/'
EMBEDDING_FILE=f'{path}glove840b300dtxt/glove.840B.300d.txt'
TRAIN_DATA_FILE=f'{path}{comp}train.csv'
TEST_DATA_FILE=f'{path}{comp}test.csv'
pv = nett.predict(xv, batch_size=500, verbose=1)
 pe = nett.predict(xe, batch_size=500, verbose=1 )<compute_test_metric>
embed_size = 300
max_features = 150000
maxlen = 150
nett.evaluate(xv, yv, batch_size=5000 )<find_best_params>
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
sv = scale[:,LEN-56:LEN-28]
se = scale[:,LEN-28:LEN]<define_variables>
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)
names = [f"F{i+1}" for i in range(28)]<create_dataframe>
embeddings_index = {}
with open(EMBEDDING_FILE,encoding='utf8')as f:
for line in f:
values = line.rstrip().rsplit(' ')
 word = values[0]
coefs = np.asarray(values[1:], dtype='float32')
 embeddings_index[word] = coefs
piv = pd.DataFrame(ids[:, 0], columns=["id"] )<feature_engineering>
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
QUANTILES = ["0.005", "0.025", "0.165", "0.250", "0.500", "0.750", "0.835", "0.975", "0.995"]
VALID = []
EVAL = []
for i, quantile in tqdm(enumerate(QUANTILES)) :
t1 = pd.DataFrame(pv[:,:, i]*sv, columns=names)
 t1 = piv.join(t1)
 t1["id"] = t1["id"] + f"_{quantile}_validation"
t2 = pd.DataFrame(pe[:,:, i]*se, columns=names)
 t2 = piv.join(t2)
 t2["id"] = t2["id"] + f"_{quantile}_evaluation"
VALID.append(t1)
 EVAL.append(t2)
<create_dataframe>
inp = Input(shape=(maxlen,))
x = Embedding(max_features, embed_size, weights=[embedding_matrix] )(inp)
 x = Conv1D(64, kernel_size = 2, padding = "valid", kernel_initializer = "glorot_uniform" )(x)
 x = Bidirectional(LSTM(100, return_sequences=True,dropout=0.05,recurrent_dropout=0.05))(x)
 avg_pool = GlobalAveragePooling1D()(x)
 max_pool = GlobalMaxPooling1D()(x)
 x = concatenate([avg_pool, max_pool])
 x = Dense(6, activation="sigmoid" )(x)
 model = Model(inputs=inp, outputs=x)
 adam = optimizers.Adam(lr=0.0015)
 model.compile(loss='binary_crossentropy', optimizer=adam, metrics=['accuracy'] )
sub = pd.DataFrame()
sub = sub.append(VALID + EVAL)
 del VALID, EVAL, t1, t2<save_to_csv>
class RocAucEvaluation(Callback):
def __init__(self, validation_data=() , interval=1):
super(Callback, self ).__init__()
self.interval = interval
self.X_val, self.y_val = validation_data
def on_epoch_end(self, epoch, logs={}):
if epoch % self.interval == 0:
y_pred = self.model.predict(self.X_val, verbose=0)
 score = roc_auc_score(self.y_val, y_pred)
 print("
ROC-AUC - epoch: %d - score: %.6f
" %(epoch+1, score))
sub.to_csv("submission.csv", index=False )<import_modules>
x_train, x_test, y_train, y_test = train_test_split(X_t, y, test_size = 0.05, random_state = 144)
 RocAuc = RocAucEvaluation(validation_data=(x_test, y_test), interval=1 )
import numpy as np
import pandas as pd
from tqdm import tqdm
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error, mean_absolute_error<predict_on_test>
model.fit(x_train, y_train, batch_size=64, epochs=2, validation_data=(x_test, y_test), callbacks=[RocAuc]);
class MultipleRegression() :
def __init__(self, reg=0.01):
self.reg = reg
self.BETA = None
def fit(self, Y, Z):
F = Z.shape[1]
U = np.linalg.inv(Z.T.dot(Z)+self.reg*np.eye(F))
self.BETA = Y.dot(Z ).dot(U)
 def predict(self, Z):
return self.BETA.dot(Z.T)
<categorify>
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 )