Kaggle competition: University of Liverpool - Ion Switching
<SOS> metric: MacroFScore Kaggle data source: university-of-liverpool-ion-switching<define_variables>
p1 = pd.read_csv('.. /input/top-ion-subs/submission1.csv' ).open_channels.values
p2 = pd.read_csv('.. /input/top-ion-subs/submission2.csv' ).open_channels.values
p3 = pd.read_csv('.. /input/top-ion-subs/submission3.csv' ).open_channels.values
final_output = []
stop_words = set(stopwords.words('english'))
with torch.no_grad() :
tk0 = tqdm(data_loader, total=len(data_loader))
for bi, d in enumerate(tk0):
ids = d["ids"]
token_type_ids = d["token_type_ids"]
mask = d["mask"]
sentiment = d["sentiment"]
orig_selected = d["orig_selected"]
orig_tweet = d["orig_tweet"]
targets_start = d["targets_start"]
targets_end = d["targets_end"]
ids = ids.to(device, dtype=torch.long)
 token_type_ids = token_type_ids.to(device, dtype=torch.long)
 mask = mask.to(device, dtype=torch.long)
 targets_start = targets_start.to(device, dtype=torch.long)
 targets_end = targets_end.to(device, dtype=torch.long)
 outputs_start1, outputs_end1 = model1(
 ids=ids,
mask=mask,
token_type_ids=token_type_ids 
)
 outputs_start2, outputs_end2 = model2(
 ids=ids,
mask=mask,
token_type_ids=token_type_ids 
)
 outputs_start3, outputs_end3 = model3(
 ids=ids,
mask=mask,
token_type_ids=token_type_ids 
)
 outputs_start4, outputs_end4 = model4(
 ids=ids,
mask=mask,
token_type_ids=token_type_ids 
)
 outputs_start5, outputs_end5 = model5(
 ids=ids,
mask=mask,
token_type_ids=token_type_ids 
)
 outputs_start6, outputs_end6 = model6(
 ids=ids,
mask=mask,
token_type_ids=token_type_ids 
)
 outputs_start7, outputs_end7 = model7(
 ids=ids,
mask=mask,
token_type_ids=token_type_ids 
)
 outputs_start8, outputs_end8 = model8(
 ids=ids,
mask=mask,
token_type_ids=token_type_ids 
)
 outputs_start =(outputs_start1 + outputs_start2 +
outputs_start3 + outputs_start4 +
outputs_start5 + outputs_start7 +
outputs_start6 + outputs_start8)/ 8
outputs_end =(outputs_end1 + outputs_end2 + outputs_end3 + outputs_end4 +
outputs_end5 + outputs_end6 +
outputs_end7 + outputs_end8)/ 8
outputs_start = torch.softmax(outputs_start, dim=1 ).cpu().detach().numpy()
outputs_end = torch.softmax(outputs_end, dim=1 ).cpu().detach().numpy()
for id, tweet in enumerate(orig_tweet):
a = np.argmax(outputs_start[id])
 b = np.argmax(outputs_end[id])
 if a > b:
selected_text = tweet
else:
n_tweet = '
 ' + bpe.encode(tweet)+ ' 
'
nn_tweet = vocab.encode_line(n_tweet, append_eos=False, add_if_not_exist=False ).long().tolist()
select = nn_tweet[a:b+1]
selected_text = bpe.decode(" ".join([vocab[i] for i in select]))
selected_text = selected_text.replace("
","" ).replace("
","" ).replace("<unk>", "")
 if selected_text.split() [-1] in set(string.punctuation)or selected_text.split() [-1] in stop_words:
selected_text = " ".join(selected_text.split() [:-1])
 if selected_text.strip() == "":
selected_text = tweet
final_output.append(selected_text )<save_to_csv>
sub = pd.read_csv('.. /input/liverpool-ion-switching/sample_submission.csv')
 sub.open_channels = np.median(np.stack([p1,p2,p3] ).T, axis=1 ).astype('int8')
 sub.to_csv('submission.csv', index=False, float_format='%.4f' )
a = pd.read_csv("/kaggle/input/tweet-sentiment-extraction/sample_submission.csv")
 a.loc[:, 'selected_text'] = final_output
a.to_csv("submission.csv", index=False )<feature_engineering>
sub.open_channels.value_counts()