Kaggle competition: Titanic - Machine Learning from Disaster
pred_paragram_test_y = model.predict([test_X], batch_size=1024, verbose=1 )<set_options>
train_data=pd.read_csv('/kaggle/input/titanic/train.csv')
 train_data
del word_index, embeddings_index, all_embs, embedding_matrix, model, inp, x
time.sleep(10 )<find_best_params>
test_data=pd.read_csv('/kaggle/input/titanic/test.csv')
 test_data
pred_val_y =(4 * pred_glove_val_y + pred_fasttext_val_y + 3 * pred_paragram_val_y + 2 * pred_cnn_val_y)/ 10.0
thresholds = []
for thresh in np.arange(0.1, 0.501, 0.01):
thresh = np.round(thresh, 2)
 res = metrics.f1_score(val_y,(pred_val_y > thresh ).astype(int))
thresholds.append([thresh, res])
 print("F1 score at threshold {0} is {1}".format(thresh, res))
thresholds.sort(key=lambda x: x[1], reverse=True)
 best_thresh = thresholds[0][0]
print("Best threshold: ", best_thresh )<save_to_csv>
train_results = train_data["Survived"].copy()
train_data.drop("Survived", axis=1, inplace=True, errors="ignore")
 titanic = pd.concat([train_data, test_data])
 traindex = train_data.index
testdex = test_data.index
pred_test_y =(4 * pred_glove_test_y + pred_fasttext_test_y + 3 * pred_paragram_test_y + 2 * pred_cnn_test_y)/ 10.0
pred_test_y =(pred_test_y > best_thresh ).astype(int)
 out_df = pd.DataFrame({"qid":test_df["qid"].values})
 out_df['prediction'] = pred_test_y
out_df.to_csv("submission.csv", index=False )<import_modules>
titanic[titanic['Cabin']=='B51 B53 B55']
tqdm.pandas()
sns.set_style('whitegrid' )<load_from_csv>
titanic.index=range(len(titanic))
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/test.csv")
 print("Train shape : ",train_df.shape)
 print("Test shape : ",test_df.shape )<categorify>
titanic[pd.isnull(titanic['Fare'])]
def load_embed(file):
def get_coefs(word,*arr):
return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(file, encoding='latin'))
return embeddings_index 
<define_variables>
mean=titanic[titanic['Pclass']==3][titanic['Embarked']=='S'][titanic['Sex']=='male'][titanic['Age']>=40][titanic['SibSp']==0][titanic['Parch']==0]
mean['Fare'].describe()
glove = '.. /input/embeddings/glove.840B.300d/glove.840B.300d.txt' 
<load_pretrained>
titanic[['Fare']]=titanic[['Fare']].fillna(value=7.69 )
embed_glove = load_embed(glove)
<feature_engineering>
titanic.isnull().sum()
def build_vocab(texts):
sentences = texts.apply(lambda x: x.split() ).values
vocab = {}
for sentence in sentences:
for word in sentence:
try:
vocab[word] += 1
except KeyError:
vocab[word] = 1
return vocab<define_variables>
titanic["Title"] = titanic.Name.str.extract('([A-Za-z]+)\.', expand=False)
 pd.crosstab(titanic['Title'], titanic['Sex'])
def check_coverage(vocab, embeddings_index):
known_words = {}
unknown_words = {}
nb_known_words = 0
nb_unknown_words = 0
for word in vocab.keys() :
try:
known_words[word] = embeddings_index[word]
nb_known_words += vocab[word]
except:
unknown_words[word] = vocab[word]
nb_unknown_words += vocab[word]
pass
print('Se encontraron embeddings para {:.3%} of vocab'.format(len(known_words)/ len(vocab)))
 unknown_words = sorted(unknown_words.items() , key=operator.itemgetter(1)) [::-1]
return unknown_words<concatenate>
titanic['Title'] = titanic['Title'].replace('Mlle', 'Miss')
 titanic['Title'] = titanic['Title'].replace('Ms', 'Miss')
 titanic['Title'] = titanic['Title'].replace('Mme', 'Mrs')
