Kaggle competition: Toxic Comment Classification Challenge
<SOS> metric: MCAUC Kaggle data source: toxic-comment-classification-challenge<drop_column>
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
ind['inst'] = np.argmax(np.array(ind[['instlevel1', 'instlevel2', 'instlevel3', 'instlevel4', 'instlevel5',
'instlevel6', 'instlevel7', 'instlevel8', 'instlevel9']]), axis = 1)
 ind = ind.drop(columns = [c for c in ind if c.startswith('instlevel')])
 ind = ind.drop(columns = 'female' )<concatenate>
path = '.. /input/'
comp = 'toxic-processed-comment-classification/'
EMBEDDING_FILE=f'{path}fasttext-crawl-300d-2m/crawl-300d-2M.vec'
TRAIN_DATA_FILE=f'{path}{comp}train_processed(1 ).csv'
TEST_DATA_FILE=f'{path}{comp}test_processed(1 ).csv'
ind = ind.drop(columns = 'Target')
 ind_agg_ordered = ind[['age', 'escolari', 'rez_esc', 'inst', 'idhogar']].groupby('idhogar' ).agg(['min', 'max'])
 new_col = []
for c in ind_agg_ordered.columns.levels[0]:
for stat in ind_agg_ordered.columns.levels[1]:
new_col.append(f'{c}-{stat}')
 ind_agg_ordered.columns = new_col
ind_agg = ind.groupby('idhogar' ).agg('mean')
 new_col = []
for c in ind_agg:
new_col.append(f'{c}-mean')
 ind_agg.columns = new_col
ind_agg = pd.concat([ind_agg, ind_agg_ordered], axis = 1 )<merge>
embed_size = 300
max_features = 20000
maxlen = 200
ind_agg = ind_agg.drop(columns = to_drop)
 final = heads.merge(ind_agg, on = 'idhogar', how = 'left' )<define_variables>
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
scorer = make_scorer(f1_score, greater_is_better = True, average = 'macro')
 submissions_base = test[['Id', 'idhogar']].copy()
train_set = final[final.Target.notnull() ].drop(columns = ['Id', 'idhogar', 'Target'])
 test_set = final[final.Target.isnull() ].drop(columns = ['Id', 'idhogar', 'Target'])
 train_labels = np.array(list(final[final['Target'].notnull() ]['Target'].astype(np.uint8)))
 test_ids = list(final.loc[final.Target.isnull() , 'idhogar'] )<train_model>
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 )
def split_model(model, train_set, train_labels, test_set, test_ids):

 train0_labels = np.array([x < 4 for x in train_labels])
 model.fit(train_set, train0_labels)
 test0_labels = model.predict(test_set)
 test1_set = test_set[test0_labels]
test1_ids = [test_ids[i] for i in range(len(test_ids)) if test0_labels[i]]
train1_set = train_set[train0_labels]
train1_labels = train_labels[train0_labels]
model.fit(train1_set, train1_labels)
 labels = model.predict(test1_set)
 labels = pd.DataFrame({'idhogar': test1_ids, 'Target': labels})
 submission = submissions_base.merge(labels, how = 'left', on = 'idhogar' ).drop(columns = 'idhogar')
 submission['Target'] = submission['Target'].fillna(4 ).astype(np.int8)
 return submission 
<categorify>
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))
pipeline = Pipeline([('imputer', Imputer(strategy = 'median')) ,('scaler', MinMaxScaler())])
 train_set = pipeline.fit_transform(train_set)
 test_set = pipeline.transform(test_set )<compute_train_metric>
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
model = RandomForestClassifier(n_estimators = 100, n_jobs = -1)
 cv_score = cross_val_score(model, train_set, train_labels, cv = 10, scoring = scorer)
 cv_score.mean() , cv_score.std()<save_to_csv>
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'] )
RF_submission = split_model(model, train_set, train_labels, test_set, test_ids)
 RF_submission.to_csv('RF_submission.csv', index = False )<compute_train_metric>
model.fit(X_t, y, batch_size=32, epochs=2, validation_split=0.1);
<train_model><EOS> 
comp1 = 'jigsaw-toxic-comment-classification-challenge/'
y_test = model.predict([X_te], batch_size=1024, verbose=1)
 sample_submission = pd.read_csv(f'{path}{comp1}sample_submission.csv')
 sample_submission[list_classes] = y_test
sample_submission.to_csv('submission.csv', index=False )