Kaggle competition: Titanic - Machine Learning from Disaster
def create_model(pretrained_model_name):
q_id = tf.keras.layers.Input(( MAX_SEQUENCE_LENGTH,), dtype=tf.int32)
 a_id = tf.keras.layers.Input(( MAX_SEQUENCE_LENGTH,), dtype=tf.int32)
 q_mask = tf.keras.layers.Input(( MAX_SEQUENCE_LENGTH,), dtype=tf.int32)
 a_mask = tf.keras.layers.Input(( MAX_SEQUENCE_LENGTH,), dtype=tf.int32)
 q_atn = tf.keras.layers.Input(( MAX_SEQUENCE_LENGTH,), dtype=tf.int32)
 a_atn = tf.keras.layers.Input(( MAX_SEQUENCE_LENGTH,), dtype=tf.int32)
 pretrained_model = model_class.from_pretrained(BERT_PATH +
f"{pretrained_model_name}")
 q_embedding = pretrained_model(q_id,
attention_mask=q_mask,
token_type_ids=q_atn)[0]
a_embedding = pretrained_model(a_id,
attention_mask=a_mask,
token_type_ids=a_atn)[0]
q = q_embedding[:, 0, :]
a = a_embedding[:, 0, :]
q = tf.keras.layers.Dense(256, activation='relu' )(q)
 a = tf.keras.layers.Dense(256, activation='relu' )(a)
 q = tf.keras.layers.Dense(21, activation='sigmoid' )(q)
 a = tf.keras.layers.Dense(9, activation='sigmoid' )(a)
 x = tf.keras.layers.Concatenate()([q, a])
 model = tf.keras.models.Model(inputs=[
q_id,
q_mask,
q_atn,
a_id,
a_mask,
a_atn,
],
outputs=x)
 return model, pretrained_model<categorify>
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv")
 print("Train shape: ",train.shape)
 print("Test shape: ",test.shape )
def create_model_cate_embed(pretrained_model_name, embed_info):
q_id = tf.keras.layers.Input(( MAX_SEQUENCE_LENGTH,), dtype=tf.int32)
 a_id = tf.keras.layers.Input(( MAX_SEQUENCE_LENGTH,), dtype=tf.int32)
 q_mask = tf.keras.layers.Input(( MAX_SEQUENCE_LENGTH,), dtype=tf.int32)
 a_mask = tf.keras.layers.Input(( MAX_SEQUENCE_LENGTH,), dtype=tf.int32)
 q_atn = tf.keras.layers.Input(( MAX_SEQUENCE_LENGTH,), dtype=tf.int32)
 a_atn = tf.keras.layers.Input(( MAX_SEQUENCE_LENGTH,), dtype=tf.int32)
 pretrained_model = model_class.from_pretrained(BERT_PATH +
f"{pretrained_model_name}")
 if "xlnet" in pretrained_model_name:
q_embedding = pretrained_model(q_id,
attention_mask=q_mask,
token_type_ids=q_atn)[0]
a_embedding = pretrained_model(a_id,
attention_mask=a_mask,
token_type_ids=a_atn)[0]
else:
q_embedding, q_pooler_output = pretrained_model(q_id,
attention_mask=q_mask,
token_type_ids=q_atn)
 a_embedding, a_pooler_output = pretrained_model(a_id,
attention_mask=a_mask,
token_type_ids=a_atn)
 q = q_embedding[:, 0, :]
a = a_embedding[:, 0, :]
host_input = tf.keras.Input(shape=(1,), name="host_input")
 netloc_input = tf.keras.Input(shape=(1,), name="netloc_input")
 cate_input = tf.keras.Input(shape=(1,), name="category_input")
 host_embed_info = embed_info["host"]
host_embed = tf.keras.layers.Embedding(input_dim=host_embed_info[0],
output_dim=host_embed_info[1],
input_length=(1,))(host_input)
 netloc_embed_info = embed_info["netloc"]
