Kaggle competition: Jigsaw Unintended Bias in Toxicity Classification
<SOS> metric: JigsawBiasAUC Kaggle data source: jigsaw-unintended-bias-in-toxicity-classification<split>
%matplotlib inline
pd.set_option('max_colwidth',400)
 print(os.listdir(".. /input"))
def set_seed(seed=0):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 set_seed(2411)
 SEED = 42
num_partitions = 10
num_cores = psutil.cpu_count()
print('number of cores:', num_cores)
 def df_parallelize_run(df, func):
df_split = np.array_split(df, num_partitions)
 pool = Pool(num_cores)
 df = pd.concat(pool.map(func, df_split))
pool.close()
pool.join()
return df
train = alldata[alldata['Train'] == 1]
test = alldata[alldata['Train'] == 0] 
<feature_engineering>
tweet = '
<3 ;D :(:-('
print(f'Original Tweet ----
{tweet}')
 tweet = re.sub(r'
print(f'
Tweet after replacing hashtags ----
{tweet}')
 tweet = re.sub(r'(<3|:\*)', ' EMO_POS ', tweet)
 print(f'
Tweet after replacing Emojis for Love with EMP_POS ----
{tweet}')
 tweet = re.sub(r'(;-?\)|;-?D|\(-?;)', ' EMO_POS ', tweet)
 print(f'
Tweet after replacing Emojis for Wink with EMP_POS ----
{tweet}')
 tweet = re.sub(r'(:\s?\(|:-\(|\)\s?:|\)-:)', ' EMO_NEG ', tweet)
 print(f'
Tweet after replacing Emojis for Sad with EMP_NEG ----
{tweet}' )
train['Fare'] = np.log1p(train['Fare'])
 test['Fare'] = np.log1p(test['Fare'] )<categorify>
tweet = re.sub(r'\s+', ' ', tweet)
 print(f'
Tweet after replacing xtra spaces ----
{tweet}')
 tweet = re.sub(r'[^\w\s]','',tweet)
 print(f'
Tweet after replacing Punctuation + with PUNC ----
{tweet}' )
le = LabelEncoder()
le.fit(train['Pclass'])
 train['Pclass'] = le.transform(train['Pclass'])
 ohe = OneHotEncoder(sparse = False, drop = 'first', categories = 'auto')
 ohe.fit(train[['Sex', 'Embarked']])
 ohecategory_train = ohe.transform(train[['Sex', 'Embarked']])
 ohecategory_test = ohe.transform(test[['Sex', 'Embarked']])
 for i in range(ohecategory_train.shape[1]):
train['dummy_variable_' + str(i)] = ohecategory_train[:,i]
for i in range(ohecategory_test.shape[1]):
test['dummy_variable_' + str(i)] = ohecategory_test[:,i]
print('Train shape :', train.shape)
 print('Test shape :', test.shape )<normalization>
positive_emojis = set([
":‑)",":)",":-]",":]",":-3",":3",":->",":>","8-)","8)",":-}",":}",":o)",":c)",":^)","=]","=)",":‑D",":D","8‑D","8D",
"x‑D","xD","X‑D","XD","=D","=3","B^D",":-)) ",";‑)",";)","*-)","*)",";‑]",";]",";^)",":‑,",";D",":‑P",":P","X‑P","XP",
"x‑p","xp",":‑p",":p",":‑Þ",":Þ",":‑þ",":þ",":‑b",":b","d:","=p",">:P", ":'‑)", ":')", ":-*", ":*", ":×"
])
 negative_emojis = set([
":‑(",":(",":‑c",":c",":‑<",":<",":‑[",":[",":-||",">:[",":{",":@",">:(","D‑':","D:<","D:","D8","D;","D=","DX",":‑/",
":/",":‑.",'>:\', ">:/", ":\", "=/" ,"=\", ":L", "=L",":S",":‑|",":|","|‑O","<:‑|"
])
 del positive_emojis, negative_emojis
sc = StandardScaler()
sc.fit(train[['Age', 'SibSp', 'Parch', 'Fare']])
 train[['Age', 'SibSp', 'Parch', 'Fare']] = sc.transform(train[['Age', 'SibSp', 'Parch', 'Fare']])
 test[['Age', 'SibSp', 'Parch', 'Fare']] = sc.transform(test[['Age', 'SibSp', 'Parch', 'Fare']])
<drop_column>
pattern = r'(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])'
train.drop(['PassengerId', 'Name', 'Sex', 'Ticket', 'Embarked', 'LastName', 'Train'], axis = 1, inplace = True)
 test.drop(['PassengerId', 'Name', 'Sex', 'Ticket', 'Embarked', 'LastName', 'Train'], axis = 1, inplace = True )<prepare_x_and_y>
pattern = r'\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b'
X_train = train.iloc[:, 1:].values
y_train = train.iloc[:, 0].values
X_test = test.iloc[:, 1:].values
y_test = test.iloc[:, 0].values
print('X_train :
', X_train[0:5])
 print('y_train :
', y_train[0:5] )<compute_train_metric>
%%time
train = pd.read_csv('.. /input/jigsaw-unintended-bias-in-toxicity-classification/train.csv',usecols=['comment_text', 'target'])
 test = pd.read_csv('.. /input/jigsaw-unintended-bias-in-toxicity-classification/test.csv')
 sub = pd.read_csv('.. /input/jigsaw-unintended-bias-in-toxicity-classification/sample_submission.csv')
 train.shape, test.shape
clf = KNeighborsClassifier(leaf_size = 1, metric = 'minkowski', n_neighbors = 12, p = 1, weights = 'distance')
 accuracies = cross_val_score(clf, X_train, y_train, cv = 10)
 print('Accuracies : ', accuracies)
 print('AVG Accuracies : ', accuracies.mean())
 print('STD:',accuracies.std())
<prepare_output>
spaces = ['\u200b', '\u200e', '\u202a', '\u202c', '\ufeff', '\uf0d8', '\u2061', '\x10', '\x7f', '\x9d', '\xad', '\xa0']
def remove_space(text):

 for space in spaces:
text = text.replace(space, ' ')
 text = text.strip()
text = re.sub('\s+', ' ', text)
 return text
def remove_diacritics(s):
return ''.join(c for c in normalize('NFKD', s.replace('ø', 'o' ).replace('Ø', 'O' ).replace('⁻', '-' ).replace('₋', '-'))
if category(c)!= 'Mn')
 special_punc_mappings = {"—": "-", "–": "-", "_": "-", '”': '"', "″": '"', '“': '"', '•': '.', '−': '-',
"’": "'", "‘": "'", "´": "'", "`": "'", '\u200b': ' ', '\xa0': ' ','،':'','„':'',
'…': '...', '\ufeff': ''}
def clean_special_punctuations(text):
for punc in special_punc_mappings:
if punc in text:
text = text.replace(punc, special_punc_mappings[punc])
 text = remove_diacritics(text)
 return text
def clean_number(text):
if bool(re.search(r'\d', text)) :
text = re.sub(r'(\d+ )([a-zA-Z])', '\g<1> \g<2>', text)
 text = re.sub(r'(\d+ )(th|st|nd|rd)', '\g<1>\g<2> ', text)
 text = re.sub(r'(\d+),(\d+)', '\g<1>\g<2>', text)
 return text
regular_punct = list(string.punctuation)
 extra_punct = [
',', '.', '"', ':', ')', '(', '!', '?', '|', ';', "'", '$', '&',
'/', '[', ']', '>', '%', '=', '
'·', '_', '{', '}', '©', '^', '®', '`', '<', '→', '°', '€', '™', '›',
'♥', '←', '×', '§', '″', '′', 'Â', '█', '½', 'à', '…', '“', '★', '”',
'–', '●', 'â', '►', '−', '¢', '²', '¬', '░', '¶', '↑', '±', '¿', '▾',
'═', '¦', '║', '―', '¥', '▓', '—', '‹', '─', '▒', '：', '¼', '⊕', '▼',
'▪', '†', '■', '’', '▀', '¨', '▄', '♫', '☆', 'é', '¯', '♦', '¤', '▲',
'è', '¸', '¾', 'Ã', '⋅', '‘', '∞', '∙', '）', '↓', '、', '│', '（', '»',
'，', '♪', '╩', '╚', '³', '・', '╦', '╣', '╔', '╗', '▬', '❤', 'ï', 'Ø',
'¹', '≤', '‡', '√', '«', '»', '´', 'º', '¾', '¡', '§', '£', '₤',
':)', ':)', ':-)', '(:', '(:', '(-:', ':')',
':D', ': D', ':-D', 'xD', 'x-D', 'XD', 'X-D',
'<3', ':*',
';-)', ';)', ';-D', ';D', '(;', '(-;',
':-(', ':(', ':(', ''):', ')-:',
'-- :','(', ':'(', ':"('',]
def handle_emojis(text):
text = re.sub(r'(:\s?\)|:-\)|\(\s?:|\(-:|:'\)) ', ' EMO_POS ', text)
 text = re.sub(r'(:\s?D|:-D|x-?D|X-?D)', ' EMO_POS ', text)
 text = re.sub(r'(<3|:\*)', ' EMO_POS ', text)
 text = re.sub(r'(;-?\)|;-?D|\(-?;)', ' EMO_POS ', text)
 text = re.sub(r'(:\s?\(|:-\(|\)\s?:|\)-:)', ' EMO_NEG ', text)
 text = re.sub(r'(:,\(|:'\(|:"\() ', ' EMO_NEG ', text)
 return text
def stop(text):
text = " ".join([w.lower() for w in text.split() ])
 stop_words = stopwords.words('english')
 words = [w for w in text.split() if not w in stop_words]
return " ".join(words)
 all_punct = list(set(regular_punct + extra_punct))
all_punct.remove('-')
 all_punct.remove('.')
 def clean_repeat_words(text):
text = re.sub(r"(I|i )(I|i)+ng", "ing", text)
 text = re.sub(r"(L|l )(L|l )(L|l)+y", "lly", text)
 text = re.sub(r"(A|a )(A|a )(A|a)+", "a", text)
 text = re.sub(r"(C|c )(C|c )(C|c)+", "cc", text)
 text = re.sub(r"(D|d )(D|d )(D|d)+", "dd", text)
 text = re.sub(r"(E|e )(E|e )(E|e)+", "ee", text)
 text = re.sub(r"(F|f )(F|f )(F|f)+", "ff", text)
 text = re.sub(r"(G|g )(G|g )(G|g)+", "gg", text)
 text = re.sub(r"(I|i )(I|i )(I|i)+", "i", text)
 text = re.sub(r"(K|k )(K|k )(K|k)+", "k", text)
 text = re.sub(r"(L|l )(L|l )(L|l)+", "ll", text)
 text = re.sub(r"(M|m )(M|m )(M|m)+", "mm", text)
 text = re.sub(r"(N|n )(N|n )(N|n)+", "nn", text)
 text = re.sub(r"(O|o )(O|o )(O|o)+", "oo", text)
 text = re.sub(r"(P|p )(P|p )(P|p)+", "pp", text)
 text = re.sub(r"(Q|q )(Q|q)+", "q", text)
 text = re.sub(r"(R|r )(R|r )(R|r)+", "rr", text)
 text = re.sub(r"(S|s )(S|s )(S|s)+", "ss", text)
 text = re.sub(r"(T|t )(T|t )(T|t)+", "tt", text)
 text = re.sub(r"(V|v )(V|v)+", "v", text)
 text = re.sub(r"(Y|y )(Y|y )(Y|y)+", "y", text)
 text = re.sub(r"plzz+", "please", text)
 text = re.sub(r"(Z|z )(Z|z )(Z|z)+", "zz", text)
 text = re.sub(r"(-+|\.+)", " ", text)
 return text
def spacing_punctuation(text):

 for punc in all_punct:
if punc in text:
text = text.replace(punc, f' {punc} ')
 return text
def preprocess(text):

 text = remove_space(text)
 text = clean_special_punctuations(text)
 text = handle_emojis(text)
 text = clean_number(text)
 text = spacing_punctuation(text)
 text = clean_repeat_words(text)
 text = remove_space(text)
 return text
mispell_dict = {'😉':'wink','😂':'joy','😀':'stuck out tongue', 'theguardian':'the guardian','deplorables':'deplorable', 'theglobeandmail':'the globe and mail', 'justiciaries': 'justiciary','creditdation': 'Accreditation','doctrne':'doctrine','fentayal': 'fentanyl','designation-': 'designation','CONartist' : 'con-artist','Mutilitated' : 'Mutilated','Obumblers': 'bumblers','negotiatiations': 'negotiations','dood-': 'dood','irakis' : 'iraki','cooerate': 'cooperate','COx':'cox','racistcomments':'racist comments','envirnmetalists': 'environmentalists',}
contraction_mapping = {"ain't": "is not", "aren't": "are not","can't": "cannot", "'cause": "because", "could've": "could have", "couldn't": "could not", "didn't": "did not", "doesn't": "does not", "don't": "do not", "hadn't": "had not", "hasn't": "has not", "haven't": "have not", "he'd": "he would","he'll": "he will", "he's": "he is", "how'd": "how did", "how'd'y": "how do you", "how'll": "how will", "how's": "how is", "I'd": "I would", "I'd've": "I would have", "I'll": "I will", "I'll've": "I will have","I'm": "I am", "I've": "I have", "i'd": "i would", "i'd've": "i would have", "i'll": "i will", "i'll've": "i will have","i'm": "i am", "i've": "i have", "isn't": "is not", "it'd": "it would", "it'd've": "it would have", "it'll": "it will", "it'll've": "it will have","it's": "it is", "let's": "let us", "ma'am": "madam", "mayn't": "may not", "might've": "might have","mightn't": "might not","mightn't've": "might not have", "must've": "must have", "mustn't": "must not", "mustn't've": "must not have", "needn't": "need not", "needn't've": "need not have","o'clock": "of the clock", "oughtn't": "ought not", "oughtn't've": "ought not have", "shan't": "shall not", "sha'n't": "shall not", "shan't've": "shall not have", "she'd": "she would", "she'd've": "she would have", "she'll": "she will", "she'll've": "she will have", "she's": "she is", "should've": "should have", "shouldn't": "should not", "shouldn't've": "should not have", "so've": "so have","so's": "so as", "this's": "this is","that'd": "that would", "that'd've": "that would have", "that's": "that is", "there'd": "there would", "there'd've": "there would have", "there's": "there is", "here's": "here is","they'd": "they would", "they'd've": "they would have", "they'll": "they will", "they'll've": "they will have", "they're": "they are", "they've": "they have", "to've": "to have", "wasn't": "was not", "we'd": "we would", "we'd've": "we would have", "we'll": "we will", "we'll've": "we will have", "we're": "we are", "we've": "we have", "weren't": "were not", "what'll": "what will", "what'll've": "what will have", "what're": "what are", "what's": "what is", "what've": "what have", "when's": "when is", "when've": "when have", "where'd": "where did", "where's": "where is", "where've": "where have", "who'll": "who will", "who'll've": "who will have", "who's": "who is", "who've": "who have", "why's": "why is", "why've": "why have", "will've": "will have", "won't": "will not", "won't've": "will not have", "would've": "would have", "wouldn't": "would not", "wouldn't've": "would not have", "y'all": "you all", "y'all'd": "you all would","y'all'd've": "you all would have","y'all're": "you all are","y'all've": "you all have","you'd": "you would", "you'd've": "you would have", "you'll": "you will", "you'll've": "you will have", "you're": "you are", "you've": "you have" }
def correct_spelling(x, dic):
for word in dic.keys() :
x = x.replace(word, dic[word])
 return x
def correct_contraction(x, dic):
for word in dic.keys() :
x = x.replace(word, dic[word])
 return x
clf.fit(X_train, y_train)
 y_pred = clf.predict(X_test)
 y_pred = y_pred.astype('int64')
 submission = pd.DataFrame()
submission['PassengerId'] = data_test['PassengerId']
submission['Survived'] = y_pred
submission['Survived'].value_counts() 
<save_to_csv>
%%time
tqdm.pandas()
def text_clean_wrapper(df):
df["comment_text"] = df["comment_text"].astype('str' ).transform(preprocess)
 df['comment_text'] = df['comment_text'].transform(lambda x: correct_spelling(x, mispell_dict))
df['comment_text'] = df['comment_text'].transform(lambda x: correct_contraction(x, contraction_mapping))
return df
train = df_parallelize_run(train, text_clean_wrapper)
 test = df_parallelize_run(test, text_clean_wrapper)
 gc.enable()
del mispell_dict, all_punct, special_punc_mappings, regular_punct, extra_punct
gc.collect()
submission.to_csv(r'Submission.csv', index = False, header = True )<import_modules>
ps = PorterStemmer()
lc = LancasterStemmer()
sb = SnowballStemmer("english")
 class Attention(Layer):
def __init__(self, step_dim,
W_regularizer=None, b_regularizer=None,
W_constraint=None, b_constraint=None,
bias=True, **kwargs):
self.supports_masking = True
self.init = initializers.get('glorot_uniform')
 self.W_regularizer = regularizers.get(W_regularizer)
 self.b_regularizer = regularizers.get(b_regularizer)
 self.W_constraint = constraints.get(W_constraint)
 self.b_constraint = constraints.get(b_constraint)
 self.bias = bias
self.step_dim = step_dim
self.features_dim = 0
super(Attention, self ).__init__(**kwargs)
 def build(self, input_shape):
assert len(input_shape)== 3
self.W = self.add_weight(( input_shape[-1],),
initializer=self.init,
name='{}_W'.format(self.name),
regularizer=self.W_regularizer,
constraint=self.W_constraint)
 self.features_dim = input_shape[-1]
if self.bias:
self.b = self.add_weight(( input_shape[1],),
initializer='zero',
name='{}_b'.format(self.name),
regularizer=self.b_regularizer,
constraint=self.b_constraint)
 else:
self.b = None
self.built = True
def compute_mask(self, input, input_mask=None):
return None
def call(self, x, mask=None):
features_dim = self.features_dim
step_dim = self.step_dim
eij = K.reshape(K.dot(K.reshape(x,(-1, features_dim)) ,
K.reshape(self.W,(features_dim, 1))),(-1, step_dim))
if self.bias:
eij += self.b
eij = K.tanh(eij)
 a = K.exp(eij)
 if mask is not None:
a *= K.cast(mask, K.floatx())
 a /= K.cast(K.sum(a, axis=1, keepdims=True)+ K.epsilon() , K.floatx())
 a = K.expand_dims(a)
 weighted_input = x * a
return K.sum(weighted_input, axis=1)
 def compute_output_shape(self, input_shape):
return input_shape[0], self.features_dim
import numpy as np
import pandas as pd
import seaborn as sns
from scipy import stats
import matplotlib.pyplot as plt
import torch
from torch import nn
import torch.optim
from torch.nn import functional as F
from torch.utils.data import TensorDataset, DataLoader
from torch.utils.data.sampler import SubsetRandomSampler
from sklearn.ensemble import RandomForestClassifier<load_from_csv>
start_time = time.time()
print("Loading data...")
 full_text = pd.concat([train['comment_text'].astype(str), test['comment_text'].astype(str)])
 y = train['target']
num_train_data = y.shape[0]
print("--- %s seconds ---" %(time.time() - start_time))
start_time = time.time()
print("Spacy NLP...")
 nlp = spacy.load('en_core_web_lg', disable=['parser','ner','tagger'])
 nlp.vocab.add_flag(lambda s: s.lower() in spacy.lang.en.stop_words.STOP_WORDS, spacy.attrs.IS_STOP)
 word_dict = {}
word_index = 1
lemma_dict = {}
docs = nlp.pipe(full_text, n_threads = 4)
 word_sequences = []
for doc in tqdm(docs):
word_seq = []
for token in doc:
if(token.text not in word_dict)and(token.pos_ is not "PUNCT"):
word_dict[token.text] = word_index
word_index += 1
lemma_dict[token.text] = token.lemma_
if token.pos_ is not "PUNCT":
word_seq.append(word_dict[token.text])
 word_sequences.append(word_seq)
 del docs
gc.collect()
train_word_sequences = word_sequences[:num_train_data]
test_word_sequences = word_sequences[num_train_data:]
print("--- %s seconds ---" %(time.time() - start_time))
train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 train_data.head()<load_from_csv>
max_len = 128
X_train = pad_sequences(train_word_sequences, maxlen = max_len)
 X_test = pad_sequences(test_word_sequences, maxlen = max_len)
 del train_word_sequences, test_word_sequences
gc.collect()
test_data = pd.read_csv('/kaggle/input/titanic/test.csv')
 test_data.head()<filter>
embed_size = 300
women = train_data.loc[train_data.Sex=='female']["Survived"]
rate_women = sum(women)/ len(women)
 F"% of women who survived: {rate_women}"<filter>
def load_glove(word_dict, lemma_dict):
EMBEDDING_FILE = '.. /input/glove840b300dtxt/glove.840B.300d.txt'
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(EMBEDDING_FILE)if len(o)>100 and o.split(" ")[0] in word_dict.keys())
 embed_size = 300
nb_words = len(word_dict)+1
embedding_matrix = np.zeros(( nb_words, embed_size), dtype=np.float32)
 unknown_vector = np.zeros(( embed_size,), dtype=np.float32)- 1.
 print(unknown_vector[:5])
 for key in tqdm(word_dict):
word = key
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None:
embedding_matrix[word_dict[key]] = embedding_vector
continue
word = key.lower()
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None:
embedding_matrix[word_dict[key]] = embedding_vector
continue
word = key.upper()
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None:
embedding_matrix[word_dict[key]] = embedding_vector
continue
word = key.capitalize()
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None:
embedding_matrix[word_dict[key]] = embedding_vector
continue
word = ps.stem(key)
 embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None:
embedding_matrix[word_dict[key]] = embedding_vector
continue
word = lc.stem(key)
 embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None:
embedding_matrix[word_dict[key]] = embedding_vector
continue
word = sb.stem(key)
 embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None:
embedding_matrix[word_dict[key]] = embedding_vector
continue
word = lemma_dict[key]
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None:
embedding_matrix[word_dict[key]] = embedding_vector
continue
embedding_matrix[word_dict[key]] = unknown_vector
return embedding_matrix, nb_words
print("Loading embedding matrix...")
 embedding_matrix, nb_words = load_glove(word_dict, lemma_dict)
 max_features = nb_words
print("--- %s seconds ---" %(time.time() - start_time))
men = train_data.loc[train_data.Sex=='male']['Survived']
men_rate = sum(men)/ len(men)
 F"% of men who survived: {men_rate}"<groupby>
y = np.where(train['target'] >= 0.5, True, False)* 1
fare_mean_1st = train_data[train_data["Pclass"]==1].Fare.mean()
fare_mean_2nd = train_data[train_data["Pclass"]==2].Fare.mean()
fare_mean_3rd = train_data[train_data["Pclass"]==3].Fare.mean()
F"Average cost of tickets for 1st, snd, 3rd classes: \
{fare_mean_1st} || {fare_mean_2nd} || {fare_mean_3rd}" 
<filter>
K.clear_session()
def build_model(lr=0.0, lr_d=0.0, spatial_dr=0.0, dense_units=128, dr=0.1):
file_path = "best_model.hdf5"
check_point = ModelCheckpoint(file_path, monitor = "val_loss", verbose = 1,
save_best_only = True, mode = "min")
 early_stop = EarlyStopping(monitor = "val_loss", mode = "min", patience = 3)
 inp = Input(shape=(max_len,))
x = Embedding(max_features, embed_size, weights=[embedding_matrix], trainable=False )(inp)
 x = SpatialDropout1D(spatial_dr )(x)
 x1 = Bidirectional(CuDNNLSTM(128, return_sequences=True))(x)
 x2 = Bidirectional(CuDNNGRU(128, return_sequences=True))(x1)
 max_pool1 = GlobalMaxPooling1D()(x1)
 max_pool2 = GlobalMaxPooling1D()(x2)
 conc = Concatenate()([max_pool1, max_pool2])
 predictions = Dense(1, activation='sigmoid' )(conc)
 model = Model(inputs=inp, outputs=predictions)
 model.compile(loss = "binary_crossentropy", optimizer = Adam(lr = lr, decay = lr_d), metrics = ["accuracy"])
 history = model.fit(X_train, y, batch_size = 512, epochs = 15, validation_split=0.1,
verbose = 1, callbacks = [check_point, early_stop])
 return model
woman_survived_1st = len(train_data[(train_data["Sex"]=="female")&(train_data["Survived"]==1)&(train_data["Pclass"]==1)].index)/ len(train_data[(train_data["Sex"]=="female")&(train_data["Pclass"]==1)].index)
 woman_survived_2nd = len(train_data[(train_data["Sex"]=="female")&(train_data["Survived"]==1)&(train_data["Pclass"]==2)].index)/ len(train_data[(train_data["Sex"]=="female")&(train_data["Pclass"]==2)].index)
 woman_survived_3rd = len(train_data[(train_data["Sex"]=="female")&(train_data["Survived"]==1)&(train_data["Pclass"]==3)].index)/ len(train_data[(train_data["Sex"]=="female")&(train_data["Pclass"]==3)].index)
 F"Rate of Survival for women in different classes: {woman_survived_1st} || {woman_survived_2nd} || {woman_survived_3rd}"<filter>
model = build_model(lr = 1e-3, lr_d = 0.001, spatial_dr = 0.23, dr=0.2)
 del X_train, embedding_matrix
gc.collect()
woman_survived_1st = len(train_data[(train_data["Sex"]=="male")&(train_data["Survived"]==1)&(train_data["Pclass"]==1)].index)/ len(train_data[(train_data["Sex"]=="male")&(train_data["Pclass"]==1)].index)
 woman_survived_2nd = len(train_data[(train_data["Sex"]=="male")&(train_data["Survived"]==1)&(train_data["Pclass"]==2)].index)/ len(train_data[(train_data["Sex"]=="male")&(train_data["Pclass"]==2)].index)
 woman_survived_3rd = len(train_data[(train_data["Sex"]=="male")&(train_data["Survived"]==1)&(train_data["Pclass"]==3)].index)/ len(train_data[(train_data["Sex"]=="male")&(train_data["Pclass"]==3)].index)
 F"Rate of Survival for men in different classes: {woman_survived_1st} || {woman_survived_2nd} || {woman_survived_3rd}"<count_missing_values>
pred = model.predict(X_test, batch_size = 512, verbose = 1 )
train_data.isnull().sum()<count_missing_values>
sub['prediction'] = pred
sub.to_csv('submission.csv', index=False )
test_data.isnull().sum()<drop_column>
pred = model.predict(X_test, batch_size = 512, verbose = 1)
 plot_model(model, to_file='model.png' )