Kaggle competition: Titanic - Machine Learning from Disaster
sub['question_type_spelling'].value_counts()<save_to_csv>
train, test = pd.read_csv('.. /input/titanic/train.csv'), \
pd.read_csv('.. /input/titanic/test.csv' )
sub.to_csv('submission.csv', index=False )<save_to_csv>
train.isna().sum()
sub.to_csv('submission.csv', index=False )<define_variables>
test.isna().sum()
USE_SAMPLE = False
USE_ONLY_SELECTED_FOLDS = True
selected_folds = [0,1,2]
WEIGHTS = 'softmax'
ROUND = True
USE_LGB = True
USE_XGB = False
USE_3SPLIT_BERT = False
USE_TEST_SAFETY_ADJUSTMENTS = True
beta = 18.5<install_modules>
datasets = [train, test]
for d in datasets:
for i in list(d.columns):
if(d[i].notnull().sum() / d.shape[0] <=.5)\
or i in ['Name', 'Ticket']:
d.drop(i, axis=1, inplace=True)
 elif d[i].dtype == float or train[i].dtype == int:
d[i] = d[i].fillna(d[i].mean())
 else:
d[i] = d[i].fillna(d[i].mode() [0] )
!pip install.. /input/sacremoses0038 > /dev/null
sys.path.insert(0, ".. /input/tokenizers0011/" )<install_modules>
y_train = train['Survived']
x_train, x_test = train.drop('Survived', axis=1), test
x_train.drop('PassengerId', axis=1, inplace=True )
!pip install.. /input/transformers241 > /dev/null --no-dependencies<install_modules>
datasets = [x_train, x_test]
for d in datasets:
d['Embarked'] = d['Embarked'].fillna('S')
 d['Embarked'] = d['Embarked'].map({
'S': 0,
'C': 1,
'Q': 2
})
 d['Sex'] = d['Sex'].map({
'male': 0,
'female': 1
})
 d['FamilySize'] = d['SibSp'] + d['Parch'] + 1
!pip install.. /input/fastparquet/fastparquet-0.3.2-cp36-cp36m-linux_x86_64.whl > /dev/null --no-dependencies<install_modules>
scaler = StandardScaler()
for d in datasets:
for c in ['Age', 'Fare', 'Parch', \
'Pclass', 'SibSp', 'FamilySize']:
d[c] = d[c].astype(float)
 d[c] = scaler.fit_transform(d[c].values.reshape(-1, 1))