netloc_embed = tf.keras.layers.Embedding(input_dim=netloc_embed_info[0],
output_dim=netloc_embed_info[1],
input_length=(1,))(netloc_input)
 cate_embed_info = embed_info["category"]
cate_embed = tf.keras.layers.Embedding(input_dim=cate_embed_info[0],
output_dim=cate_embed_info[1],
input_length=(1,))(cate_input)
 host_embed = tf.keras.layers.Reshape(
 target_shape=(host_embed_info[1],))(host_embed)
 netloc_embed = tf.keras.layers.Reshape(
 target_shape=(netloc_embed_info[1],))(netloc_embed)
 cate_embed = tf.keras.layers.Reshape(
 target_shape=(cate_embed_info[1],))(cate_embed)
 embed_concat = tf.keras.layers.Concatenate()(
 [host_embed, netloc_embed, cate_embed])
 embed_concat = tf.keras.layers.Dense(128, activation='relu' )(embed_concat)
 q_concat = tf.keras.layers.Concatenate()([q, embed_concat])
 q_concat = tf.keras.layers.Dense(256, activation='relu' )(q_concat)
 a_concat = tf.keras.layers.Concatenate()([a, embed_concat])
 a_concat = tf.keras.layers.Dense(256, activation='relu' )(a_concat)
 q_concat = tf.keras.layers.Dense(21, activation='sigmoid' )(q_concat)
 a_concat = tf.keras.layers.Dense(9, activation='sigmoid' )(a_concat)
 x = tf.keras.layers.Concatenate()([q_concat, a_concat])
 model = tf.keras.models.Model(inputs=[
q_id, q_mask, q_atn, a_id, a_mask, a_atn, host_input, netloc_input,
cate_input
],
outputs=x)
 return model, pretrained_model<string_transform>
train.isnull().sum()
set_all_seeds(rand_seed)
 gkf = GroupKFold(n_splits=n_splits ).split(X=df_train.question_body,
groups=df_train.question_body)
 gkf = list(gkf)
 len(gkf )<categorify>
test.isnull().sum()
outputs = compute_output_arrays(df_train, output_categories )<categorify>
datostotales = [train, test]
def optimize_ranks(preds, unique_labels):
new_preds = np.zeros(preds.shape)
 for i in range(preds.shape[1]):
interpolate_bins = np.digitize(preds[:, i],
bins=unique_labels,
right=False)
 if len(np.unique(interpolate_bins)) == 1:
new_preds[:, i] = preds[:, i]
else:
new_preds[:, i] = unique_labels[interpolate_bins]
return new_preds<count_unique_values>
for datatotal in datostotales:
datatotal['Title'] = datatotal['Name'].str.extract('([A-Za-z]+)\.', expand=False )
y_labels = df_train[output_categories].copy()
y_labels = y_labels.values.flatten()
unique_labels = np.array(sorted(np.unique(y_labels)))
 unique_labels<define_variables>
title_mapping = {"Mr": 0, "Miss": 1, "Mrs": 2,
"Master": 3, "Dr": 3, "Rev": 3, "Col": 3, "Major": 3, "Mlle": 3,"Countess": 3,
"Ms": 3, "Lady": 3, "Jonkheer": 3, "Don": 3, "Dona" : 3, "Mme": 3,"Capt": 3,"Sir": 3 }
for datatotal in datostotales:
datatotal['Title'] = datatotal['Title'].map(title_mapping )
denominator = 60
q = np.arange(0, 101, 100 / denominator)
 exp_labels = np.percentile(unique_labels, q)
 exp_labels<define_variables>
train.drop('Name', axis=1, inplace=True)
 test.drop('Name', axis=1, inplace=True )
infer_start_time = time.time()
all_test_preds = []
all_val_preds = []
all_val_scores = []
all_magic_val_scores = []
gc.collect()
for k, MODEL_PATH in enumerate(MODEL_PATH_list):
pretrained_model_name, is_tf, infer_batch_size, cate_embed_mode = pretrained_model_metadata[k]
model_filename_prefix = model_filename_prefix_list[k]
print(f"Generating prediction from fine-tuned {pretrained_model_name}...... ")
 preprocessing_start = time.time()