df = pd.concat([train_df ,test_df])
 vocab = build_vocab(df['question_text'] )<compute_test_metric>
titanic['Title'] = titanic['Title'].replace(['Lady', 'Countess','Capt','Col','Don', 'Dr', 'Major','Rev', 'Sir', 'Jonkheer', 'Dona'], 'Not married' )
print("Glove : ")
 oov_glove = check_coverage(vocab, embed_glove )<feature_engineering>
titanic['Title'] = titanic['Title'].replace(['Mr', 'Mrs'], 'Married' )
df['question_text'] = df['question_text'].apply(lambda x: x.lower() )<categorify>
titanic["Surname"] = titanic.Name.str.split(',' ).str.get(0 )
def add_lower(embedding, vocab):
count = 0
for word in vocab:
if word in embedding and word.lower() not in embedding:
embedding[word.lower() ] = embedding[word]
count += 1
print(f"Anadidas {count} palabras al embedding" )<categorify>
titanic=titanic.drop(['Name'],axis=1 )
print("Glove : ")
 oov_glove = check_coverage(vocab, embed_glove)
 add_lower(embed_glove, vocab)
 oov_glove = check_coverage(vocab, embed_glove )<define_variables>
titanic.Surname.value_counts()
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",
"who'd": "who would",
"who're": "who are",
"'re": " are",
"tryin'": "trying",
"doesn'": "does not",
'howdo': 'how do',
'whatare': 'what are',
'howcan': 'how can',
'howmuch': 'how much',
'howmany': 'how many',
'whydo': 'why do',
'doI': 'do I',
'theBest': 'the best',
'howdoes': 'how does',
}<define_variables>
titanic['SurnameFreq']=titanic.groupby('Surname')['Surname'].transform('count')
def known_contractions(embed):
known = []
for contract in contraction_mapping:
if contract in embed:
known.append(contract)
 return known<import_modules>
titanic.Ticket.value_counts()
print("- Contracciones Conocidas -")
 print(" Glove :")
 print(known_contractions(embed_glove))<string_transform>
titanic['TicketFreq']=titanic.groupby('Ticket')['Ticket'].transform('count')
def clean_contractions(text, mapping):
specials = ["’", "‘", "´", "`"]
for s in specials:
text = text.replace(s, "'")
 text = ' '.join([mapping[t] if t in mapping else t for t in text.split(" ")])
 return text<drop_column>
titanic['customizedFare']=titanic.Fare/(titanic.TicketFreq*titanic.Pclass )
df['question_text'] = df['question_text'].apply(lambda x: clean_contractions(x, contraction_mapping))<drop_column>
titanic.isnull().sum()
def unknown_punct(embed, punct):
unknown = ''
for p in punct:
if p not in embed:
unknown += p
unknown += ' '
return unknown<define_variables>
titanic.loc[(titanic.Age.isnull())&(titanic.Title=='Master'),'Age']=int(4.0 )
punct_mapping = {"‘": "'", "₹": "e", "´": "'", "°": "", "€": "e", "™": "tm", "√": " sqrt ", "×": "x", "²": "2", "—": "-", "–": "-", "’": "'", "_": "-", "`": "'", '“': '"', '”': '"', '“': '"', "£": "e", '∞': 'infinity', 'θ': 'theta', '÷': '/', 'α': 'alpha', '•': '.', 'à': 'a', '−': '-', 'β': 'beta', '∅': '', '³': '3', 'π': 'pi', }<categorify>
titanic.loc[(titanic.Age.isnull())&(titanic.Sex=='male'),'Age']=int(30.0 )
def clean_special_chars(text, punct, mapping):
for p in mapping:
text = text.replace(p, mapping[p])
 for p in punct:
text = text.replace(p, f' {p} ')
 specials = {'\u200b': ' ', '…': '...', '\ufeff': '', 'करना': '', 'है': ''}
for s in specials:
text = text.replace(s, specials[s])
 return text<feature_engineering>
