Kaggle competition: Jigsaw Unintended Bias in Toxicity Classification
<SOS> metric: JigsawBiasAUC Kaggle data source: jigsaw-unintended-bias-in-toxicity-classification<predict_on_test>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
yPredicted=clf.predict(xTest )<train_model>
def seed_everything(seed=123):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything()
clf = RandomForestClassifier(random_state=0)
 clf.fit(xTrain_small,yTrain_small )<compute_test_metric>
CRAWL_EMBEDDING_PATH = '.. /input/pickled-crawl300d2m-for-kernel-competitions/crawl-300d-2M.pkl'
GLOVE_EMBEDDING_PATH = '.. /input/pickled-glove840b300d-for-10sec-loading/glove.840B.300d.pkl'
clf.score(xTest_small,yTest_small )<train_model>
NUM_MODELS = 2
LSTM_UNITS = 128
DENSE_HIDDEN_UNITS = 4 * LSTM_UNITS
MAX_LEN = 220
def get_coefs(word, *arr):
return word, np.asarray(arr, dtype='float32')
 def load_embeddings(path):
with open(path,'rb')as f:
emb_arr = pickle.load(f)
 return emb_arr
clf_logistic_regression=LogisticRegression(max_iter=1000)
 clf_logistic_regression.fit(xTrain_small,yTrain_small )<compute_test_metric>
def build_matrix(word_index, path):
embedding_index = load_embeddings(path)
 embedding_matrix = np.zeros(( max_features + 1, 300))
unknown_words = []
for word, i in word_index.items() :
if i <= max_features:
try:
embedding_matrix[i] = embedding_index[word]
except KeyError:
try:
embedding_matrix[i] = embedding_index[word.lower() ]
except KeyError:
try:
embedding_matrix[i] = embedding_index[word.title() ]
except KeyError:
unknown_words.append(word)
 return embedding_matrix, unknown_words
clf.score(xTest_small,yTest_small )<create_dataframe>
def sigmoid(x):
return 1 /(1 + np.exp(-x))
class SpatialDropout(nn.Dropout2d):
def forward(self, x):
x = x.unsqueeze(2)
 x = x.permute(0, 3, 2, 1)
 x = super(SpatialDropout, self ).forward(x)
 x = x.permute(0, 3, 2, 1)
 x = x.squeeze(2)
 return x
def train_model(learn,test,output_dim,lr=0.001,
batch_size=512, n_epochs=4,
enable_checkpoint_ensemble=True):
all_test_preds = []
checkpoint_weights = [2 ** epoch for epoch in range(n_epochs)]
test_loader = torch.utils.data.DataLoader(test, batch_size=batch_size, shuffle=False)
 n = len(learn.data.train_dl)
 phases = [(TrainingPhase(n ).schedule_hp('lr', lr *(0.6**(i)))) for i in range(n_epochs)]
sched = GeneralScheduler(learn, phases)
 learn.callbacks.append(sched)
 for epoch in range(n_epochs):
learn.fit(1)
 test_preds = np.zeros(( len(test), output_dim))
for i, x_batch in enumerate(test_loader):
X = x_batch[0].cuda()
X_normal_feats = x_batch[1].cuda()
y_pred = sigmoid(learn.model(X, X_normal_feats ).detach().cpu().numpy())
 test_preds[i * batch_size:(i+1)* batch_size, :] = y_pred
all_test_preds.append(test_preds)
 if enable_checkpoint_ensemble:
test_preds = np.average(all_test_preds, weights=checkpoint_weights, axis=0)
 else:
test_preds = all_test_preds[-1]
return test_preds
final_dataFrame=pd.DataFrame()
final_dataFrame['PassengerId']=testPIds<import_modules>
train = pd.read_csv('.. /input/jigsaw-unintended-bias-in-toxicity-classification/train.csv')
 test = pd.read_csv('.. /input/jigsaw-unintended-bias-in-toxicity-classification/test.csv' )
