Kaggle competition: Ghouls, Goblins, and Ghosts... Boo!
train = pd.read_csv(f'{DATA_DIR}/train.csv')
 train.head()<load_from_csv>
train = pd.read_csv("/kaggle/input/data-ghoul/train.csv")
 test = pd.read_csv("/kaggle/input/data-ghoul/test.csv")
 IDtest = test["id"]
test = pd.read_csv(f'{DATA_DIR}/test.csv')
 test.head()<set_options>
def detect_outliers(df,n,features):
outlier_indices = []
for col in features:
Q1 = np.percentile(df[col], 25)
 Q3 = np.percentile(df[col],75)
 IQR = Q3 - Q1
outlier_step = 1.5 * IQR
outlier_list_col = df[(df[col] < Q1 - outlier_step)|(df[col] > Q3 + outlier_step)].index
outlier_indices.extend(outlier_list_col)
 outlier_indices = Counter(outlier_indices)
 multiple_outliers = list(k for k, v in outlier_indices.items() if v > n)
 return multiple_outliers
Outliers_to_drop = detect_outliers(train,2,["bone_length","rotting_flesh","hair_length","has_soul"] )
MAX_LEN = 512
SEP_TOKEN_ID = 102
class QuestDataset(torch.utils.data.Dataset):
def __init__(self, df, model_type="bert-base-cased", max_len=512, content="Question_Answer", train_mode=True, labeled=True):
self.df = df
self.train_mode = train_mode
self.labeled = labeled
self.max_len = max_len
self.content = content
bert_tokenizer_path = '.. /input/pretrained-bert-models-for-pytorch/' + model_type + '-vocab.txt'
xlnet_tokenizer_path = '.. /input/xlnet-pretrained-models-pytorch/' + model_type + '-spiece.model'
roberta_tokenizer_path = '.. /input/roberta-transformers-pytorch/roberta-base/vocab.json'
roberta_tokenizer_merges_file = '.. /input/roberta-transformers-pytorch/roberta-base/merges.txt'
if model_type == "bert-base-uncased":
self.tokenizer = BertTokenizer.from_pretrained(bert_tokenizer_path)
 elif model_type == "bert-base-cased":
self.tokenizer = BertTokenizer.from_pretrained(bert_tokenizer_path)
 elif model_type == "xlnet-base-cased":
self.tokenizer = XLNetTokenizer.from_pretrained(xlnet_tokenizer_path)
 elif model_type == "roberta-base":
self.tokenizer = RobertaTokenizer(vocab_file=roberta_tokenizer_path, merges_file=roberta_tokenizer_merges_file)
 def __getitem__(self, index):
row = self.df.iloc[index]
token_ids, seg_ids = self.get_token_ids(row)
 if self.labeled:
labels = self.get_label(row)
 return token_ids, seg_ids, labels
else:
return token_ids, seg_ids
def __len__(self):
return len(self.df)
 def select_tokens(self, tokens, max_num):
if len(tokens)<= max_num:
return tokens
if self.train_mode:
num_remove = len(tokens)- max_num
remove_start = random.randint(0, len(tokens)-num_remove-1)
 return tokens[:remove_start] + tokens[remove_start + num_remove:]