if is_tf:
model_class = TFAutoModel
tokenizer_class = AutoTokenizer
else:
model_class = AutoModel
tokenizer_class = AutoTokenizer
tokenizer = tokenizer_class.from_pretrained(BERT_PATH +
f"{pretrained_model_name}")
 inputs = compute_input_arrays(df_train, input_categories, tokenizer,
MAX_SEQUENCE_LENGTH)
 test_inputs = compute_input_arrays(df_test, input_categories,
tokenizer, MAX_SEQUENCE_LENGTH)
 print(f"Time spent on preprocessing: {(time.time() -preprocessing_start)/60:,.2f} minutes")
 test_preds = []
val_preds = []
val_scores = []
magic_val_scores = []
for i,(train_idx, valid_idx)in enumerate(gkf):
set_all_seeds(rand_seed)
 print(f"Using {pretrained_model_name} Fold {i+1} {pretrained_model_name} for inference...... ")
 fold_start = time.time()
valid_inputs = [inputs[i][valid_idx] for i in range(len(inputs)) ]
valid_outputs = outputs[valid_idx]
if cate_embed_mode:
embed_info = {}
category_features = {}
def extract_category_ids(train, val, test, c, info):
le = LabelEncoder()
le.fit(train[c])
 val[c] = val[c].map(lambda s: '<unknown>'
if s not in le.classes_ else s)
 test[c] = test[c].map(lambda s: '<unknown>'
if s not in le.classes_ else s)
 le_classes = le.classes_.tolist()
bisect.insort_left(le_classes, '<unknown>')
 le.classes_ = le_classes
train[c + "_label"] = le.transform(train[c])
 val[c + "_label"] = le.transform(val[c])
 test[c + "_label"] = le.transform(test[c])
 no_of_unique_cat = train[c + "_label"].nunique()
embedding_size = min(np.ceil(( no_of_unique_cat)/ 2), 50)
 embedding_size = int(embedding_size)
 vocab_size = no_of_unique_cat + 1
info[c] =(vocab_size, embedding_size)
 print(f"Extracted(vocab_size, embedding_size)for {c}:({vocab_size}, {embedding_size})")
 return val[c + "_label"], test[c + "_label"]
host_val, host_test = extract_category_ids(df_train.iloc[train_idx, :].copy() ,
df_train.iloc[valid_idx, :].copy() ,
df_test.copy() ,
"host",
embed_info)
 netloc_val, netloc_test = extract_category_ids(df_train.iloc[train_idx, :].copy() ,
df_train.iloc[valid_idx, :].copy() ,
df_test.copy() ,
"netloc",
embed_info)
 cate_val, cate_test = extract_category_ids(df_train.iloc[train_idx, :].copy() ,
df_train.iloc[valid_idx, :].copy() ,
df_test.copy() ,
"category",
embed_info)
 valid_inputs.append(host_val)
 valid_inputs.append(netloc_val)
 valid_inputs.append(cate_val)
 submit_inputs = [np.copy(x)for x in test_inputs]
submit_inputs.append(host_test)
 submit_inputs.append(netloc_test)
 submit_inputs.append(cate_test)
 print("Cleaning session...")
 K.clear_session()
print("Loading pretrained model and weights...")
 if cate_embed_mode:
model, pretrained_model = create_model_cate_embed(pretrained_model_name, embed_info)
 else:
model, pretrained_model = create_model(pretrained_model_name)
 model_filename = f"{model_filename_prefix}_fold{i+1}.h5"