!pip install.. /input/fastparquet/thrift-0.13.0-cp36-cp36m-linux_x86_64.whl > /dev/null --no-dependencies<set_options>
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(32, input_dim=x_train.shape[1], activation='relu'),
tf.keras.layers.Dropout(0.4),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
 ])
 model.compile(
 loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
np.set_printoptions(suppress=True)
 print(tf.__version__)
 pd.set_option('display.max_colwidth', 500)
 pd.set_option('display.max_rows', 500)
 pd.set_option('display.max_columns', 500)
 COLOR = 'black'
matplotlib.rcParams['text.color'] = COLOR
matplotlib.rcParams['axes.labelcolor'] = COLOR
matplotlib.rcParams['xtick.color'] = COLOR
matplotlib.rcParams['ytick.color'] = COLOR
pd.set_option('display.max_colwidth', 500)
 pd.set_option('display.max_rows', 500)
 pd.set_option('display.max_columns', 500 )<load_from_csv>
num_epochs = 42
history = model.fit(x_train, y_train, epochs=num_epochs, \
batch_size=50, validation_split = 0.2 )
PATH = '.. /input/google-quest-challenge/'
BERT_PATH = '.. /input/bert-base-uncased-huggingface-transformer/'
GPT2_PATH = '.. /input/gpt2-hugginface-pretrained/'
XLNET_PATH = '.. /input/xlnet-huggingface-pretrained/'
MAX_SEQUENCE_LENGTH = 512
df_train = pd.read_csv(PATH+'train.csv')
 df_test = pd.read_csv(PATH+'test.csv')
 df_sub = pd.read_csv(PATH+'sample_submission.csv')
 print('train shape =', df_train.shape)
 print('test shape =', df_test.shape)
 output_categories = list(df_train.columns[11:])
 input_categories = list(df_train.columns[[1,2,5]])
 print('
output categories:
\t', output_categories)
 print('
input categories:
\t', input_categories)
 if USE_SAMPLE:
df_train = df_train.iloc[0:round(0.05*df_train.shape[0]),:]
df_test = df_test.iloc[0:round(0.1*df_test.shape[0]),:]<load_pretrained>
scores = model.evaluate(x_train, y_train, batch_size=32 )
def save_file(var, name):
pickle.dump(var, open(f"/kaggle/working/{name}.p", "wb"))<categorify>
print(f'Loss: {scores[0]} Accuracy: {scores[1]}' )
def _convert_to_transformer_inputs(title, question, answer, tokenizer, max_sequence_length):

 def return_id(str1, str2, truncation_strategy, length):
inputs = tokenizer.encode_plus(str1, str2,
add_special_tokens=True,
max_length=length,
truncation_strategy=truncation_strategy)
 input_ids = inputs["input_ids"]
input_masks = [1] * len(input_ids)
 input_segments = inputs["token_type_ids"]
padding_length = length - len(input_ids)
 padding_id = tokenizer.pad_token_id if USING_PAD_TOKEN else 0
input_ids = input_ids +([padding_id] * padding_length)
 input_masks = input_masks +([0] * padding_length)
 input_segments = input_segments +([0] * padding_length)
 return [input_ids, input_masks, input_segments]
input_ids_q, input_masks_q, input_segments_q = return_id(title, question, 'longest_first', max_sequence_length)
 input_ids_qa, input_masks_qa, input_segments_qa = return_id(title + ' ' + question, answer, 'longest_first', max_sequence_length)
 input_ids_a, input_masks_a, input_segments_a = return_id(answer, None, 'longest_first', max_sequence_length)
 return [input_ids_q, input_masks_q, input_segments_q,
input_ids_qa, input_masks_qa, input_segments_qa,
input_ids_a, input_masks_a, input_segments_a]
def compute_input_arrays(df, columns, tokenizer, max_sequence_length):
input_ids_q, input_masks_q, input_segments_q = [], [], []
input_ids_qa, input_masks_qa, input_segments_qa = [], [], []
input_ids_a, input_masks_a, input_segments_a = [], [], []
for _, instance in tqdm(df[columns].iterrows()):
t, q, a = instance.question_title, instance.question_body, instance.answer
ids_q, masks_q, segments_q, ids_qa, masks_qa, segments_qa, ids_a, masks_a, segments_a = _convert_to_transformer_inputs(t, q, a, tokenizer, max_sequence_length)
 input_ids_q.append(ids_q)
 input_masks_q.append(masks_q)
 input_segments_q.append(segments_q)
 input_ids_qa.append(ids_qa)
 input_masks_qa.append(masks_qa)
 input_segments_qa.append(segments_qa)
 input_ids_a.append(ids_a)
 input_masks_a.append(masks_a)
 input_segments_a.append(segments_a)
 return [np.asarray(input_ids_q, dtype=np.int32),
np.asarray(input_masks_q, dtype=np.int32),
np.asarray(input_segments_q, dtype=np.int32),
np.asarray(input_ids_qa, dtype=np.int32),
np.asarray(input_masks_qa, dtype=np.int32),
np.asarray(input_segments_qa, dtype=np.int32),
np.asarray(input_ids_a, dtype=np.int32),
np.asarray(input_masks_a, dtype=np.int32),
np.asarray(input_segments_a, dtype=np.int32)]
def compute_output_arrays(df, columns):
return np.asarray(df[columns] )<init_hyperparams>
x_test_without_ids = x_test.drop('PassengerId', axis=1)
 y_predicted = model.predict(x_test_without_ids)
 y_test =(y_predicted > 0.5 ).astype(int ).reshape(x_test_without_ids.shape[0] )
xlnetcfg = {'architectures': ['XLNetLMHeadModel'],
'attn_type': 'bi',
'bi_data': False,
'bos_token_id': 0,
'clamp_len': -1,
'd_head': 64,
'd_inner': 3072,
'd_model': 768,
'do_sample': False,
'dropout': 0.1,
'end_n_top': 5,
'eos_token_ids': 0,
'ff_activation': 'gelu',
'finetuning_task': None,
'id2label': {0: 'LABEL_0', 1: 'LABEL_1'},
'initializer_range': 0.02,
'is_decoder': False,
'label2id': {'LABEL_0': 0, 'LABEL_1': 1},
'layer_norm_eps': 1e-12,
'length_penalty': 1.0,
'max_length': 20,
'mem_len': None,
'model_type': 'xlnet',
'n_head': 12,
'n_layer': 12,
'num_beams': 1,
'num_labels': 2,
'num_return_sequences': 1,
'output_attentions': False,
'output_hidden_states': False,
'output_past': True,
'pad_token_id': 0,
'pruned_heads': {},
'repetition_penalty': 1.0,
'reuse_len': None,
'same_length': False,
'start_n_top': 5,
'summary_activation': 'tanh',
'summary_last_dropout': 0.1,
'summary_type': 'last',
'summary_use_proj': True,
'temperature': 1.0,
'top_k': 50,
'top_p': 1.0,
'torchscript': False,
'untie_r': True,
'use_bfloat16': False,
'vocab_size': 32000}<feature_engineering>
results = pd.DataFrame()
results['PassengerId'] = x_test['PassengerId']
results['Survived'] = y_test
results.to_csv('submission.csv', index=False )