titanic.loc[(titanic.Age.isnull())&(titanic.Sex=='female'),'Age']=int(27.0 )
df['question_text'] = df['question_text'].apply(lambda x: clean_special_chars(x, punct, punct_mapping))<compute_test_metric>
vocab = build_vocab(df['question_text'])
 print("Glove : ")
 oov_glove = check_coverage(vocab, embed_glove )<define_variables>
titanic.isnull().sum()
mispell_dict = {'advanatges': 'advantages',
'irrationaol': 'irrational' ,
'defferences': 'differences',
'lamboghini':'lamborghini',
'hypothical':'hypothetical',
'colour': 'color',
'centre': 'center',
'favourite': 'favorite',
'travelling': 'traveling',
'counselling': 'counseling',
'theatre': 'theater',
'cancelled': 'canceled',
'labour': 'labor',
'organisation': 'organization',
'wwii': 'world war 2',
'citicise': 'criticize',
'youtu ': 'youtube ',
'Qoura': 'Quora',
'sallary': 'salary',
'Whta': 'What',
'narcisist': 'narcissist',
'howdo': 'how do',
'whatare': 'what are',
'howcan': 'how can',
'howmuch': 'how much',
'howmany': 'how many',
'whydo': 'why do',
'doI': 'do I',
'theBest': 'the best',
'howdoes': 'how does',
'mastrubation': 'masturbation',
'mastrubate': 'masturbate',
"mastrubating": 'masturbating',
'pennis': 'penis',
'Etherium': 'Ethereum',
'narcissit': 'narcissist',
'bigdata': 'big data',
'2k17': '2017',
'2k18': '2018',
'qouta': 'quota',
'exboyfriend': 'ex boyfriend',
'airhostess': 'air hostess',
"whst": 'what',
'watsapp': 'whatsapp',
'demonitisation': 'demonetization',
'demonitization': 'demonetization',
'demonetisation': 'demonetization',
'pokémon': 'pokemon'}<categorify>
titanic['Embarked'].fillna('S',inplace=True)
def correct_spelling(x, dic):
for word in dic.keys() :
x = x.replace(word, dic[word])
 return x<feature_engineering>
titanic.isnull().sum()
df['question_text'] = df['question_text'].apply(lambda x: correct_spelling(x, mispell_dict)) 
<compute_test_metric>
titanic['Family']=titanic['SibSp']+titanic['Parch']+1
titanic=titanic.drop(['SibSp','Parch'],axis=1 )
vocab = build_vocab(df['question_text'])
 print("Glove : ")
 oov_glove = check_coverage(vocab, embed_glove )<categorify>
titanic.SurnameFreq=titanic.TicketFreq
def clean_numbers(x):
x = re.sub('[0-9]{5,}', ' number ', x)
 x = re.sub('[0-9]{4}', ' number ', x)
 x = re.sub('[0-9]{3}', ' number ', x)
 x = re.sub('[0-9]{2}', ' number ', x)
 return x<feature_engineering>
titanic['Deck']=titanic['Cabin'].notnull().astype(str ).str[0]
titanic['Deck'].value_counts()
df['question_text'] = df['question_text'].apply(lambda x: clean_numbers(x))<compute_test_metric>
titanic=titanic.drop(['Cabin'],axis=1 )
vocab = build_vocab(df['question_text'])
 print("Glove : ")
 oov_glove = check_coverage(vocab, embed_glove )<feature_engineering>
train_df['treated_question'] = train_df['question_text'].apply(lambda x: x.lower())
 train_df['treated_question'] = train_df['treated_question'].apply(lambda x: clean_contractions(x, contraction_mapping))
train_df['treated_question'] = train_df['treated_question'].apply(lambda x: clean_special_chars(x, punct, punct_mapping))
train_df['treated_question'] = train_df['treated_question'].apply(lambda x: correct_spelling(x, mispell_dict))
train_df['treated_question'] = train_df['treated_question'].apply(lambda x: clean_numbers(x))<feature_engineering>
def FamilyGroup(family):
a=''
if family<=1:
a='Single'
elif family<=4:
a='Small'
else:
a='Large'
return a
titanic['FamilyGroup']=titanic['Family'].map(FamilyGroup)
 titanic=titanic.drop(['Family'],axis=1 )