model.load_weights(MODEL_PATH + model_filename)
 fold_val_preds = model.predict(valid_inputs,
batch_size=infer_batch_size)
 rho_val = compute_spearmanr(valid_outputs, fold_val_preds)
 print(f"Fold {i+1} Validation Score: {rho_val:.6f}")
 val_preds.append(fold_val_preds)
 val_scores.append(rho_val)
 val_magic_preds = optimize_ranks(fold_val_preds, exp_labels)
 magic_rho_val = compute_spearmanr(valid_outputs, val_magic_preds)
 print(f"Fold {i+1} Magic Validation Score: {magic_rho_val:.6f}")
 magic_val_scores.append(magic_rho_val)
 if cate_embed_mode:
test_preds.append(model.predict(submit_inputs,
batch_size=infer_batch_size))
else:
test_preds.append(model.predict(test_inputs,
batch_size=infer_batch_size))
print(f"Time spent on fold {i+1} inference: {(time.time() -fold_start)/60:,.2f} minutes")
 del model, pretrained_model, valid_inputs, valid_outputs, fold_val_preds, rho_val
gc.collect()
all_test_preds.append(test_preds)
 all_val_preds.append(val_preds)
 all_val_scores.append(val_scores)
 all_magic_val_scores.append(magic_val_scores)
 del tokenizer, inputs, test_inputs
gc.collect()
print(f"Time spent on ensemble inference: {(time.time() -infer_start_time)/60:,.2f} minutes" )<compute_test_metric>
sex_mapping = {"male": 0, "female": 1}
for datatotal in datostotales:
datatotal['Sex'] = datatotal['Sex'].map(sex_mapping )
print(f"Mean Validation Score: {np.mean(all_val_scores):.6f}")
 print(f"Mean Magic Validation Score: {np.mean(all_magic_val_scores):.6f}" )<compute_test_metric>
train["Age"].fillna(train.groupby("Title")["Age"].transform("median"), inplace=True)
 test["Age"].fillna(test.groupby("Title")["Age"].transform("median"), inplace=True )
<predict_on_test>
for datatotal in datostotales:
datatotal.loc[ datatotal['Age'] <= 15, 'Age'] = 0,
datatotal.loc[(datatotal['Age'] > 15)&(datatotal['Age'] <= 35), 'Age'] = 1,
datatotal.loc[(datatotal['Age'] > 35)&(datatotal['Age'] <= 55), 'Age'] = 2,
datatotal.loc[(datatotal['Age'] > 55)&(datatotal['Age'] <= 69), 'Age'] = 3,
datatotal.loc[ datatotal['Age'] > 69, 'Age'] = 4
def val_ensemble_preds(all_val_preds, weights):
oof_preds = np.zeros(outputs.shape)
 for i, model_preds in enumerate(all_val_preds):
for j,(train_idx, valid_idx)in enumerate(gkf):
tmp = np.vstack(model_preds[j])
 oof_preds[valid_idx] += tmp * weights[i]
oof_preds /= np.sum(weights)
 return oof_preds<load_pretrained>
Pclass1 = train[train['Pclass']==1]['Embarked'].value_counts()
Pclass2 = train[train['Pclass']==2]['Embarked'].value_counts()
Pclass3 = train[train['Pclass']==3]['Embarked'].value_counts()
df = pd.DataFrame([Pclass1, Pclass2, Pclass3])
 df.index = ['1st class','2nd class', '3rd class']
df.plot(kind='bar',stacked=True, figsize=(10,5))
with open('ensemble-models-v4-v7.pickle', 'wb')as handle:
pickle.dump(all_val_preds, handle, protocol=pickle.HIGHEST_PROTOCOL )<find_best_params>
for datatotal in datostotales:
datatotal['Embarked'] = datatotal['Embarked'].fillna('Q' )
weights = [1.0, 1.0, 1.0, 1.0, 1.0]
oof_preds = val_ensemble_preds(all_val_preds, weights)
 magic_preds = optimize_ranks(oof_preds, exp_labels)
 blend_score = compute_spearmanr(outputs, magic_preds)
 print(weights, blend_score)
 weights = [2.0, 1.0, 1.0, 1.0, 2.0]
