Kaggle competition: Toxic Comment Classification Challenge
Answer = clf.predict(d_test)
 sub = pd.DataFrame()
sub['ID'] = test.ID
sub['y'] = Answer
sub.to_csv('mercedes_benz_The_best_or_Nothing.csv', index=False )<import_modules>
train = pd.read_csv(".. /input/jigsaw-toxic-comment-classification-challenge/train.csv")
 test = pd.read_csv(".. /input/jigsaw-toxic-comment-classification-challenge/test.csv")
 embeding_file_path = ".. /input/glove840b300dtxt/glove.840B.300d.txt"
import matplotlib.pyplot as plt
import lightgbm as lgb
import gc
from sklearn.model_selection import KFold, cross_val_score
from sklearn.metrics import confusion_matrix,precision_recall_curve,auc,roc_auc_score,roc_curve,recall_score,classification_report
from sklearn.preprocessing import LabelEncoder<load_from_csv>
def get_wordnet_pos(treebank_tag):
if treebank_tag.startswith('J'):
return wordnet.ADJ
elif treebank_tag.startswith('V'):
return wordnet.VERB
elif treebank_tag.startswith('N'):
return wordnet.NOUN
elif treebank_tag.startswith('R'):
return wordnet.ADV
else:
return wordnet.NOUN
wl = WordNetLemmatizer()
filters='!"
'
def normalize(text):
text = text.lower()
translate_map = str.maketrans(filters, " " * len(filters))
text = text.translate(translate_map)
 tokens = nltk.word_tokenize(text)
 tags = nltk.pos_tag(tokens)
 stop_words = set(stopwords.words('english'))
seq= [wl.lemmatize(t[0], pos=get_wordnet_pos(t[1])) for t in tags if t[0] not in stop_words]
seq= [wl.lemmatize(t[0], pos=get_wordnet_pos(t[1])) for t in tags]
return seq
app_train = pd.read_csv('.. /input/application_train.csv')
 print('Training data shape: ', app_train.shape)
 app_train.head()<load_from_csv>
max_features = 50000
embed_size = 300
maxlen = 150
data = pd.concat(( train['comment_text'], test['comment_text']))
seqs = [normalize(text)for text in data]
def seq_to_sequence(seq, word_index):
sequence = []
for word in seq:
if not word_index.get(word): continue
sequence.append(word_index[word])
 return sequence
def fit_on_sequence(seqs):
word_counts = dict()
for seq in seqs:
for w in seq:
if w not in word_counts:
word_counts[w] = 0
word_counts[w] += 1
wcounts = list(word_counts.items())
 wcounts.sort(key=lambda x: x[1], reverse=True)
 sorted_voc = [wc[0] for wc in wcounts if wc[1]>=3]
sorted_voc = [wc[0] for wc in wcounts]
word_index = dict(list(zip(sorted_voc, list(range(1, len(sorted_voc)+ 1)))))
 return word_index
word_index = fit_on_sequence(seqs)
 train_words = [seq_to_sequence(seq, word_index)for seq in seqs[:train.shape[0]]]
test_words = [seq_to_sequence(seq, word_index)for seq in seqs[train.shape[0]:]]
train_words = pad_sequences(train_words, maxlen=maxlen)
 test_words = pad_sequences(test_words, maxlen=maxlen )
app_test = pd.read_csv('.. /input/application_test.csv')
 print('Testing data shape: ', app_test.shape)
 app_test.head()<count_values>
def get_coef(word, *coefs):
return word, np.asarray(coefs, dtype=np.float64)
 embeding_dict = dict(get_coef(*s.strip().split(" ")) for s in open(embeding_file_path))
app_train['TARGET'].value_counts()
print('The proportion of label 1 is %.2f' %(sum(app_train['TARGET']==1)/app_train.shape[0]*100), '%' )<count_missing_values>
max_words = min(max_features, len(word_index))
embeding_matrix = np.zeros(( max_words+1, embed_size))
for word,i in word_index.items() :
if word not in embeding_dict: continue
if i>max_words:break
embeding_matrix[i] = embeding_dict[word]
def missing_values_table(df):
mis_val = df.isnull().sum()
mis_val_percent = df.isnull().sum() * 100 / df.shape[0]
mis_val_table = pd.concat([mis_val, mis_val_percent], axis = 1)
 mis_val_table_rename_columns = mis_val_table.rename(columns = {0: 'Missing Values', 1: 'Percentage'})
 mis_val_table_rename_columns = mis_val_table_rename_columns[
mis_val_table_rename_columns.iloc[:, 1]!=0].sort_values('Percentage', ascending=False ).round(1)
 print('The total dataframe has ' + str(df.shape[1])+ ' columns')
 print('There are ' + str(mis_val_table_rename_columns.shape[0])+ ' columns')
 return mis_val_table_rename_columns<count_missing_values>
def get_model() :
inp = Input(shape=(maxlen,))
x = Embedding(max_words+1, embed_size, weights=[embeding_matrix] )(inp)
 x = SpatialDropout1D(0.2 )(x)
 x = Bidirectional(GRU(maxlen, return_sequences=True))(x)
 x = Conv1D(64, kernel_size = 2, padding = "valid", kernel_initializer = "glorot_uniform" )(x)
 max_pool = GlobalMaxPooling1D()(x)
 avg_pool = GlobalAveragePooling1D()(x)
 conc = concatenate([max_pool, avg_pool])
 oup = Dense(6, activation='sigmoid' )(conc)
 model = Model(input=inp, output=oup)
 model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
 return model
model = get_model()
missing_values = missing_values_table(app_train)
 missing_values.head(20 )<count_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))
batch_size = 128
epochs = 4
y_train = train[["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"]].values
X_tra, X_val, y_tra, y_val = train_test_split(train_words, y_train, train_size=0.95, random_state=233)
 RocAuc = RocAucEvaluation(validation_data=(X_val, y_val), interval=1)
 hist = model.fit(X_tra, y_tra, batch_size=batch_size, epochs=epochs, validation_data=(X_val, y_val),
callbacks=[RocAuc,], verbose=1)
 y_pred = model.predict(test_words, batch_size=1024)
<count_unique_values><EOS> 
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('gru.csv', index=False )