from sklearn.neural_network import MLPClassifier<train_model>
symbols_to_isolate = '.,?!-;*"…:—() %
symbols_to_delete = '
🍕\r🐵😑\xa0\ue014\t\uf818\uf04a\xad😢🐶️\uf0e0😜😎👊\u200b\u200e😁عدويهصقأناخلىبمغر😍💖💵Е👎😀😂\u202a\u202c🔥😄🏻💥ᴍʏʀᴇɴᴅᴏᴀᴋʜᴜʟᴛᴄᴘʙғᴊᴡɢ😋👏שלוםבי😱‼\x81エンジ故障\u2009🚌ᴵ͞🌟😊😳😧🙀😐😕\u200f👍😮😃😘אעכח💩💯⛽🚄🏼ஜ😖ᴠ🚲‐😟😈💪🙏🎯🌹😇💔😡\x7f👌ἐὶήιὲκἀίῃἴξ🙄Ｈ😠\ufeff\u2028😉😤⛺🙂\u3000تحكسة👮💙فزط😏🍾🎉😞\u2008🏾😅😭👻😥😔😓🏽🎆🍻🍽🎶🌺🤔😪\x08‑🐰🐇🐱🙆😨🙃💕𝘊𝘦𝘳𝘢𝘵𝘰𝘤𝘺𝘴𝘪𝘧𝘮𝘣💗💚地獄谷улкнПоАН🐾🐕😆ה🔗🚽歌舞伎🙈😴🏿🤗🇺🇸мυтѕ⤵🏆🎃😩\u200a🌠🐟💫💰💎эпрд\x95🖐🙅⛲🍰🤐👆🙌\u2002💛🙁👀🙊🙉\u2004ˢᵒʳʸᴼᴷᴺʷᵗʰᵉᵘ\x13🚬🤓\ue602😵άοόςέὸתמדףנרךצט😒͝🆕👅👥👄🔄🔤👉👤👶👲🔛🎓\uf0b7\uf04c\x9f\x10成都😣⏺😌🤑🌏😯ех😲Ἰᾶὁ💞🚓🔔📚🏀👐\u202d💤🍇\ue613小土豆🏡❔⁉\u202f👠》कर्मा🇹🇼🌸蔡英文🌞🎲レクサス😛外国人关系Сб💋💀🎄💜🤢َِьыгя不是\x9c\x9d🗑\u2005💃📣👿༼つ༽😰ḷЗз▱ц￼🤣卖温哥华议会下降你失去所有的钱加拿大坏税骗子🐝ツ🎅\x85🍺آإشء🎵🌎͟ἔ油别克🤡🤥😬🤧й\u2003🚀🤴ʲшчИОРФДЯМюж😝🖑ὐύύ特殊作戦群щ💨圆明园קℐ🏈😺🌍⏏ệ🍔🐮🍁🍆🍑🌮🌯🤦\u200d𝓒𝓲𝓿𝓵안영하세요ЖљКћ🍀😫🤤ῦ我出生在了可以说普通话汉语好极🎼🕺🍸🥂🗽🎇🎊🆘🤠👩🖒🚪天一家⚲\u2006⚭⚆⬭⬯⏖新✀╌🇫🇷🇩🇪🇮🇬🇧😷🇨🇦ХШ🌐\x1f杀鸡给猴看ʁ𝗪𝗵𝗲𝗻𝘆𝗼𝘂𝗿𝗮𝗹𝗶𝘇𝗯𝘁𝗰𝘀𝘅𝗽𝘄𝗱📺ϖ\u2000үսᴦᎥһͺ\u2007հ\u2001ɩｙｅ൦ｌƽｈ𝐓𝐡𝐞𝐫𝐮𝐝𝐚𝐃𝐜𝐩𝐭𝐢𝐨𝐧Ƅᴨןᑯ໐ΤᏧ௦Іᴑ܁𝐬𝐰𝐲𝐛𝐦𝐯𝐑𝐙𝐣𝐇𝐂𝐘𝟎ԜТᗞ౦〔Ꭻ𝐳𝐔𝐱𝟔𝟓𝐅🐋ﬃ💘💓ё𝘥𝘯𝘶💐🌋🌄🌅𝙬𝙖𝙨𝙤𝙣𝙡𝙮𝙘𝙠𝙚𝙙𝙜𝙧𝙥𝙩𝙪𝙗𝙞𝙝𝙛👺🐷ℋ𝐀𝐥𝐪🚶𝙢Ἱ🤘ͦ💸ج패티Ｗ𝙇ᵻ👂👃ɜ🎫\uf0a7БУі🚢🚂ગુજરાતીῆ🏃𝓬𝓻𝓴𝓮𝓽𝓼☘﴾̯﴿₽\ue807𝑻𝒆𝒍𝒕𝒉𝒓𝒖𝒂𝒏𝒅𝒔𝒎𝒗𝒊👽😙\u200cЛ‒🎾👹⎌🏒⛸公寓养宠物吗🏄🐀🚑🤷操美𝒑𝒚𝒐𝑴🤙🐒欢迎来到阿拉斯ספ𝙫🐈𝒌𝙊𝙭𝙆𝙋𝙍𝘼𝙅ﷻ🦄巨收赢得白鬼愤怒要买额ẽ🚗🐳𝟏𝐟𝟖𝟑𝟕𝒄𝟗𝐠𝙄𝙃👇锟斤拷𝗢𝟳𝟱𝟬⦁マルハニチロ株式社⛷한국어ㄸㅓ니͜ʖ𝘿𝙔₵𝒩ℯ𝒾𝓁𝒶𝓉𝓇𝓊𝓃𝓈𝓅ℴ𝒻𝒽𝓀𝓌𝒸𝓎𝙏ζ𝙟𝘃𝗺𝟮𝟭𝟯𝟲👋🦊多伦🐽🎻🎹⛓🏹🍷🦆为和中友谊祝贺与其想象对法如直接问用自己猜本传教士没积唯认识基督徒曾经让相信耶稣复活死怪他但当们聊些政治题时候战胜因圣把全堂结婚孩恐惧且栗谓这样还♾🎸🤕🤒⛑🎁批判检讨🏝🦁🙋😶쥐스탱트뤼도석유가격인상이경제황을렵게만들지않록잘관리해야합다캐나에서대마초와화약금의품런성분갈때는반드시허된사용🔫👁凸ὰ💲🗯𝙈Ἄ𝒇𝒈𝒘𝒃𝑬𝑶𝕾𝖙𝖗𝖆𝖎𝖌𝖍𝖕𝖊𝖔𝖑𝖉𝖓𝖐𝖜𝖞𝖚𝖇𝕿𝖘𝖄𝖛𝖒𝖋𝖂𝕴𝖟𝖈𝕸👑🚿💡知彼百\uf005𝙀𝒛𝑲𝑳𝑾𝒋𝟒😦𝙒𝘾𝘽🏐𝘩𝘨ὼṑ𝑱𝑹𝑫𝑵𝑪🇰🇵👾ᓇᒧᔭᐃᐧᐦᑳᐨᓃᓂᑲᐸᑭᑎᓀᐣ🐄🎈🔨🐎🤞🐸💟🎰🌝🛳点击查版🍭𝑥𝑦𝑧ＮＧ👣\uf020っ🏉ф💭🎥Ξ🐴👨🤳🦍\x0b🍩𝑯𝒒😗𝟐🏂👳🍗🕉🐲چی𝑮𝗕𝗴🍒ꜥⲣⲏ🐑⏰鉄リ事件ї💊「」\uf203\uf09a\uf222\ue608\uf202\uf099\uf469\ue607\uf410\ue600燻製シ虚偽屁理屈Г𝑩𝑰𝒀𝑺🌤𝗳𝗜𝗙𝗦𝗧🍊ὺἈἡχῖΛ⤏🇳𝒙ψՁմեռայինրւդձ冬至ὀ𝒁🔹🤚🍎𝑷🐂💅𝘬𝘱𝘸𝘷𝘐𝘭𝘓𝘖𝘹𝘲𝘫کΒώ💢ΜΟΝΑΕ🇱♲𝝈↴💒⊘Ȼ🚴🖕🖤🥘📍👈➕🚫🎨🌑🐻𝐎𝐍𝐊𝑭🤖🎎😼🕷ｇｒｎｔｉｄｕｆｂｋ𝟰🇴🇭🇻🇲𝗞𝗭𝗘𝗤👼📉🍟🍦🌈🔭《🐊🐍\uf10aლڡ🐦\U0001f92f\U0001f92a🐡💳ἱ🙇𝗸𝗟𝗠𝗷🥜さようなら🔼'
clf=MLPClassifier(hidden_layer_sizes=(20,10),max_iter=1000,activation='logistic')
 clf.fit(xTrain_small,yTrain_small )<compute_test_metric>