oof_preds = val_ensemble_preds(all_val_preds, weights)
 magic_preds = optimize_ranks(oof_preds, exp_labels)
 blend_score = compute_spearmanr(outputs, magic_preds)
 print(weights, blend_score)
 weights = [2.0, 1.0, 1.0, 1.0, 1.5]
oof_preds = val_ensemble_preds(all_val_preds, weights)
 magic_preds = optimize_ranks(oof_preds, exp_labels)
 blend_score = compute_spearmanr(outputs, magic_preds)
 print(weights, blend_score)
 weights = [1.5, 1.0, 1.0, 1.0, 1.5]
oof_preds = val_ensemble_preds(all_val_preds, weights)
 magic_preds = optimize_ranks(oof_preds, exp_labels)
 blend_score = compute_spearmanr(outputs, magic_preds)
 print(weights, blend_score )<feature_engineering>
embarked_mapping = {"S": 0, "C": 1, "Q": 2}
for datatotal in datostotales:
datatotal['Embarked'] = datatotal['Embarked'].map(embarked_mapping )
submit_preds = [np.average(x, axis=0)for x in all_test_preds]<define_search_space>
train["Fare"].fillna(train.groupby("Pclass")["Fare"].transform("median"), inplace=True)
 test["Fare"].fillna(test.groupby("Pclass")["Fare"].transform("median"), inplace=True )
submit_preds = np.average(submit_preds, weights = [2.0, 1.0, 1.0, 1.0, 1.5],
axis=0 )<compute_test_metric>
for datatotal in datostotales:
datatotal.loc[(datatotal['Fare'] <= 30), 'Fare'] = 0,
datatotal.loc[(datatotal['Fare'] > 30)&(datatotal['Fare'] <= 100), 'Fare'] = 1,
datatotal.loc[(datatotal['Fare'] > 30)&(datatotal['Fare'] <= 100), 'Fare'] = 2,
datatotal.loc[(datatotal['Fare'] > 100), 'Fare'] = 3
submit_preds = optimize_ranks(submit_preds, exp_labels )<save_to_csv>
train.Cabin.value_counts()
df_sub.iloc[:, 1:] = submit_preds
df_sub.to_csv('submission.csv', index=False )<install_modules>
for datatotal in datostotales:
datatotal['Cabin'] = datatotal['Cabin'].str[:1]
!pip install.. /input/huggingface-transformers/sacremoses-master/sacremoses-master
!pip install.. /input/huggingface-transformers/transformers-master/transformers-master<set_options>
cabin_mapping = {"A": 0, "B": 0.4, "C": 0.8, "D": 1.2, "E": 1.6, "F": 2, "G": 2.4, "T": 2.8}
for datatotal in datostotales:
datatotal['Cabin'] = datatotal['Cabin'].map(cabin_mapping )
tqdm.pandas()
warnings.filterwarnings('ignore')
<load_from_csv>
train["Cabin"].fillna(train.groupby("Pclass")["Cabin"].transform("median"), inplace=True)
 test["Cabin"].fillna(test.groupby("Pclass")["Cabin"].transform("median"), inplace=True )
PATH = '.. /input/google-quest-challenge/'
BERT_PATH = '.. /input/bert-base-from-tfhub/bert_en_uncased_L-12_H-768_A-12'
tokenizer_google_qa = tokenization.FullTokenizer(BERT_PATH+'/assets/vocab.txt', True)
 tokenizer2 = BertTokenizer.from_pretrained(BERT_PATH+'/assets/vocab.txt', do_lower_case=True,)
 MAX_SEQUENCE_LENGTH = 512
