Kaggle competition: Titanic - Machine Learning from Disaster
os.environ['TF_DETERMINISTIC_OPS'] = '1'
gc.enable()
np.set_printoptions(suppress=True)
 print('Tensorflow version', tf.__version__)
 print('PyTorch version', torch.__version__)
 print('Transformers version',
transformers.__version__ )<set_options>
path = '/kaggle/input/titanic/'
traindf = pd.read_csv(path + 'train.csv')
 testdf = pd.read_csv(path + 'test.csv')
 submission = pd.read_csv(path + 'gender_submission.csv' )
gpus = tf.config.experimental.list_physical_devices('GPU')
 if gpus:
try:
tf.config.experimental.set_visible_devices(gpus[0], 'GPU')
 logical_gpus = tf.config.experimental.list_logical_devices('GPU')
 print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPU")
 except RuntimeError as e:
print(e)
<define_variables>
import catboost
from catboost import CatBoostClassifier, Pool
from sklearn.model_selection import train_test_split
rand_seed = 20201120
n_splits = 5
BERT_PATH = ".. /input/"
dataset_folder = Path(".. /input/google-quest-challenge")
 MODEL_PATH_list = [
".. /input/tf-roberta-base-exp-v7/",
".. /input/bert-base-uncased-exp-v4/",
".. /input/tf-bert-base-cased-exp-v4/",
".. /input/tf-roberta-base-exp-v4/",
".. /input/xlnet-base-cased-exp-v7/",
]
pretrained_model_metadata = [ 
("tf-roberta-base", True, 56, True), 
("bert-base-uncased", True, 56, False), 
("tf-bert-base-cased", True, 56, False), 
("tf-roberta-base", True, 56, False), 
("xlnet-base-cased", True, 48, True),
]
model_filename_prefix_list = [
"tf-roberta-base_exp_cate_embed",
"bert-base-uncased_exp_split_dense",
"tf-bert-base-cased_exp_split_dense",
"tf-roberta-base_exp_split_dense",
"xlnet-base-cased_exp_cate_embed",
]
MAX_SEQUENCE_LENGTH = 512
max_title_length = 100 
<define_variables>
testdf['Title'] = testdf.Name.apply(lambda name: name.split(',')[1].split('.')[0].strip())
 traindf['Title'] = traindf.Name.apply(lambda name: name.split(',')[1].split('.')[0].strip())
for i, p in enumerate(MODEL_PATH_list):
prefix = model_filename_prefix_list[i]
for f in os.listdir(p):
if f != "dataset-metadata.json":
print(p+f)
 assert prefix in f<load_from_csv>
testdf['Title'].value_counts()
df_train = pd.read_csv(dataset_folder / 'train.csv')
 df_test = pd.read_csv(dataset_folder / 'test.csv')
 df_sub = pd.read_csv(dataset_folder / 'sample_submission.csv')
 print('Train shape:', df_train.shape)
 print('Test shape:', df_test.shape )<feature_engineering>
traindf = traindf[['PassengerId', 'Title', 'Pclass','Sex', 'Fare', 'Age', 'Survived']]
testdf = testdf[['PassengerId', 'Title', 'Pclass','Sex', 'Age', 'Fare']]
def extract_netloc(x):
tokens = x.split(".")
 if len(tokens)> 3:
print(x)
 return ".".join(tokens[:2])
 else:
return tokens[0]
df_train['netloc'] = df_train['host'].apply(lambda x: x.split(".")[0])
 df_test['netloc'] = df_test['host'].apply(lambda x: x.split(".")[0] )<set_options>
X = traindf.drop('Survived', axis = 1)
 y = traindf['Survived']
def set_all_seeds(rand_seed):
np.random.seed(rand_seed)
 random.seed(rand_seed)
 os.environ['PYTHONHASHSEED'] = str(rand_seed)
 tf.random.set_seed(rand_seed)
 torch.manual_seed(rand_seed)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False<set_options>
cat_features_index = np.where(X.dtypes != float)[0]
set_all_seeds(rand_seed )<categorify>
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size =.25, random_state = 42 )
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
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]
def remove_html_special_symbols(x):
html_entities = [ 
("&quot;",<categorify>
model = CatBoostClassifier(
 iterations = 50000,
task_type = 'GPU',
learning_rate =.001,
early_stopping_rounds = 1000,
depth = 10,
loss_function = 'CrossEntropy',
eval_metric = 'BalancedAccuracy' )
def compute_input_arrays(df, columns, tokenizer, max_sequence_length):
input_ids_q, input_masks_q, input_segments_q = [], [], []
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_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_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_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] )<compute_test_metric>
model.fit(X_train, y_train, cat_features = cat_features_index, eval_set =(X_val, y_val), plot = True )
def compute_spearmanr_ignore_nan(trues, preds):
rhos = []
for tcol, pcol in zip(np.transpose(trues), np.transpose(preds)) :
rhos.append(spearmanr(tcol, pcol ).correlation)
 return np.nanmean(rhos)
 def compute_spearmanr(trues, preds):
rhos = []
for tcol, pcol in zip(np.transpose(trues), np.transpose(preds)) :
rhos.append(spearmanr(tcol, pcol ).correlation)
 return np.mean(rhos )<choose_model_class>
submission['PassengerId'] = testdf['PassengerId']
submission['Survived'] = model.predict(testdf, prediction_type='Class')
 submission['Survived'] = submission['Survived'].astype(int)
 submission.to_csv('submission.csv', index = False )
class SpearmanMonitorCallback(tf.keras.callbacks.Callback):
def __init__(self, valid_data, batch_size=16, fold=None):
self.valid_inputs = valid_data[0]
self.valid_outputs = valid_data[1]
self.batch_size = batch_size
self.fold = fold
def on_train_begin(self, logs={}):
self.valid_predictions = []
def on_epoch_end(self, epoch, logs={}):
self.valid_predictions.append(
 self.model.predict(self.valid_inputs, batch_size=self.batch_size))
rho_val = compute_spearmanr(
 self.valid_outputs, np.average(self.valid_predictions, axis=0))
print(f" Fold {self.fold+1} Validation Score: {rho_val:.6f}" )<data_type_conversions>
model.predict(testdf )