tokenizer = TreebankWordTokenizer()
isolate_dict = {ord(c):f' {c} ' for c in symbols_to_isolate}
remove_dict = {ord(c):f'' for c in symbols_to_delete}
def handle_punctuation(x):
x = x.translate(remove_dict)
 x = x.translate(isolate_dict)
 return x
def handle_contractions(x):
x = tokenizer.tokenize(x)
 return x
def fix_quote(x):
x = [x_[1:] if x_.startswith("'")else x_ for x_ in x]
x = ' '.join(x)
 return x
def preprocess(x):
x = handle_punctuation(x)
 x = handle_contractions(x)
 x = fix_quote(x)
 return x
clf.score(xTest_small,yTest_small )<predict_on_test>
x_train = train['comment_text'].progress_apply(lambda x:preprocess(x))
y_aux_train = train[['target', 'severe_toxicity', 'obscene', 'identity_attack', 'insult', 'threat']]
x_test = test['comment_text'].progress_apply(lambda x:preprocess(x))
identity_columns = [
'male', 'female', 'homosexual_gay_or_lesbian', 'christian', 'jewish',
'muslim', 'black', 'white', 'psychiatric_or_mental_illness']
weights = np.ones(( len(x_train),)) / 4
weights +=(train[identity_columns].fillna(0 ).values>=0.5 ).sum(axis=1 ).astype(bool ).astype(np.int)/ 4
weights +=(((train['target'].values>=0.5 ).astype(bool ).astype(np.int)+ 
(train[identity_columns].fillna(0 ).values<0.5 ).sum(axis=1 ).astype(bool ).astype(np.int)) > 1 ).astype(bool ).astype(np.int)/ 4
weights +=(((train['target'].values<0.5 ).astype(bool ).astype(np.int)+ 
(train[identity_columns].fillna(0 ).values>=0.5 ).sum(axis=1 ).astype(bool ).astype(np.int)) > 1 ).astype(bool ).astype(np.int)/ 4
loss_weight = 1.0 / weights.mean()
y_train = np.vstack([(train['target'].values>=0.5 ).astype(np.int),weights] ).T
max_features = 400000
yPredicted_MLP_Small=clf.predict(xTest_small )<compute_test_metric>
tokenizer = text.Tokenizer(num_words = max_features, filters='',lower=False )
print(classification_report(yTest_small,yPredicted_MLP_Small))<compute_test_metric>
tokenizer.fit_on_texts(list(x_train)+ list(x_test))
crawl_matrix, unknown_words_crawl = build_matrix(tokenizer.word_index, CRAWL_EMBEDDING_PATH)
 print('n unknown words(crawl): ', len(unknown_words_crawl))
glove_matrix, unknown_words_glove = build_matrix(tokenizer.word_index, GLOVE_EMBEDDING_PATH)
 print('n unknown words(glove): ', len(unknown_words_glove))
max_features = max_features or len(tokenizer.word_index)+ 1
max_features
embedding_matrix = np.concatenate([crawl_matrix, glove_matrix], axis=-1)
 embedding_matrix.shape