else:
return tokens[:max_num//2] + tokens[-(max_num - max_num//2):]
def trim_input_single_content(self, title, content, max_sequence_length=512,
t_max_len=30, c_max_len=512-30-4, num_token=3):
content = html.unescape(content)
 title = html.unescape(title)
 t = self.tokenizer.tokenize(title)
 c = self.tokenizer.tokenize(content)
 t_len = len(t)
 c_len = len(c)
 if(t_len+c_len+num_token)> max_sequence_length:
if t_max_len > t_len:
t_new_len = t_len
c_max_len = c_max_len + floor(( t_max_len - t_len)/2)
 else:
t_new_len = t_max_len
if c_max_len > c_len:
c_new_len = c_len
else:
c_new_len = c_max_len
if t_new_len+c_new_len+num_token > max_sequence_length:
raise ValueError("New sequence length should be less or equal than %d, but is %d"
%(max_sequence_length,(t_new_len+c_new_len+num_token)))
 if len(t)- t_new_len > 0:
t = t[:t_new_len//4] + t[len(t)-t_new_len+t_new_len//4:]
else:
t = t[:t_new_len]
if len(c)- c_new_len > 0:
c = c[:c_new_len//4] + c[len(c)-c_new_len+c_new_len//4:]
else:
c = c[:c_new_len]
if(len(t)+ len(c)+ num_token > max_sequence_length):
more_token = len(t)+ len(c)+ num_token - max_sequence_length
c = c[:(len(c)-more_token)]
return t, c
def trim_input(self, title, question, answer, max_sequence_length=MAX_LEN,
t_max_len=30, q_max_len=239, a_max_len=239, num_token=4):
question = html.unescape(question)
 answer = html.unescape(answer)
 title = html.unescape(title)
 t = self.tokenizer.tokenize(title)
 q = self.tokenizer.tokenize(question)
 a = self.tokenizer.tokenize(answer)
 t_len = len(t)
 q_len = len(q)
 a_len = len(a)
 if(t_len+q_len+a_len+num_token)> max_sequence_length:
if t_max_len > t_len:
t_new_len = t_len
a_max_len = a_max_len + floor(( t_max_len - t_len)/2)
 q_max_len = q_max_len + ceil(( t_max_len - t_len)/2)
 else:
t_new_len = t_max_len
if a_max_len > a_len:
a_new_len = a_len
q_new_len = q_max_len +(a_max_len - a_len)
 elif q_max_len > q_len:
a_new_len = a_max_len +(q_max_len - q_len)
 q_new_len = q_len
else:
a_new_len = a_max_len
q_new_len = q_max_len
if t_new_len+a_new_len+q_new_len+num_token > max_sequence_length:
raise ValueError("New sequence length should be %d, but is %d"
%(max_sequence_length,(t_new_len+a_new_len+q_new_len+num_token)))
 if len(t)- t_new_len > 0:
t = t[:t_new_len//4] + t[len(t)-t_new_len+t_new_len//4:]
else:
t = t[:t_new_len]
if len(q)- q_new_len > 0:
q = q[:q_new_len//4] + q[len(q)-q_new_len+q_new_len//4:]
else:
q = q[:q_new_len]
if len(a)- a_new_len > 0:
a = a[:a_new_len//4] + a[len(a)-a_new_len+a_new_len//4:]
else:
a = a[:a_new_len]
return t, q, a
def get_token_ids(self, row):
num_token = 4
if self.content == "Question":
num_token -= 1
elif self.content == "Answer":
num_token -= 1
if self.content == "Question_Answer":
t_max_len=30
q_max_len=int(( self.max_len-t_max_len-num_token)/2)
 a_max_len=(self.max_len-t_max_len - num_token - int(( self.max_len-t_max_len-num_token)/2))
elif self.content == "Question":
t_max_len=30
q_max_len=self.max_len-t_max_len-num_token
a_max_len=0
elif self.content == "Answer":
t_max_len=30
q_max_len=0
a_max_len=self.max_len-t_max_len-num_token
else:
raise NotImplementedError
if self.content == "Question_Answer":
t_tokens, q_tokens, a_tokens = self.trim_input(row.question_title, row.question_body, row.answer, max_sequence_length=self.max_len, \
t_max_len=t_max_len, q_max_len=q_max_len, a_max_len=a_max_len, num_token=num_token)
 elif self.content == "Question":
t_tokens, c_tokens = self.trim_input_single_content(row.question_title, row.question_body, max_sequence_length=self.max_len, \
t_max_len=t_max_len, c_max_len=q_max_len, num_token=num_token)
 elif self.content == "Answer":
t_tokens, c_tokens = self.trim_input_single_content(row.question_title, row.answer, max_sequence_length=self.max_len, \
t_max_len=t_max_len, c_max_len=a_max_len, num_token=num_token)
 else:
raise NotImplementedError
if self.content == "Question_Answer":
tokens = ['[CLS]'] + t_tokens + ['[SEP]'] + q_tokens + ['[SEP]'] + a_tokens + ['[SEP]']
elif(( self.content == "Question")or(self.content == "Answer")) :
tokens = ['[CLS]'] + t_tokens + ['[SEP]'] + c_tokens + ['[SEP]']
else:
raise NotImplementedError
token_ids = self.tokenizer.convert_tokens_to_ids(tokens)
 if len(token_ids)< self.max_len:
token_ids += [0] *(self.max_len - len(token_ids))
ids = torch.tensor(token_ids)
 seg_ids = self.get_seg_ids(ids)
 return ids, seg_ids
def get_seg_ids(self, ids):
seg_ids = torch.zeros_like(ids)
 seg_idx = 0
first_sep = True
for i, e in enumerate(ids):
seg_ids[i] = seg_idx
if e == self.tokenizer.sep_token_id:
if first_sep:
first_sep = False
else:
seg_idx = 1
pad_idx = torch.nonzero(ids == 0)
 seg_ids[pad_idx] = 0
return seg_ids
def get_label(self, row):
return torch.tensor(row[TARGET_COLUMNS].values.astype(np.float32))
def collate_fn(self, batch):
token_ids = torch.stack([x[0] for x in batch])
 seg_ids = torch.stack([x[1] for x in batch])
 if self.labeled:
labels = torch.stack([x[2] for x in batch])
 return token_ids, seg_ids, labels
else:
return token_ids, seg_ids
def get_test_loader(model_type="bert-base-cased", max_len=512, content="Question_Answer", batch_size=4):
df = pd.read_csv(f'{DATA_DIR}/test.csv')
 ds_test = QuestDataset(df, model_type, max_len=max_len, content=content, train_mode=False, labeled=False)
 loader = torch.utils.data.DataLoader(ds_test, batch_size=batch_size, shuffle=False, num_workers=2, collate_fn=ds_test.collate_fn, drop_last=False)
 loader.num = len(df)
 return loader, ds_test.tokenizer
def get_train_val_loaders(model_type="bert-base-cased", max_len=512, content="Question_Answer", batch_size=4, val_batch_size=4, ifold=0):
df = pd.read_csv(f'{DATA_DIR}/train.csv')
 df = shuffle(df, random_state=42)
 gkf = GroupKFold(n_splits=5 ).split(X=df.question_body, groups=df.question_body)
 for fold,(train_idx, valid_idx)in enumerate(gkf):
if fold == ifold:
df_train = df.iloc[train_idx]
df_val = df.iloc[valid_idx]
break
print(df_train.shape)
 print(df_val.shape)
 ds_train = QuestDataset(df_train, model_type, max_len=max_len, content=content)
 train_loader = torch.utils.data.DataLoader(ds_train, batch_size=batch_size, shuffle=True, num_workers=2, collate_fn=ds_train.collate_fn, drop_last=True)
 train_loader.num = len(df_train)
 ds_val = QuestDataset(df_val, model_type, max_len=max_len, content=content, train_mode=False)
 val_loader = torch.utils.data.DataLoader(ds_val, batch_size=val_batch_size, shuffle=False, num_workers=2, collate_fn=ds_val.collate_fn, drop_last=False)
 val_loader.num = len(df_val)
 val_loader.df = df_val
return train_loader, val_loader, ds_train.tokenizer
def test_train_loader() :
loader, _, _ = get_train_val_loaders("xlnet-base-cased", 512, "Question", 4, 4, 1)
 for ids, seg_ids, labels in loader:
print(ids)
 print(seg_ids.numpy())
 print(labels)
 break
def test_test_loader() :
loader, _ = get_test_loader("roberta-base", 512, "Question", 4)
 for ids, seg_ids in loader:
print(ids)
 print(seg_ids)
 break<load_pretrained>
train.loc[Outliers_to_drop]
test_test_loader()<load_pretrained>
train_len = len(train)
 dataset = pd.concat(objs=[train, test], axis=0 ).reset_index(drop=True )
test_train_loader()<load_pretrained>
dataset = dataset.fillna(np.nan)
 dataset.isnull().sum()
class QuestModel(nn.Module):
def __init__(self, model_type="xlnet-base-cased", tokenizer=None, n_classes=30, hidden_layers=[-1, -3, -5, -7, -9]):
super(QuestModel, self ).__init__()
self.model_name = 'QuestModel'
self.model_type = model_type
self.hidden_layers = hidden_layers
if model_type == "bert-base-uncased":
bert_model_config = '.. /input/pretrained-bert-models-for-pytorch/bert-base-uncased/bert_config.json'
bert_config = BertConfig.from_json_file(bert_model_config)
 bert_config.output_hidden_states = True
model_path = os.path.join('.. /input/pretrained-bert-models-for-pytorch/' + model_type)
 self.bert_model = BertModel.from_pretrained(model_path, config=bert_config)
 elif model_type == "bert-base-cased":
bert_model_config = '.. /input/pretrained-bert-models-for-pytorch/bert-base-cased/bert_config.json'
bert_config = BertConfig.from_json_file(bert_model_config)
 bert_config.output_hidden_states = True
model_path = os.path.join('.. /input/pretrained-bert-models-for-pytorch/' + model_type)
 self.bert_model = BertModel.from_pretrained(model_path, config=bert_config)
 elif model_type == "xlnet-base-cased":
xlnet_model_config = '.. /input/xlnet-pretrained-models-pytorch/xlnet-base-cased-config.json'
xlnet_config = XLNetConfig.from_json_file(xlnet_model_config)
 xlnet_config.output_hidden_states = True
xlnet_config.hidden_dropout_prob = 0
model_path = os.path.join('.. /input/xlnet-pretrained-models-pytorch/' + model_type + '-pytorch_model.bin')
 self.xlnet_model = XLNetModel.from_pretrained(model_path, config=xlnet_config)
 elif model_type == "xlnet-large-cased":
xlnet_model_config = '.. /input/xlnet-pretrained-models-pytorch/xlnet-large-cased-config.json'
xlnet_config = XLNetConfig.from_json_file(xlnet_model_config)
 xlnet_config.output_hidden_states = True
xlnet_config.hidden_dropout_prob = 0
model_path = os.path.join('.. /input/xlnet-pretrained-models-pytorch/' + model_type + '-pytorch_model.bin')
 self.xlnet_model = XLNetModel.from_pretrained(model_path, config=xlnet_config)
 elif model_type == "roberta-base":
roberta_model_config = '.. /input/roberta-transformers-pytorch/roberta-base/config.json'
roberta_config = RobertaConfig.from_json_file(roberta_model_config)
 roberta_config.output_hidden_states = True
roberta_config.hidden_dropout_prob = 0
model_path = os.path.join('.. /input/roberta-transformers-pytorch/roberta-base/pytorch_model.bin')
 self.roberta_model = RobertaModel.from_pretrained(model_path, config=roberta_config)
 self.roberta_model.resize_token_embeddings(len(tokenizer))
if model_type == "bert-base-uncased":
self.hidden_size = 768
elif model_type == "bert-large-uncased":
self.hidden_size = 1024
elif model_type == "bert-base-cased":
self.hidden_size = 768
elif model_type == "xlnet-base-cased":
self.hidden_size = 768
elif model_type == "xlnet-large-cased":
self.hidden_size = 1024
elif model_type == "roberta-base":
self.hidden_size = 768
else:
raise NotImplementedError
self.fc_1 = nn.Linear(self.hidden_size * len(hidden_layers), self.hidden_size)
 self.fc = nn.Linear(self.hidden_size, n_classes)
 self.selu = nn.SELU()
self.relu = nn.ReLU()
self.tanh = nn.Tanh()
self.dropouts = nn.ModuleList([
nn.Dropout(0.5)for _ in range(5)
 ])
 def forward(self, ids, seg_ids):
attention_mask =(ids > 0)
 if(( self.model_type == "bert-base-uncased")\
or(self.model_type == "bert-base-cased")\
or(self.model_type == "bert-large-uncased")\
or(self.model_type == "bert-large-cased")) :
outputs = self.bert_model(input_ids=ids, token_type_ids=seg_ids, attention_mask=attention_mask)
 hidden_states = outputs[2]
for i in range(len(self.hidden_layers)) :
if i == 0:
hidden_layer = self.hidden_layers[i]
hidden_state = hidden_states[hidden_layer][:, 0]
fuse_hidden = torch.unsqueeze(hidden_state, dim=-1)
 else:
hidden_layer = self.hidden_layers[i]
hidden_state = hidden_states[hidden_layer][:, 0]
h = torch.unsqueeze(hidden_state, dim=-1)
 fuse_hidden = torch.cat([fuse_hidden, h], dim=-1)
 fuse_hidden = fuse_hidden.reshape(fuse_hidden.shape[0], -1)
 h = self.relu(self.fc_1(fuse_hidden))
elif(( self.model_type == "xlnet-base-cased")\
or(self.model_type == "xlnet-large-cased")) :
attention_mask = attention_mask.float()
outputs = self.xlnet_model(input_ids=ids, token_type_ids=seg_ids, attention_mask=attention_mask)
 hidden_states = outputs[1]
for i in range(len(self.hidden_layers)) :
if i == 0:
hidden_layer = self.hidden_layers[i]
hidden_state = hidden_states[hidden_layer][:, 0]
fuse_hidden = torch.unsqueeze(hidden_state, dim=-1)
 else:
hidden_layer = self.hidden_layers[i]
hidden_state = hidden_states[hidden_layer][:, 0]
h = torch.unsqueeze(hidden_state, dim=-1)
 fuse_hidden = torch.cat([fuse_hidden, h], dim=-1)
 fuse_hidden = fuse_hidden.reshape(fuse_hidden.shape[0], -1)
 h = self.relu(self.fc_1(fuse_hidden))
elif(self.model_type == "roberta-base"):
attention_mask = attention_mask.float()
outputs = self.roberta_model(input_ids=ids, token_type_ids=seg_ids, attention_mask=attention_mask)
 hidden_states = outputs[2]
for i in range(len(self.hidden_layers)) :
if i == 0:
hidden_layer = self.hidden_layers[i]
hidden_state = hidden_states[hidden_layer][:, 0]
fuse_hidden = torch.unsqueeze(hidden_state, dim=-1)
 else:
hidden_layer = self.hidden_layers[i]
hidden_state = hidden_states[hidden_layer][:, 0]
h = torch.unsqueeze(hidden_state, dim=-1)
 fuse_hidden = torch.cat([fuse_hidden, h], dim=-1)
 fuse_hidden = fuse_hidden.reshape(fuse_hidden.shape[0], -1)
 h = self.relu(self.fc_1(fuse_hidden))
for j, dropout in enumerate(self.dropouts):
if j == 0:
logit = self.fc(dropout(h))
else:
logit += self.fc(dropout(h))
return logit / len(self.dropouts)
 def test_model(model_type="bert-base-cased", hidden_layers=[-1, -3, -5, -7, -9]):
x = torch.tensor([[1,2,3,4,5, 0, 0], [1,2,3,4,5, 0, 0]])
 seg_ids = torch.tensor([[0,0,0,0,0, 0, 0], [0,0,0,0,0, 0, 0]])
 model = QuestModel(model_type=model_type, hidden_layers=hidden_layers)
 y = model(x, seg_ids)
 print(y )<train_model>
train.isnull().sum()
test_model(model_type="bert-base-cased", hidden_layers=[-3, -4, -5, -6, -7] )<load_pretrained>
train.isnull().sum()
def create_bert_base_uncased_models() :
models = []
for i in range(10):
model = QuestModel(model_type="bert-base-uncased", hidden_layers=[-1, -3, -5, -7, -9])
 model.load_state_dict(torch.load(f'.. /input/qabertuncasedaugdiffv2swa/fold_{i}_checkpoint_swa.pth'))
model.eval()
models.append(model)
 return models
def create_bert_base_cased_models() :
models = []
for i in range(10):
model = QuestModel(model_type="bert-base-cased", hidden_layers=[-1, -3, -5, -7, -9])
 model.load_state_dict(torch.load(f'.. /input/qabertbasecasedaugdiffv2swa/fold_{i}_checkpoint_swa.pth'))
model.eval()
models.append(model)
 return models
def create_xlnet_base_cased_models() :
models = []
for i in range(5):
model = QuestModel(model_type="xlnet-base-cased", hidden_layers=[-3, -4, -5, -6, -7])
 model.load_state_dict(torch.load(f'.. /input/qaxlnetbasecasedaugdiff/fold_{i}_checkpoint.pth'))
model.eval()
models.append(model)
 return models
def create_xlnet_base_cased_question_models() :
models = []
for i in range(5):
model = QuestModel(model_type="xlnet-base-cased", n_classes=21, hidden_layers=[-3, -4, -5, -6, -7])
 model.load_state_dict(torch.load(f'.. /input/qaxlnetbasecasedaugdiffswaquestion/fold_{i}_checkpoint_swa.pth'))
model.eval()
models.append(model)
 return models
def create_xlnet_base_cased_answer_models() :
models = []
for i in range(5):
model = QuestModel(model_type="xlnet-base-cased", n_classes=9, hidden_layers=[-3, -4, -5, -6, -7])
 model.load_state_dict(torch.load(f'.. /input/qaxlnetbasecasedaugdiffswaanswer/fold_{i}_checkpoint_swa.pth'))
model.eval()
models.append(model)
 return models
def create_bert_base_uncased_question_models() :
models = []
for i in range(5):
model = QuestModel(model_type="bert-base-uncased", n_classes=21, hidden_layers=[-3, -4, -5, -6, -7])
 model.load_state_dict(torch.load(f'.. /input/qabertbaseuncasedaugdiffswaquestion/fold_{i}_checkpoint_swa.pth'))
model.eval()
models.append(model)
 return models
def create_bert_base_uncased_answer_models() :
models = []
for i in range(5):
model = QuestModel(model_type="bert-base-uncased", n_classes=9, hidden_layers=[-3, -4, -5, -6, -7])
 model.load_state_dict(torch.load(f'.. /input/qabertbaseuncasedaugdiffswaanswer/fold_{i}_checkpoint_swa.pth'))
model.eval()
models.append(model)
 return models
def create_bert_base_cased_question_models() :
models = []
for i in range(5):
model = QuestModel(model_type="bert-base-cased", n_classes=21, hidden_layers=[-2, -4, -6, -8, -10])
 model.load_state_dict(torch.load(f'.. /input/qabertbasecasedaugdiffswaquestion/fold_{i}_checkpoint_swa.pth'))
model.eval()
models.append(model)
 return models
def create_bert_base_cased_answer_models() :
models = []
for i in range(5):
model = QuestModel(model_type="bert-base-cased", n_classes=9, hidden_layers=[-2, -4, -6, -8, -10])
 model.load_state_dict(torch.load(f'.. /input/qabertbasecasedaugdiffswaanswer/fold_{i}_checkpoint_swa.pth'))
model.eval()
models.append(model)
 return models
def create_roberta_base_question_models(tokenizer):
models = []
for i in range(5):
model = QuestModel(model_type="roberta-base", tokenizer=tokenizer, n_classes=21, hidden_layers=[-3, -4, -5, -6, -7])
 model.load_state_dict(torch.load(f'.. /input/qarobertabasecasedaugdiffswaquestion/fold_{i}_checkpoint_swa.pth'))
model.eval()
models.append(model)
 return models
def create_roberta_base_answer_models(tokenizer):
models = []
for i in range(5):
model = QuestModel(model_type="roberta-base", tokenizer=tokenizer, n_classes=9, hidden_layers=[-3, -4, -5, -6, -7])
 model.load_state_dict(torch.load(f'.. /input/qarobertabasecasedaugdiffswaanswer/fold_{i}_checkpoint_swa.pth'))
model.eval()
models.append(model)
 return models<find_best_params>
train["type_int"] = train["type"].replace({
"Ghoul":1,
"Goblin":2,
"Ghost":3
} )
def predict(models, test_loader):
all_scores = []
with torch.no_grad() :
for ids, seg_ids in tqdm(test_loader, total=test_loader.num // test_loader.batch_size):
ids, seg_ids = ids.cuda() , seg_ids.cuda()
scores = []
for model in models:
model = model.cuda()
outputs = torch.sigmoid(model(ids, seg_ids)).cpu()
scores.append(outputs)
 all_scores.append(torch.mean(torch.stack(scores), 0))
all_scores = torch.cat(all_scores, 0 ).numpy()
return all_scores<load_pretrained>
dataset = pd.get_dummies(dataset, columns=["color"] )
test_loader, tokenizer = get_test_loader(model_type="roberta-base", content="Question", batch_size=32 )<predict_on_test>
dataset["type"] = dataset["type"].replace({
"Ghoul":1,
"Goblin":2,
"Ghost":3
} )
roberta_base_question_models = create_roberta_base_question_models(tokenizer)
 roberta_base_question_preds = predict(roberta_base_question_models, test_loader )<set_options>
train = dataset[:train_len]
test = dataset[train_len:]
test.drop(["type", "id"],axis = 1,inplace=True )
del roberta_base_question_models, test_loader, tokenizer
torch.cuda.empty_cache()
gc.collect()<load_pretrained>
train["type"] = train["type"].astype(int)
 Y_train = train["type"]
X_train = train.drop(["type", "id"], axis = 1 )
test_loader, tokenizer = get_test_loader(model_type="roberta-base", content="Answer", batch_size=32 )<predict_on_test>
kfold = StratifiedKFold(n_splits=10 )
roberta_base_answer_models = create_roberta_base_answer_models(tokenizer)
 roberta_base_answer_preds = predict(roberta_base_answer_models, test_loader )<set_options>
random_state = 2
classifiers = []
classifiers.append(SVC(random_state=random_state))
classifiers.append(DecisionTreeClassifier(random_state=random_state))
classifiers.append(AdaBoostClassifier(DecisionTreeClassifier(random_state=random_state),random_state=random_state,learning_rate=0.1))
classifiers.append(RandomForestClassifier(random_state=random_state))
classifiers.append(ExtraTreesClassifier(random_state=random_state))
classifiers.append(GradientBoostingClassifier(random_state=random_state))
classifiers.append(MLPClassifier(random_state=random_state))
classifiers.append(KNeighborsClassifier())
 classifiers.append(LogisticRegression(random_state = random_state))
classifiers.append(LinearDiscriminantAnalysis())
 cv_results = []
for classifier in classifiers :
cv_results.append(cross_val_score(classifier, X_train, y = Y_train, scoring = "accuracy", cv = kfold, n_jobs=4))
cv_means = []
cv_std = []
for cv_result in cv_results:
cv_means.append(cv_result.mean())
 cv_std.append(cv_result.std())
 cv_res = pd.DataFrame({"CrossValMeans":cv_means,"CrossValerrors": cv_std,"Algorithm":["SVC","DecisionTree","AdaBoost",
"RandomForest","ExtraTrees","GradientBoosting","MultipleLayerPerceptron","KNeighboors","LogisticRegression","LinearDiscriminantAnalysis"]} )
del roberta_base_answer_models, test_loader, tokenizer
torch.cuda.empty_cache()
gc.collect()<concatenate>
classifiers
roberta_base_question_answer_preds = np.concatenate([roberta_base_question_preds, roberta_base_answer_preds], axis=1 )<load_from_csv>
LDA = LinearDiscriminantAnalysis()
ex_param_grid = {"n_components": [None, 1, 2, 3, 4]}
gsLDA = GridSearchCV(LDA,param_grid = ex_param_grid, cv=kfold, scoring="accuracy", n_jobs= 4, verbose = 1)
 gsLDA.fit(X_train,Y_train)
 LDA_best = gsLDA.best_estimator_
gsLDA.best_score_
<prepare_output>
GBC = GradientBoostingClassifier()
gb_param_grid = {'loss' : ["deviance"],
'n_estimators' : [100,200,300],
'learning_rate': [0.1, 0.05, 0.01],
'max_depth': [4, 8],
'min_samples_leaf': [100,150],
'max_features': [0.3, 0.1]
}
gsGBC = GridSearchCV(GBC,param_grid = gb_param_grid, cv=kfold, scoring="accuracy", n_jobs= 4, verbose = 1)
 gsGBC.fit(X_train,Y_train)
 GBC_best = gsGBC.best_estimator_
gsGBC.best_score_
sub[TARGET_COLUMNS] = preds<load_from_csv>
MLP = MLPClassifier()
ex_param_grid = {
'hidden_layer_sizes': [(50,50,50),(50,100,50),(100,)],
'activation': ['tanh', 'relu'],
'solver': ['sgd', 'adam'],
'alpha': [0.0001, 0.05],
'learning_rate': ['constant','adaptive'],
}
gsMLP = GridSearchCV(MLP, param_grid = ex_param_grid, cv=kfold, scoring="accuracy", n_jobs= 4, verbose = 1)
 gsMLP.fit(X_train,Y_train)
 MLP_best = gsMLP.best_estimator_
gsMLP.best_score_
test = pd.read_csv(f'{DATA_DIR}/test.csv' )<merge>
LRC = LogisticRegression()
ex_param_grid = {
'C': [0.001, 0.01, 0.1, 1, 10, 100, 1000]
}
gsLRC = GridSearchCV(LRC, param_grid = ex_param_grid, cv=kfold, scoring="accuracy", n_jobs= 4, verbose = 1)
 gsLRC.fit(X_train,Y_train)
 LRC_best = gsLRC.best_estimator_
gsLRC.best_score_
test = test.set_index('qa_id' ).join(sub.set_index('qa_id'))<normalization>
votingC = VotingClassifier(estimators=[('lda', LDA_best),('gbc', GBC_best), 
('mlp', MLP_best),('lrc',LRC_best)], voting='soft', n_jobs=4)
 votingC = votingC.fit(X_train, Y_train )
def postprocessing(oof_df):
scaler = MinMaxScaler()
type_one_column_list = [
'question_conversational', \
'question_has_commonly_accepted_answer', \
'question_not_really_a_question', \
'question_type_choice', \
'question_type_compare', \
'question_type_consequence', \
'question_type_definition', \
'question_type_entity', \
'question_type_instructions',
]
oof_df[type_one_column_list] = scaler.fit_transform(oof_df[type_one_column_list])
 tmp = oof_df.copy(deep=True)
 for column in type_one_column_list:
oof_df.loc[tmp[column] <= 0.16667, column] = 0
oof_df.loc[(tmp[column] > 0.16667)&(tmp[column] <= 0.41667), column] = 0.333333
oof_df.loc[(tmp[column] > 0.41667)&(tmp[column] <= 0.58333), column] = 0.500000
oof_df.loc[(tmp[column] > 0.58333)&(tmp[column] <= 0.73333), column] = 0.666667
oof_df.loc[(tmp[column] > 0.73333), column] = 1
type_three_column_list = [
'question_interestingness_self',
]
scaler = MinMaxScaler(feature_range=(0, 1))
oof_df[type_three_column_list] = scaler.fit_transform(oof_df[type_three_column_list])
 tmp[type_three_column_list] = scaler.fit_transform(tmp[type_three_column_list])
 for column in type_three_column_list:
oof_df.loc[tmp[column] <= 0.385, column] = 0.333333
oof_df.loc[(tmp[column] > 0.385)&(tmp[column] <= 0.47), column] = 0.444444
oof_df.loc[(tmp[column] > 0.47)&(tmp[column] <= 0.525), column] = 0.5
oof_df.loc[(tmp[column] > 0.525)&(tmp[column] <= 0.605), column] = 0.555556
oof_df.loc[(tmp[column] > 0.605)&(tmp[column] <= 0.715), column] = 0.666667
oof_df.loc[(tmp[column] > 0.715)&(tmp[column] <= 0.8), column] = 0.833333
oof_df.loc[(tmp[column] > 0.8)&(tmp[column] <= 0.94), column] = 0.888889
oof_df.loc[(tmp[column] > 0.94), column] = 1
type_four_column_list = [
'answer_satisfaction'
]
scaler = MinMaxScaler(feature_range=(0.2, 1))
oof_df[type_four_column_list] = scaler.fit_transform(oof_df[type_four_column_list])
 tmp[type_four_column_list] = scaler.fit_transform(tmp[type_four_column_list])
 for column in type_four_column_list:
oof_df.loc[tmp[column] <= 0.233, column] = 0.200000
oof_df.loc[(tmp[column] > 0.233)&(tmp[column] <= 0.283), column] = 0.266667
oof_df.loc[(tmp[column] > 0.283)&(tmp[column] <= 0.315), column] = 0.300000
oof_df.loc[(tmp[column] > 0.315)&(tmp[column] <= 0.365), column] = 0.333333
oof_df.loc[(tmp[column] > 0.365)&(tmp[column] <= 0.433), column] = 0.400000
oof_df.loc[(tmp[column] > 0.433)&(tmp[column] <= 0.483), column] = 0.466667
oof_df.loc[(tmp[column] > 0.483)&(tmp[column] <= 0.517), column] = 0.500000
oof_df.loc[(tmp[column] > 0.517)&(tmp[column] <= 0.567), column] = 0.533333
oof_df.loc[(tmp[column] > 0.567)&(tmp[column] <= 0.633), column] = 0.600000
oof_df.loc[(tmp[column] > 0.633)&(tmp[column] <= 0.683), column] = 0.666667
oof_df.loc[(tmp[column] > 0.683)&(tmp[column] <= 0.715), column] = 0.700000
oof_df.loc[(tmp[column] > 0.715)&(tmp[column] <= 0.767), column] = 0.733333
oof_df.loc[(tmp[column] > 0.767)&(tmp[column] <= 0.833), column] = 0.800000
oof_df.loc[(tmp[column] > 0.883)&(tmp[column] <= 0.915), column] = 0.900000
oof_df.loc[(tmp[column] > 0.915)&(tmp[column] <= 0.967), column] = 0.933333
oof_df.loc[(tmp[column] > 0.967), column] = 1
oof_values = oof_df[TARGET_COLUMNS].values
DEGREE = len(oof_df)//45*9
oof_values = np.around(oof_values * DEGREE)/ DEGREE
oof_df[TARGET_COLUMNS] = oof_values
return oof_df<categorify>
test_type = pd.Series(votingC.predict(test), name="Type")
 test_type = test_type.replace({
1:"Ghoul",
2:"Goblin",
3:"Ghost"
})
 results = pd.concat([IDtest,test_type],axis=1)
 results.to_csv("voting_prediction.csv",index=False )