df_train = pd.read_csv(PATH+'train.csv',header=0,encoding='utf-8')
 df_test = pd.read_csv(PATH+'test.csv',header=0,encoding='utf-8')
 df_sub = pd.read_csv(PATH+'sample_submission.csv')
 df = pd.concat([df_train,df_test],axis=0,ignore_index=True)
 print(f)
 output_categories = ['question_asker_intent_understanding', 'question_body_critical', 'question_conversational', 'question_expect_short_answer', 'question_fact_seeking', 'question_has_commonly_accepted_answer', 'question_interestingness_others', 'question_interestingness_self', 'question_multi_intent', 'question_not_really_a_question', 'question_opinion_seeking', 'question_type_choice', 'question_type_compare', 'question_type_consequence', 'question_type_definition', 'question_type_entity', 'question_type_instructions', 'question_type_procedure', 'question_type_reason_explanation', 'question_type_spelling', 'question_well_written', 'answer_helpful', 'answer_level_of_information', 'answer_plausible', 'answer_relevance', 'answer_satisfaction', 'answer_type_instructions', 'answer_type_procedure', 'answer_type_reason_explanation', 'answer_well_written']
input_categories = ['question_title', 'question_body', 'answer']
print('
output categories:
\t', output_categories)
 print('
input categories:
\t', input_categories )<string_transform>
train["FamilySize"] = train["SibSp"] + train["Parch"] + 1
test["FamilySize"] = test["SibSp"] + test["Parch"] + 1
tree_tokenizer = TreebankWordTokenizer()
def get_tree_tokens(x):
x = tree_tokenizer.tokenize(x)
 x = ' '.join(x)
 return x<feature_engineering>
family_size = {1: 0, 2: 0.4, 3: 0.8, 4: 1.2, 5: 1.6, 6: 2, 7: 2.4, 8: 2.8, 9: 3.2, 10: 3.6, 11: 4}
for datatotal in datostotales:
datatotal['FamilySize'] = datatotal['FamilySize'].map(family_size )
for col in input_categories:
df_train[f'treated_{col}'] = df_train[col].progress_apply(lambda x: get_tree_tokens(x))
df_test[f'treated_{col}'] = df_test[col].progress_apply(lambda x: get_tree_tokens(x))
df[f'treated_{col}'] = df[col].progress_apply(lambda x: get_tree_tokens(x))<choose_model_class>
features_drop = ['Ticket', 'SibSp', 'Parch']
train = train.drop(features_drop, axis=1)
 test = test.drop(features_drop, axis=1)
 train = train.drop(['PassengerId'], axis=1 )
tokenizer = text.Tokenizer(lower=False )<prepare_x_and_y>
train_dfX = train.drop('Survived', axis=1)
 train_dfY = train['Survived']
submission = test[['PassengerId']].copy()
test_df = test.drop(['PassengerId'], axis=1 )
X_train_question = df_train['question_body']
X_train_title = df_train['question_title']
X_train_answer = df_train['answer']
X_test_question = df_test['question_body']
X_test_title = df_test['question_title']
X_test_answer = df_test['answer']<train_model>
categorical = ['Embarked', 'Title', 'Pclass', 'Fare']
for var in categorical:
train_dfX = pd.concat([train_dfX, pd.get_dummies(train_dfX[var], prefix=var)], axis=1)
 del train_dfX[var]
tokenizer.fit_on_texts(list(X_train_title)+list(X_train_question)+list(X_train_answer)+list(X_test_title)+list(X_test_question)+list(X_test_answer))<choose_model_class>
categorical = ['Embarked', 'Title', 'Pclass', 'Fare']
for var in categorical:
test_df = pd.concat([test_df, pd.get_dummies(test_df[var], prefix=var)], axis=1)
 del test_df[var]
nlp = English()
sentencizer = nlp.create_pipe('sentencizer')
 nlp.add_pipe(sentencizer )<string_transform>
sc = StandardScaler()
train_dfX = sc.fit_transform(train_dfX)
 test_df = sc.transform(test_df)
 print("Test shape : ",test_df.shape )
def split_document(texts):
all_sents = []
max_num_sentences = 0.0
for text in texts:
doc = nlp(text)
 sents=[]
for i,sent in enumerate(doc.sents):
sents.append(sent.text)
 all_sents.append(sents)
 return all_sents
X_train_question = split_document(X_train_question)
 X_train_answer = split_document(X_train_answer)
 X_test_question = split_document(X_test_question)
 X_test_answer = split_document(X_test_answer)
<feature_engineering>
train_dfX,val_dfX,train_dfY, val_dfY = train_test_split(train_dfX,train_dfY , test_size=0.10, stratify=train_dfY)
 print("Tamaño set de Entrenamiento: ",train_dfX.shape)
 print("Tamaño set de Validacion : ",val_dfX.shape )
def add_question_metadata_features(text):
doc=nlp(text)
 indirect = 0
choice_words=0
reason_explanation_words = 0
question_count = 0
for sent in doc.sents:
if '?' in sent.text and '?' == sent.text[-1]:
question_count += 1
for token in sent:
if token.text.lower() =='why':
reason_explanation_words+=1
elif token.text.lower() =='or':
choice_words+=1
if question_count==0:
indirect+=1
return np.array([indirect, question_count, reason_explanation_words, choice_words] )<count_duplicates>
def func_model() :
inp = Input(shape=(17,))
x=Dropout(0.1 )(inp)
 x=Dense(350, activation="relu", kernel_regularizer=regularizers.l2(0.01))(inp)
 x=Dropout(0.50 )(x)
 x=Dense(350, activation="relu", kernel_regularizer=regularizers.l2(0.01))(x)
 x=Dropout(0.50 )(x)
 x=Dense(350, activation="relu", kernel_regularizer=regularizers.l2(0.01))(x)
 x=Dropout(0.30 )(x)
 x=Dense(350, activation="relu", kernel_regularizer=regularizers.l2(0.01))(x)
 x=Dropout(0.30 )(x)
 x=Dense(350, activation="relu", kernel_regularizer=regularizers.l2(0.01))(x)
 x=Dropout(0 )(x)
 x=Dense(1, activation="sigmoid" )(x)
 model = Model(inputs=inp, outputs=x)
 model.compile(loss='binary_crossentropy', optimizer='Adam', metrics=['binary_accuracy'])
 return model
ans_user_and_category=df_train[df_train[['answer_user_name', 'category']].duplicated() ][['answer_user_name', 'category']].values
ans_user_and_category.shape<string_transform>
train_history = model.fit(train_dfX, train_dfY, batch_size=64, epochs=epochs, validation_data=(val_dfX, val_dfY))
def question_answer_author_same(df):
q_username = df['question_user_name']
a_username = df['answer_user_name']
author_same=[]
for i in range(len(df)) :
if q_username[i] == a_username[i]:
author_same.append(int(1))
else:
author_same.append(int(0))
return author_same 
<feature_engineering>
print("Tiempo de ejecución %s segundos" %(time.time() - start_time))
def add_external_features(df):
df['question_body'] = df['question_body'].progress_apply(lambda x: str(x))
df['question_body_num_words'] = df['question_body'].str.count('\S+')
 df['answer'] = df['answer'].progress_apply(lambda x: str(x))
df['answer_num_words'] = df['answer'].str.count('\S+')
 df['question_vs_answer_length'] = df['question_body_num_words']/df['answer_num_words']
df['q_a_author_same'] = question_answer_author_same(df)
 answer_user_cat = []
for i in tqdm(df[['answer_user_name', 'category']].values):
if i in ans_user_and_category:
answer_user_cat.append(int(1))
else:
answer_user_cat.append(int(0))
df['answer_user_cat'] = answer_user_cat
handmade_features=[]
for text in df['question_body'].values:
handmade_features.append(add_question_metadata_features(text))
return df, np.array(handmade_features)
<create_dataframe>
y_test = model.predict(test_df)
 submission['Survived'] = np.rint(y_test ).astype(int)
 print(submission)
 submission.to_csv('submission.csv', index=False)