del crawl_matrix
del glove_matrix
gc.collect()
y_train_torch = torch.tensor(np.hstack([y_train, y_aux_train]), dtype=torch.float32 )
print(confusion_matrix(yTest_small,yPredicted_MLP_Small))<predict_on_test>
x_train = tokenizer.texts_to_sequences(x_train)
 x_test = tokenizer.texts_to_sequences(x_test )
yPredicted_MLP=clf.predict(xTest )<create_dataframe>
lengths = torch.from_numpy(np.array([len(x)for x in x_train]))
maxlen = 300
x_train_padded = torch.from_numpy(sequence.pad_sequences(x_train, maxlen=maxlen))
x_train_padded.shape
final_dataFrame['Survived']=yPredicted_MLP<save_to_csv>
class SequenceBucketCollator() :

 def __init__(self, choose_length, normal_feats_idx, sequence_index, length_index, label_index=None):
self.choose_length = choose_length
self.sequence_index = sequence_index
self.length_index = length_index
self.label_index = label_index
self.normal_feats_idx = normal_feats_idx
def __call__(self, batch):
batch = [torch.stack(x)for x in list(zip(*batch)) ]
sequences = batch[self.sequence_index]
lengths = batch[self.length_index]
normal_feats = batch[self.normal_feats_idx]
length = self.choose_length(lengths)
 mask = torch.arange(start=maxlen, end=0, step=-1)< length
padded_sequences = sequences[:, mask]
batch[self.sequence_index] = padded_sequences
batch[self.normal_feats_idx] = normal_feats
if self.label_index is not None:
return [x for i, x in enumerate(batch)if i != self.label_index], batch[self.label_index]
return batch
final_dataFrame.to_csv('titanic.csv', index=False)
 print("Your submission was successfully saved!" )<set_options>
class NeuralNet(nn.Module):
def __init__(self, embedding_matrix, num_aux_targets):
super(NeuralNet, self ).__init__()
embed_size = embedding_matrix.shape[1]
self.embedding = nn.Embedding(max_features, embed_size)
 self.embedding.weight = nn.Parameter(torch.tensor(embedding_matrix, dtype=torch.float32))
self.embedding.weight.requires_grad = False
self.embedding_dropout = SpatialDropout(0.3)
 self.lstm1 = nn.LSTM(embed_size, LSTM_UNITS, bidirectional=True, batch_first=True)
 self.lstm2 = nn.LSTM(LSTM_UNITS * 2, LSTM_UNITS, bidirectional=True, batch_first=True)
 self.normal_linear = nn.Linear(ADD_FEATS_UNITS, DENSE_HIDDEN_UNITS)
 self.linear1 = nn.Linear(DENSE_HIDDEN_UNITS, DENSE_HIDDEN_UNITS)
 self.linear2 = nn.Linear(DENSE_HIDDEN_UNITS, DENSE_HIDDEN_UNITS)
 self.linear_out = nn.Linear(DENSE_HIDDEN_UNITS, 1)
 self.linear_aux_out = nn.Linear(DENSE_HIDDEN_UNITS, num_aux_targets)
 def forward(self, x, normal_feats, lengths=None):
h_embedding = self.embedding(x.long())
 h_embedding = self.embedding_dropout(h_embedding)
 h_lstm1, _ = self.lstm1(h_embedding)
 h_lstm2, _ = self.lstm2(h_lstm1)
 avg_pool = torch.mean(h_lstm2, 1)
 max_pool, _ = torch.max(h_lstm2, 1)
 h_conc = torch.cat(( max_pool, avg_pool), 1)
 h_conc_linear1 = F.relu(self.linear1(h_conc))
h_conc_linear2 = F.relu(self.linear2(h_conc))
normal_linear = F.relu(self.normal_linear(normal_feats.float()))
hidden = h_conc + h_conc_linear1 + h_conc_linear2 + normal_linear
result = self.linear_out(hidden)
 aux_result = self.linear_aux_out(hidden)
 out = torch.cat([result, aux_result], 1)
 return out
%pylab inline
plt.style.use('seaborn-darkgrid')
 sns.set(font_scale=2)
 warnings.filterwarnings(action="ignore")
 n_arbres = 200
max_depth = 6
noms = [
"Random_Forest",
"Ada_Boost",
"Gradient_Boosting",
"LightGBM",
"XGBoost",
"CatBoost"
]
classifieurs = [
RandomForestClassifier(n_estimators=n_arbres,max_depth=max_depth),
AdaBoostClassifier(n_estimators=n_arbres),
GradientBoostingClassifier(n_estimators=n_arbres,max_depth=max_depth, max_leaf_nodes=4,min_samples_split=5),
XGBClassifier(n_estimators=n_arbres,max_depth=max_depth),
LGBMClassifier(n_estimators=n_arbres,max_depth=max_depth),
CatBoostClassifier(n_estimators=n_arbres,silent=True)
 ]
classifieursArbresDict = {nom:classifieur for nom, classifieur in zip(noms, classifieurs)}
def comparaisonsClassifieurs(classifieursDict, X_train, X_test, y_train, y_test):
t0 = time.time()
np.random.seed(123456)
 aucROC,accuracy,logloss,hammingloss,precision,sensibilite,f1,jaccard_similarity,avgPrecRec = \
