Kaggle competition: Toxic Comment Classification Challenge
<SOS> metric: MCAUC Kaggle data source: toxic-comment-classification-challenge<prepare_x_and_y>
print(os.listdir(".. /input"))
y_pred = pd.DataFrame()
y_pred['sig_id'] = predictions[0]['sig_id']
for column in target_columns:
column_data = []
for i in range(len(predictions)) :
column_data.append(predictions[i][column])
 y_pred[column] = np.mean(column_data, axis=0)
 y_pred.shape<save_to_csv>
EMBEDDING_FILE = '.. /input/glove840b300dtxt/glove.840B.300d.txt'
train = pd.read_csv('.. /input/jigsaw-toxic-comment-classification-challenge/train.csv')
 test = pd.read_csv('.. /input/jigsaw-toxic-comment-classification-challenge/test.csv' )
submission = pd.DataFrame(index = public_ids + private_ids, columns=target_columns)
 submission.index.name = 'sig_id'
submission[:] = 0
submission.loc[y_pred.sig_id,:] = y_pred[target_columns].values
submission.loc[test[test.cp_type == 'ctl_vehicle'].sig_id] = 0
submission.to_csv('submission.csv', index=True)
 submission.head().T<load_pretrained>
train["comment_text"].fillna("fillna")
 test["comment_text"].fillna("fillna")
 X_train = train["comment_text"].str.lower()
y_train = train[["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"]].values
X_test = test["comment_text"].str.lower()
class simple_mpl(object):
def __init__(self, path, file_head):
print("Loading", path, "+", file_head)
 file_list = os.listdir(path)
 num = len(file_head + "_")
 self.models = []
for file in file_list:
if file[:num] == file_head + "_":
self.models.append(tf.keras.models.load_model(os.path.join(path, file)))
 cfg = pd.read_pickle(os.path.join(path, 'config.pkl'))
self.mean = cfg['mean']
self.std =cfg['std']
self.output_num = cfg["output_num"].max()
def predict(self, data):
pred = pd.DataFrame(np.zeros([data.index.size, self.output_num]), index=data.index)
 for mdl in self.models:
pred += mdl.predict(( data.astype("float32")- self.mean)/ self.std, batch_size=4096)
 pred /= len(self.models)
 return pred<load_from_csv>
max_features=100000
maxlen=150
embed_size=300
train = pd.read_csv('.. /input/lish-moa/train_features.csv')
 train_target = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 test = pd.read_csv('.. /input/lish-moa/test_features.csv')
 targets_nonscored = pd.read_csv('.. /input/lish-moa/train_targets_nonscored.csv')
 train_size = train.shape[0]
traintest = pd.concat([train, test])
 traintest.drop(['cp_type'], axis=1, inplace=True)
 traintest = pd.concat([traintest, pd.get_dummies(traintest['cp_time'], prefix='cp_time')], axis=1)
 traintest = pd.concat([traintest, pd.get_dummies(traintest['cp_dose'], prefix='cp_dose')], axis=1)
 traintest = traintest.drop(['cp_time', 'cp_dose'], axis=1)
 train = traintest[:train_size]
test = traintest[train_size:]
del traintest
x_train = train.drop('sig_id', axis=1)
 y_train = train_target.drop('sig_id', axis=1 ).astype(np.float32)
 x_test = test.drop('sig_id', axis=1 )<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}".format(epoch+1, score))
mdl = simple_mpl(path=".. /input/mplmodel11/mpl", file_head="")
 remove_cols=['g-39', 'g-85', 'g-75', 'g-734', 'g-484', 'g-217', 'g-545', 'g-10']
x_test = x_test[np.setdiff1d(x_test.columns.values, remove_cols)]
res = mdl.predict(x_test)
 submit = pd.DataFrame(res.values, index = test['sig_id'])
 submit.columns = y_train.columns
submit.to_csv("submission.csv")
 submit<set_options>
tok=text.Tokenizer(num_words=max_features,lower=True)
 tok.fit_on_texts(list(X_train)+list(X_test))
X_train=tok.texts_to_sequences(X_train)
 X_test=tok.texts_to_sequences(X_test)
 x_train=sequence.pad_sequences(X_train,maxlen=maxlen)
 x_test=sequence.pad_sequences(X_test,maxlen=maxlen )
%matplotlib inline<load_from_csv>
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
train_features = pd.read_csv('.. /input/lish-moa/train_features.csv')
 train_targets = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 test_features = pd.read_csv('.. /input/lish-moa/test_features.csv')
 sample_submission = pd.read_csv('.. /input/lish-moa/sample_submission.csv' )<categorify>
word_index = tok.word_index
num_words = min(max_features, len(word_index)+ 1)
 embedding_matrix = np.zeros(( num_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
def preprocess(df):

 df = df.copy()
df.loc[:, 'cp_type'] = df.loc[:, 'cp_type'].map({'trt_cp': 0, 'ctl_vehicle': 1})
 df.loc[:, 'cp_dose'] = df.loc[:, 'cp_dose'].map({'D1': 0, 'D2': 1})
 df.loc[:, 'cp_time'] = df.loc[:, 'cp_time'].map({24: 0, 48: 1, 72: 2})
 del df['sig_id']
return df<drop_column>
sequence_input = Input(shape=(maxlen,))
x = Embedding(max_features, embed_size, weights=[embedding_matrix],trainable = False )(sequence_input)
 x = SpatialDropout1D(0.2 )(x)
 x = Bidirectional(GRU(128, return_sequences=True,dropout=0.1,recurrent_dropout=0.1))(x)
 x = Conv1D(64, kernel_size = 3, padding = "valid", kernel_initializer = "glorot_uniform" )(x)
 avg_pool = GlobalAveragePooling1D()(x)
 max_pool = GlobalMaxPooling1D()(x)
 x = concatenate([avg_pool, max_pool])
 preds = Dense(6, activation="sigmoid" )(x)
 model = Model(sequence_input, preds)
 model.compile(loss='binary_crossentropy',optimizer=Adam(lr=1e-3),metrics=['accuracy'] )
train = preprocess(train_features)
 test = preprocess(test_features)
 del train_targets['sig_id']<filter>
batch_size = 128
epochs = 4
X_tra, X_val, y_tra, y_val = train_test_split(x_train, y_train, train_size=0.9, random_state=233 )
train_targets = train_targets.loc[train['cp_type']==0].reset_index(drop=True)
 train = train.loc[train['cp_type']==0].reset_index(drop=True )<normalization>
model.fit(X_tra, y_tra, batch_size=batch_size, epochs=epochs, validation_data=(X_val, y_val),callbacks = callbacks_list,verbose=1)
 model.load_weights(filepath)
 print('Predicting.... ')
 y_pred = model.predict(x_test,batch_size=1024,verbose=1 )
scaler = MinMaxScaler()
scaler.fit(train)
 train = scaler.transform(train)
 test = scaler.transform(test )<categorify>
submission = pd.read_csv('.. /input/jigsaw-toxic-comment-classification-challenge/sample_submission.csv')
 submission[["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"]] = y_pred
submission.to_csv('submission.csv', index=False )