Kaggle competition: Toxic Comment Classification Challenge
<SOS> metric: MCAUC Kaggle data source: toxic-comment-classification-challenge<compute_train_metric>
print(os.listdir(".. /input"))
model = lgb.LGBMClassifier(boosting_type = 'dart', colsample_bytree = 0.88, learning_rate = 0.028,
min_child_samples = 10, num_leaves = 36, reg_alpha = 0.76, reg_lambda = 0.43,
subsample_for_bin = 40000, subsample = 0.54, class_weight = 'balanced',
objective = 'multiclass', n_estimators = 100, random_state = 10)
 cv_score = cross_val_score(model, train_set, train_labels, cv = 10, scoring = scorer)
 cv_score.mean() , cv_score.std()
model = lgb.LGBMClassifier(boosting_type = 'dart', colsample_bytree = 0.88, learning_rate = 0.028,
min_child_samples = 10, num_leaves = 36, reg_alpha = 0.76, reg_lambda = 0.43,
subsample_for_bin = 40000, subsample = 0.54, class_weight = 'balanced',
objective = 'binary', n_estimators = 100, random_state = 10)
 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 = lgb.LGBMClassifier(boosting_type = 'dart', colsample_bytree = 0.88, learning_rate = 0.028,
min_child_samples = 10, num_leaves = 36, reg_alpha = 0.76, reg_lambda = 0.43,
subsample_for_bin = 40000, subsample = 0.54, class_weight = 'balanced',
objective = 'multiclass', n_estimators = 100, random_state = 10)
 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)
 submission.to_csv('lgb_submission.csv', index = False )<import_modules>
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' )
import numpy as np
import pandas as pd
from sklearn import neighbors
import matplotlib.pyplot as plt
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import f1_score
import lightgbm as lgb
from sklearn.model_selection import StratifiedKFold
from sklearn import manifold
import os<load_from_csv>
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()
df = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 sample = pd.read_csv(".. /input/sample_submission.csv" )<sort_values>
max_features=100000
maxlen=150
embed_size=300
total = df.isnull().sum().sort_values(ascending = False)
 total[total>0]<data_type_conversions>
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))
df['rez_esc'] = df['rez_esc'].fillna(0)
 test['rez_esc'] = test['rez_esc'].fillna(0 )<categorify>
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 )
def replace_v18q1(x):
if(x['v18q'] == 0):
return 0
else:
return x['v18q1']<feature_engineering>
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
df['v18q1'] = df.apply(lambda x: replace_v18q1(x), axis=1)
 test['v18q1'] = test.apply(lambda x: replace_v18q1(x), axis=1 )<data_type_conversions>
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
df['v2a1'] = df['v2a1'].fillna(0)
 test['v2a1'] = test['v2a1'].fillna(0 )<data_type_conversions>
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'] )
df['meaneduc'] = df['meaneduc'].fillna(0)
 test['SQBmeaned'] = test['SQBmeaned'].fillna(0 )<data_type_conversions>
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 )
df['edjefe'] = df['edjefe'].replace({'no': 0, 'yes':1} ).astype(float)
 test['edjefe'] = test['edjefe'].replace({'no': 0, 'yes':1} ).astype(float)
 df['edjefa'] = df['edjefa'].replace({'no': 0, 'yes':1} ).astype(float)
 test['edjefa'] = test['edjefa'].replace({'no': 0, 'yes':1} ).astype(float )<feature_engineering>
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 )
<feature_engineering><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('submission.csv', index=False )