dict() ,dict() ,dict() ,dict() ,dict() ,dict() ,dict() ,dict() ,dict()
yClassifications = pd.DataFrame()
yClassifications['Observations']=y_test
plt.figure(figsize=(18,36))
for nom in classifieursDict:
t1 = time.time()
classifieursDict[nom].fit(X_train, y_train)
 score = classifieursDict[nom].score(X_test, y_test)*100
print(f'Prédiction : {nom:32s}'+(' %.8f' % score ).lstrip('0'),end=' -- ')
 yClassifications[nom+'_prob'] = classifieursDict[nom].predict_proba(X_test)[:, 1]
yClassifications[nom+'_pred'] = classifieursDict[nom].predict(X_test)
 fpr, tpr, thresholds = roc_curve(y_test.ravel() , yClassifications[nom+'_prob'])
 accuracy[nom] = accuracy_score(yClassifications['Observations'],yClassifications[nom+'_pred'])
 logloss[nom] = log_loss(yClassifications['Observations'],yClassifications[nom+'_pred'])
 hammingloss[nom] = hamming_loss(yClassifications['Observations'],yClassifications[nom+'_pred'])
 precision[nom] = precision_score(yClassifications['Observations'],yClassifications[nom+'_pred'])
 sensibilite[nom] = recall_score(yClassifications['Observations'],yClassifications[nom+'_pred'])
 f1[nom] = f1_score(yClassifications['Observations'],yClassifications[nom+'_pred'])
 jaccard_similarity[nom] = jaccard_score(yClassifications['Observations'],yClassifications[nom+'_pred'])
 aucROC[nom] = auc(fpr, tpr)
 plt.subplot(2, 1, 1)
 print("Area under the ROC curve : %0.8f" % aucROC[nom],end=' -- ')
 plt.plot(fpr, tpr, label=f"{nom}(AUC = {aucROC[nom]:0.8f})")
 plt.plot([0, 1], [0, 1], 'k--')
 plt.xlim([-0.05, 1.05])
 plt.ylim([-0.05, 1.05])
 plt.xlabel('(1 - Specificity)--La Spécificité(Specificity)= VN /(FP + VN)',size=18)
 plt.ylabel('La Sensibilité(Sensitivity)= VP /(VP + FN)',size=18)
 plt.title('La courbe ROC(Receiver Operating Caracteristic)',size=20)
 plt.legend(loc="lower right")
 prec, rec, thresholds = precision_recall_curve(y_test.ravel() , yClassifications[nom+'_prob'])
 avgPrecRec[nom] = average_precision_score(y_test.ravel() , yClassifications[nom+'_prob'])
 plt.subplot(2, 1, 2)
 plt.step(rec, prec, where='post', label=f"{nom}(AP = {avgPrecRec[nom]:0.8f})")
 plt.fill_between(rec, prec, step='post', alpha=0.05)
 plt.plot([0, 1], [0.5, 0.5], 'k--')
 plt.xlabel('Le Rappel(Recall)= VP /(VP + FN)',size=18)
 plt.ylabel('La Précision = VP /(VP + FP)',size=18)
 plt.title('La courbe précision-rappel',size=20)
 plt.legend(loc="lower right")
 print(( '%0.2fs' %(time.time() - t1)).lstrip('0'))
plt.show()
print('Exécution :'+('%.2fs' %(time.time() - t0)).lstrip('0'))
resultats = pd.DataFrame(pd.Series(aucROC),columns=["aucROC"])
 resultats["avgPrecRec"] = pd.Series(avgPrecRec)
 resultats["accuracy"] = pd.Series(accuracy)
 resultats["log_loss"] = pd.Series(logloss)
 resultats["hamming_loss"] = pd.Series(hammingloss)
 resultats["precision"] = pd.Series(precision)
 resultats["sensibilite"] = pd.Series(sensibilite)
 resultats["f1"] = pd.Series(f1)
 resultats["jaccard_similarity"] = pd.Series(jaccard_similarity)
 resultats.sort_values(by='aucROC',ascending=False, inplace=True)
 return resultats
precision_score, recall_score, f1_score, jaccard_score, \
precision_recall_curve, average_precision_score
def controleClassifieur(classifieur, X_train, X_test, y_train, y_test, nom = 'CatBoost'):
t0 = time.time()
np.random.seed(123456)
 aucROC,accuracy,logloss,hammingloss,precision,sensibilite,f1,jaccard_similarity,avgPrecRec = \
dict() ,dict() ,dict() ,dict() ,dict() ,dict() ,dict() ,dict() ,dict()
yClassifications = pd.DataFrame()
yClassifications['Observations']=y_test
plt.figure(figsize=(18,36))
t1 = time.time()
classifieur.fit(X_train, y_train)
 score = classifieur.score(X_test, y_test)*100
print(f'Prédiction : {nom:32s}'+(' %.8f' % score ).lstrip('0'),end=' -- ')
 yClassifications[nom+'_prob'] = classifieur.predict_proba(X_test)[:, 1]