test_df['treated_question'] = test_df['question_text'].apply(lambda x: x.lower())
 test_df['treated_question'] = test_df['treated_question'].apply(lambda x: clean_contractions(x, contraction_mapping))
test_df['treated_question'] = test_df['treated_question'].apply(lambda x: clean_special_chars(x, punct, punct_mapping))
test_df['treated_question'] = test_df['treated_question'].apply(lambda x: correct_spelling(x, mispell_dict))
test_df['treated_question'] = test_df['treated_question'].apply(lambda x: clean_numbers(x))<feature_engineering>
def AgeGroup(age):
a=''
if age<=15:
a='Child'
elif age<=30:
a='Young'
elif age<=50:
a='Adult'
else:
a='Old'
return a
titanic['AgeGroup']=titanic['Age'].map(AgeGroup)
 titanic=titanic.drop(['Age'],axis=1 )
<split>
titanic=titanic.drop(['PassengerId','TicketFreq','Ticket','Fare','Title','Surname'], axis=1 )
train, val = train_test_split(train_df, test_size=0.2, random_state=2)
<prepare_x_and_y>
titanic_data=pd.get_dummies(titanic,columns=['Embarked','AgeGroup','Sex','Deck','FamilyGroup'] )
xtrain = train['question_text'].fillna('_na_' ).values
xval = val['question_text'].fillna('_na_' ).values
xtest = test_df['question_text'].fillna('_na_' ).values<string_transform>
titanic_data.loc[891:1308]
EMBED_SIZE = 300
MAX_FEATURES = 100000
MAXLEN = 60
tokenizer = Tokenizer(num_words=MAX_FEATURES)
 tokenizer.fit_on_texts(list(xtrain))
xtrain = tokenizer.texts_to_sequences(xtrain)
 xval = tokenizer.texts_to_sequences(xval)
 xtest = tokenizer.texts_to_sequences(xtest )<string_transform>
train_df = titanic_data.loc[0:890]
train_df['Survived'] = train_results
test_df = titanic_data.loc[891:1308]
xtrain = pad_sequences(xtrain, maxlen=MAXLEN)
 xval = pad_sequences(xval, maxlen=MAXLEN)
 xtest = pad_sequences(xtest, maxlen=MAXLEN )<prepare_x_and_y>
X=train_df.drop(['Survived'],axis=1)
 X.head()
y=train_df.Survived
y.head()
ytrain = train['target'].values
yval = val['target'].values<statistical_test>
def load_glove_matrix(word_index, embeddings_index):
all_embs = np.stack(embeddings_index.values())
 emb_mean, emb_std = all_embs.mean() , all_embs.std()
EMBED_SIZE = all_embs.shape[1]
nb_words = min(MAX_FEATURES, len(word_index))
embedding_matrix = np.random.normal(emb_mean, emb_std,(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
return embedding_matrix 
<prepare_x_and_y>
xgbr=XGBClassifier(n_estimators=2800,
min_child_weight=0.1,
learning_rate=0.002,
max_depth=2,
subsample=0.47,
colsample_bytree=0.35,
gamma=0.4,
reg_lambda=0.4,
random_state=42,
n_jobs=-1,)
 xgbr.fit(X,y)
 predicts=xgbr.predict(test_df)
np.random.seed(2)
 trn_idx = np.random.permutation(len(xtrain))
val_idx = np.random.permutation(len(xval))
xtrain = xtrain[trn_idx]
ytrain = ytrain[trn_idx]
xval = xval[val_idx]
yval = yval[val_idx]
embedding_matrix_glove = load_glove_matrix(tokenizer.word_index, embed_glove )<set_options>
submission = pd.DataFrame({
"PassengerId": test_data["PassengerId"],
"Survived": predicts
})
 submission.Survived = submission.Survived.round().astype("int")
 submission.to_csv('titanic.csv', index=False)
 print("Submitted Successfully")