Kaggle competition: Toxic Comment Classification Challenge
<set_options><EOS> 
import pandas as pd
import numpy as np
<SOS> metric: MCAUC Kaggle data source: toxic-comment-classification-challenge<load_from_csv>
import pandas as pd
import numpy as np
train = pd.read_csv(".. /input/mercedes-benz-greener-manufacturing/train.csv")
 test = pd.read_csv(".. /input/mercedes-benz-greener-manufacturing/test.csv")
 print("Training Dataset Shape {}".format(train.shape))
print("Testing Dataset Shape {}".format(test.shape))<drop_column>
warnings.filterwarnings('ignore')
Submission = test[['ID']]
test.drop('ID',axis=1,inplace=True)
 train.drop('ID',axis=1,inplace=True )<count_values>
path = '.. /input/'
comp = 'jigsaw-toxic-comment-classification-challenge/'
EMBEDDING_FILE = '.. /input/glove840b300dtxt/glove.840B.300d.txt'
TRAIN_DATA_FILE=f'{path}{comp}train.csv'
TEST_DATA_FILE=f'{path}{comp}test.csv'
train.dtypes.value_counts() 
<count_missing_values>
train = pd.read_csv(TRAIN_DATA_FILE)
 test = pd.read_csv(TEST_DATA_FILE)
 submission = pd.read_csv('.. /input/jigsaw-toxic-comment-classification-challenge/sample_submission.csv')
 X_train = train["comment_text"].fillna("fillna" ).values
y_train = train[["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"]].values
X_test = test["comment_text"].fillna("fillna" ).values
train.isna().sum() [train.isna().sum() > 0] 
<filter>
embed_size = 300
max_features = 100000
maxlen = 200
test.isna().sum() [test.isna().sum() > 0] 
<drop_column>
tokenizer = text.Tokenizer(num_words=max_features)
 tokenizer.fit_on_texts(list(X_train)+ list(X_test))
X_train = tokenizer.texts_to_sequences(X_train)
 X_test = tokenizer.texts_to_sequences(X_test)
 x_train = sequence.pad_sequences(X_train, maxlen=maxlen)
 x_test = sequence.pad_sequences(X_test, maxlen=maxlen )
train.drop(train[train['y'] > 250].index[0],axis=0,inplace=True )<count_unique_values>
def get_coefs(word, *arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.rstrip().rsplit(' ')) for o in open(EMBEDDING_FILE))
lst_train = []
for col in train.columns:
if train[col].nunique() == 1:
lst_train.append(col)
 print("Column {} has Single Unique Value".format(col))<count_unique_values>
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
lst_test = []
for col in test.columns:
if test[col].nunique() == 1:
lst_test.append(col)
 print("Column {} has Single Unique Value".format(col))<count_unique_values>
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))
for col in train.columns:
if train[col].dtype == "object":
print("Unique Values in {} column".format(col),train[col].nunique() )<count_unique_values>
def get_model() :
inp = Input(shape=(maxlen,))
x = Embedding(max_features,embed_size,weights=[embedding_matrix] )(inp)
 x = SpatialDropout1D(0.3 )(x)
 conv_0 = Conv1D(64, kernel_size=3, kernel_initializer='normal',activation='elu',padding='same' )(x)
 maxpool_0 = MaxPooling1D()(conv_0)
 conv_1 = Conv1D(64, kernel_size=5, kernel_initializer='normal',activation='elu',padding='same' )(x)
 maxpool_1 = MaxPooling1D()(conv_1)
 conv_2 = Conv1D(64, kernel_size=6, kernel_initializer='normal',activation='elu',padding='same' )(x)
 maxpool_2 = MaxPooling1D()(conv_2)
 z = Concatenate(axis=1 )([maxpool_0, maxpool_1, maxpool_2])
 z = Bidirectional(GRU(40, return_sequences=True))(z)
 avg_pool = GlobalAveragePooling1D()(z)
 max_pool = GlobalMaxPooling1D()(z)
 conc = concatenate([avg_pool, max_pool])
 outp = Dense(6, activation="sigmoid" )(conc)
 model = Model(inputs=inp, outputs=outp)
 model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
 return model
for col in test.columns:
if test[col].dtype == "object":
print("Unique Values in {} column".format(col),test[col].nunique() )<count_values>
session_conf = tf.ConfigProto(intra_op_parallelism_threads=4, inter_op_parallelism_threads=4)
 K.set_session(tf.Session(graph=tf.get_default_graph() , config=session_conf))
test['X4'].value_counts()<groupby>
model = get_model()
train.groupby('X4')['y'].agg({"count","min",'max','mean','median'} )<feature_engineering>
batch_size = 32
epochs = 2
X_tra, X_val, y_tra, y_val = train_test_split(x_train, y_train, train_size=0.95, random_state=233)
 RocAuc = RocAucEvaluation(validation_data=(X_val, y_val), interval=1 )
df = train[[col for col in train.columns if(( train[col].dtype == 'int')&(col not in ['ID','y'])) ]].columns
lst_train = []
for col in train.columns:
if(( train[col].dtype == 'int')&(col not in ['ID','y'])) :
lst_train.append(sum(train[col])/len(train))
df_new = pd.DataFrame(lst_train,df,columns=['Ratio_Train'] ).sort_index()
lst_test = []
for col in test.columns:
if(( test[col].dtype == 'int')&(col !='ID')) :
lst_test.append(sum(test[col])/len(test))
df_new['Ratio_Test'] = lst_test
df_new.sort_index()<data_type_conversions>
hist = model.fit(X_tra, y_tra, batch_size=batch_size, epochs=2, validation_data=(X_val, y_val),
callbacks=[RocAuc], verbose=2 )
upper = corr_matrix.where(np.triu(np.ones(corr_matrix.shape), k=1 ).astype(np.bool))
upper.head()<drop_column>
y_pred = model.predict(x_test, batch_size=1024)
 submission[["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"]] = y_pred
submission.to_csv('submission.csv', index=False )
<train_model><EOS>