yClassifications[nom+'_pred'] = classifieur.predict(X_test)
 fpr, tpr, thresholds = roc_curve(y_test.ravel() , yClassifications[nom+'_prob'])
 accuracy[nom] = accuracy_score(yClassifications['Observations'],yClassifications[nom+'_pred'])
 logloss[nom] = log_loss(yClassifications['Observations'],yClassifications[nom+'_pred'])
 hammingloss[nom] = hamming_loss(yClassifications['Observations'],yClassifications[nom+'_pred'])
 precision[nom] = precision_score(yClassifications['Observations'],yClassifications[nom+'_pred'])
 sensibilite[nom] = recall_score(yClassifications['Observations'],yClassifications[nom+'_pred'])
 f1[nom] = f1_score(yClassifications['Observations'],yClassifications[nom+'_pred'])
 jaccard_similarity[nom] = jaccard_score(yClassifications['Observations'],yClassifications[nom+'_pred'])
 aucROC[nom] = auc(fpr, tpr)
 plt.subplot(2, 1, 1)
 print("Area under the ROC curve : %0.8f" % aucROC[nom],end=' -- ')
 plt.plot(fpr, tpr, label=f"{nom}(AUC = {aucROC[nom]:0.8f})")
 nb_decimales = 2
angleText = 36
transparence=0.6
for i,t in enumerate(thresholds*100):
if i > 0 :
plt.text(fpr[i], tpr[i],str(t.round(nb_decimales)) , rotation= angleText, alpha=transparence)
 plt.plot([0, 1], [0, 1], 'k--')
 plt.xlim([-0.05, 1.05])
 plt.ylim([-0.05, 1.05])
 plt.xlabel('(1 - Specificity)--La Spécificité(Specificity)= VN /(FP + VN)',size=18)
 plt.ylabel('La Sensibilité(Sensitivity)= VP /(VP + FN)',size=18)
 plt.title('La courbe ROC(Receiver Operating Caracteristic)',size=20)
 plt.legend(loc="lower right")
 prec, rec, thresholds = precision_recall_curve(y_test.ravel() , yClassifications[nom+'_prob'])
 avgPrecRec[nom] = average_precision_score(y_test.ravel() , yClassifications[nom+'_prob'])
 plt.subplot(2, 1, 2)
 plt.step(rec, prec, where='post', label=f"{nom}(AP = {avgPrecRec[nom]:0.8f})")
 plt.fill_between(rec, prec, step='post', alpha=0.05)
 plt.plot([0, 1], [0.5, 0.5], 'k--')
 plt.xlabel('Le Rappel(Recall)= VP /(VP + FN)',size=18)
 plt.ylabel('La Précision = VP /(VP + FP)',size=18)
 plt.title('La courbe précision-rappel',size=20)
 plt.legend(loc="lower right")
 print(( '%0.2fs' %(time.time() - t1)).lstrip('0'))
plt.show()
print('Exécution :'+('%.2fs' %(time.time() - t0)).lstrip('0'))
resultats = pd.DataFrame(pd.Series(aucROC),columns=["aucROC"])
 resultats["avgPrecRec"] = pd.Series(avgPrecRec)
 resultats["accuracy"] = pd.Series(accuracy)
 resultats["log_loss"] = pd.Series(logloss)
 resultats["hamming_loss"] = pd.Series(hammingloss)
 resultats["precision"] = pd.Series(precision)
 resultats["sensibilite"] = pd.Series(sensibilite)
 resultats["f1"] = pd.Series(f1)
 resultats["jaccard_similarity"] = pd.Series(jaccard_similarity)
 resultats.sort_values(by='aucROC',ascending=False, inplace=True)
 return resultats
def afficheImportanceVariables(classifieurs, X, nom = 'CatBoost'):
importances = pd.DataFrame(X.columns, columns=['nom'])
 importances['importance'] = classifieurs.feature_importances_
importances.sort_values(by='importance',ascending=False, inplace=True)
 sns_plot = sns.catplot(x='nom', y='importance',data=importances, kind='bar', size=18, aspect=4);
loc, labels = plt.xticks()
sns_plot.set_xticklabels(labels, rotation=90);
plt.show()
return importances
def effectueValidationCroisee(classifieur,
X,
y,
n_splits = 5,
nom = 'CatBoost'):
moyAucROC,stdAucRoc,moyAucPR,stdAucPR,moyAccuracy,stdAccuracy = \
dict() ,dict() ,dict() ,dict() ,dict() ,dict()
validationCroisee = StratifiedShuffleSplit(n_splits=n_splits)
 resultatsFinaux = pd.DataFrame()
classifieursCV={}
tprs = []
recs = []
aucROCs = pd.Series()
aucPRs = pd.Series()
scores = pd.Series()
mean_fpr = np.linspace(0, 1, 100)
 mean_prec = np.linspace(0, 1, 100)
 aucROC,avgPrecRec,accuracy,logloss,hammingloss,precision,sensibilite,f1,jaccard_similarity= \
dict() ,dict() ,dict() ,dict() ,dict() ,dict() ,dict() ,dict() ,dict()
plt.figure(figsize=(24,48))
for i,(train, test)in enumerate(validationCroisee.split(X, y)) :
X_train, X_test, y_train, y_test = X.iloc[train], X.iloc[test], y.iloc[train], y.iloc[test]
classifieur.fit(X_train, y_train)
 classifieursCV[i] = deepcopy(classifieur)
 scores = pd.concat([scores,pd.Series(classifieur.score(X_test, y_test)*100)])
 yClassifications_prob = classifieur.predict_proba(X_test)[:, 1]
yClassifications_pred = classifieur.predict(X_test)
 fpr, tpr, thresholds = roc_curve(y_test, yClassifications_prob)
 prec, rec, thresholds = precision_recall_curve(y_test, yClassifications_prob)
 avgPrecRec[(nom,i)] = average_precision_score(y_test, yClassifications_prob)
 accuracy[(nom,i)] = accuracy_score(y_test,yClassifications_pred)
 logloss[(nom,i)] = log_loss(y_test,yClassifications_pred)
 hammingloss[(nom,i)] = hamming_loss(y_test,yClassifications_pred)
 precision[(nom,i)] = precision_score(y_test,yClassifications_pred)
 sensibilite[(nom,i)] = recall_score(y_test,yClassifications_pred)
 f1[(nom,i)] = f1_score(y_test,yClassifications_pred)
 jaccard_similarity[(nom,i)] = jaccard_score(y_test,yClassifications_pred)
 aucROC[(nom,i)] = auc(fpr, tpr)
 tprs.append(interp(mean_fpr, fpr, tpr))
tprs[-1][0] = 0.0
recs.append(interp(mean_prec, prec, rec))
recs[-1][0] = 0.0
aucROCs = pd.concat([aucROCs,pd.Series(aucROC[(nom,i)])])
 aucPRs = pd.concat([aucPRs,pd.Series(avgPrecRec[(nom,i)])])
 plt.subplot(2, 1, 1)
 plt.plot(fpr, tpr, lw=1, alpha=0.3,
label='Segment %2d(AUC = %0.6f Accuracy = %0.6f)' % 
(i, aucROC[(nom,i)],accuracy[(nom,i)]))
plt.subplot(2, 1, 2)
 plt.step(rec, prec, where='post',
label=f"Segment {i:2d}(AP = {avgPrecRec[(nom,i)]:0.8f})")
 plt.fill_between(rec, prec, step='post', alpha=0.05)
 plt.subplot(2, 1, 1)
 plt.plot([0, 1], [0, 1], linestyle='--', lw=2, color='r',
label='AUC <= 0.5', alpha=.8)
 mean_tpr = np.mean(tprs, axis=0)
 mean_tpr[-1] = 1.0
mean_aucROC = auc(mean_fpr, mean_tpr)
 std_aucROC = np.std(aucROCs)
 moyAucROC[nom], stdAucRoc[nom],moyAucPR[nom], stdAucPR[nom], moyAccuracy[nom], stdAccuracy[nom] = \
aucROCs.mean() ,aucROCs.std() ,aucPRs.mean() ,aucPRs.std() ,scores.mean() ,scores.std()
plt.subplot(2, 1, 1)
 plt.plot(mean_fpr, mean_tpr, color='b',
label=r'Moyenne(AUC = %0.5f $\pm$ %0.5f Accuracy = %0.4f $\pm$ %0.4f)' % 
(mean_aucROC, std_aucROC, scores.mean() , scores.std()),
lw=2, alpha=.8)
 std_tpr = np.std(tprs, axis=0)
 tprs_upper = np.minimum(mean_tpr + std_tpr, 1)
 tprs_lower = np.maximum(mean_tpr - std_tpr, 0)
 plt.fill_between(mean_fpr, tprs_lower, tprs_upper, color='grey', alpha=.4,
label=r'$\pm$ %0.8f std.dev.'%(np.mean(std_tpr)))
 plt.xlim([-0.05, 1.05])
 plt.ylim([-0.05, 1.05])
 plt.xlabel('Le taux de faux Positifs(1 - Specificity)',size=18)
 plt.ylabel('Le taux de vrais positifs(Sensitivity)',size=18)
 plt.title('La courbe ROC '+nom,size=20)
 plt.legend(loc="lower right")
 plt.subplot(2, 1, 2)
 plt.plot([0, 1], [0.5, 0.5], linestyle='--', lw=2, color='r',
label='AC <= 0.5', alpha=.8)
 plt.xlabel('Le Rappel(Recall)= VP /(VP + FN)',size=18)
 plt.ylabel('La Précision = VP /(VP + FP)',size=18)
 plt.title('La courbe précision-rappel '+nom,size=20)
 plt.legend(loc="lower right")
 plt.show()
resultats = pd.DataFrame(pd.Series(aucROC),columns=["aucROC"])
 resultats["avgPrecRec"] = pd.Series(avgPrecRec)
 resultats["accuracy"] = pd.Series(accuracy)
 resultats["log_loss"] = pd.Series(logloss)
 resultats["hamming_loss"] = pd.Series(hammingloss)
 resultats["precision"] = pd.Series(precision)
 resultats["sensibilite"] = pd.Series(sensibilite)
 resultats["f1"] = pd.Series(f1)
 resultats["jaccard_similarity"] = pd.Series(jaccard_similarity)
 resultatsFinaux = pd.concat([resultatsFinaux,resultats])
 return resultatsFinaux,classifieursCV<load_from_csv>
def count_regexp_occ(regexp="", text=None):

 return len(re.findall(regexp, text))
def normal_feature_engineering(text_list):
add_feats_matrix = np.zeros(( len(text_list), 11))
for idx in range(len(text_list)) :
target_text = text_list[idx]
add_feats_matrix[idx, 0] = len(target_text.split(' '))
add_feats_matrix[idx, 1] = len(set(target_text.split(' ')))
 add_feats_matrix[idx, 2] = count_regexp_occ(r'http[s]{0,1}://\S+', target_text)
 add_feats_matrix[idx, 3] = count_regexp_occ(r'[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+', target_text)
 add_feats_matrix[idx, 4] = count_regexp_occ(r'^[A-Z]+[A-Z]$', target_text)
 symbol_cnt = 0
for symbol in symbols_to_isolate:
if symbol in target_text:
symbol_cnt += 1
add_feats_matrix[idx, 5] = symbol_cnt
symbol_delete_cnt = 0
for symbol_del in symbols_to_delete:
if symbol_del in target_text:
symbol_delete_cnt += 1
add_feats_matrix[idx, 6] = symbol_delete_cnt
add_feats_matrix[idx, 7] = add_feats_matrix[idx, 1] / add_feats_matrix[idx, 0]
add_feats_matrix[idx, 8] = add_feats_matrix[idx, 4] / add_feats_matrix[idx, 0]
add_feats_matrix[idx, 9] = add_feats_matrix[idx, 5] / add_feats_matrix[idx, 0]
add_feats_matrix[idx, 10] = add_feats_matrix[idx, 6] / add_feats_matrix[idx, 0]
print('normal feature enginnering done: {}'.format(add_feats_matrix.shape))
return add_feats_matrix
train=pd.read_csv(".. /input/titanic/train.csv")
 print(train.shape)
 train.head()<load_from_csv>
%%time
train_add_feats_matrix = normal_feature_engineering(train["comment_text"].values)
 test_add_feats_matrix = normal_feature_engineering(test["comment_text"].values)
 ADD_FEATS_UNITS = train_add_feats_matrix.shape[1]
print("ADD_FEATS_UNITS: {}".format(ADD_FEATS_UNITS))
train_normal_feats = torch.from_numpy(train_add_feats_matrix)
 test_normal_feats = torch.from_numpy(test_add_feats_matrix)
 del train_add_feats_matrix, test_add_feats_matrix
gc.collect()
test=pd.read_csv(".. /input/titanic/test.csv")
 print(test.shape)
 test.head()<concatenate>
test_lengths = torch.from_numpy(np.array([len(x)for x in x_test]))
x_test_padded = torch.from_numpy(sequence.pad_sequences(x_test, maxlen=maxlen))
donnees = pd.concat([train,test],sort=False)
 donnees.head()<feature_engineering>
batch_size = 512
train_dataset = data.TensorDataset(x_train_padded, train_normal_feats, lengths, y_train_torch)
 test_dataset = data.TensorDataset(x_test_padded, test_normal_feats, test_lengths)
 valid_dataset = data.Subset(train_dataset, indices=[0, 1])
 train_collator = SequenceBucketCollator(lambda lenghts: lenghts.max() , sequence_index=0, length_index=2, normal_feats_idx=1, label_index=3)
 test_collator = SequenceBucketCollator(lambda lenghts: lenghts.max() , sequence_index=0, length_index=2, normal_feats_idx=1)
 train_loader = data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True, collate_fn=train_collator)
 valid_loader = data.DataLoader(valid_dataset, batch_size=batch_size, shuffle=False, collate_fn=train_collator)
 databunch = DataBunch(train_dl=train_loader, valid_dl=valid_loader, collate_fn=train_collator )
donnees['Title'] = donnees.Name.str.extract('([A-Za-z]+)\.', expand=False)
 pd.crosstab(donnees['Title'], donnees['Sex'] )<feature_engineering>
def custom_loss(data, targets):

 bce_loss_1 = nn.BCEWithLogitsLoss(weight=targets[:,1:2] )(data[:,:1],targets[:,:1])
 bce_loss_2 = nn.BCEWithLogitsLoss()(data[:,1:],targets[:,2:])
 return(bce_loss_1 * loss_weight)+ bce_loss_2
donnees['Title'] = donnees['Title'].replace(['Capt','Col','Major','Dr','Rev'], 'Autres')
 donnees['Title'] = donnees['Title'].replace(['Lady', 'Countess', 'Don', 'Sir', 'Jonkheer', 'Dona'], 'Noblesse')
 donnees['Title'] = donnees['Title'].replace('Mlle', 'Miss')
 donnees['Title'] = donnees['Title'].replace('Ms', 'Miss')
 donnees['Title'] = donnees['Title'].replace('Mme', 'Mrs')
 donnees['Sex'] = donnees['Sex'].replace("male","Homme")
 donnees['Sex'] = donnees['Sex'].replace("female","Femme")
 pd.crosstab(donnees['Title'], donnees['Sex'] )<feature_engineering>
all_test_preds = []
for model_idx in range(NUM_MODELS):
print('Model ', model_idx)
 seed_everything(1 + model_idx)
 model = NeuralNet(embedding_matrix, y_aux_train.shape[-1])
 learn = Learner(databunch, model, loss_func=custom_loss)
 test_preds = train_model(learn,test_dataset,output_dim=7)
 all_test_preds.append(test_preds )
<data_type_conversions><EOS> 
submission = pd.DataFrame.from_dict({
'id': test['id'],
'prediction': np.mean(all_test_preds, axis=0)[:, 0]
})
 submission.to_csv('submission.csv', index=False )