Kaggle competition: Google QUEST Q&A Labeling
<SOS> metric: MCSpearmanR Kaggle data source: google-quest-q&a-labeling<feature_engineering>
%%time
!pip install /kaggle/input/pythonmag/mag > /dev/null
!pip install.. /input/sacremoses/sacremoses-master/ > /dev/null
!pip install /kaggle/input/sacrebleu/sacreBLEU-master/ > /dev/null
!pip install /kaggle/input/fairseq-hacked/fairseq > /dev/null
combined_training_dataset['project_submitted_datetime'] = pd.to_datetime(combined_training_dataset['project_submitted_datetime'])
 combined_training_dataset['year_week'] = combined_training_dataset['project_submitted_datetime'].map(lambda x: 100*(x.year - 2000)+ x.week ).apply(str)
 combined_test_dataset['project_submitted_datetime'] = pd.to_datetime(combined_test_dataset['project_submitted_datetime'])
 combined_test_dataset['year_week'] = combined_test_dataset['project_submitted_datetime'].map(lambda x: 100*(x.year - 2000)+ x.week ).apply(str)
 combined_training_dataset[0:5]<import_modules>
import os
import json
from collections import Counter
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt<groupby>
%%time
!python.. /input/bert-base-random-code/run.py \
--sub_file=model2_bert_base_cased_pred.csv \
--data_path=/kaggle/input/google-quest-challenge/ \
--max_sequence_length=500 \
--max_title_length=26 \
--max_question_length=260 \
--max_answer_length=210 \
--batch_size=8 \
--bert_model=/kaggle/input/bert-base-pretrained/stackx-base-cased/
grouped_prefixes = combined_training_dataset.groupby(["teacher_prefix", "project_is_approved"])
 grouped_prefixes = grouped_prefixes.agg({'teacher_prefix' : 'count'} ).rename(columns={'teacher_prefix' : 'count'})
 grouped_prefixes<prepare_x_and_y>
ROBERTA_EXPERIMENT_DIR = "2-4-roberta-base-saved-5-head_tail-roberta-stackx-base-v2-pl1kksample20k-1e-05-210-260-500-26-roberta-200"
!mkdir $ROBERTA_EXPERIMENT_DIR
!ln -s /kaggle/input/roberta-stackx-base-pl20k/checkpoints $ROBERTA_EXPERIMENT_DIR/checkpoints
ROBERTA_CONFIG = {
"_seed": 42,
"batch_accumulation": 2,
"batch_size": 4,
"bert_model": "roberta-base-saved",
"folds": 5,
"head_tail": True,
"label": "roberta-stackx-base-v2-pl1kksample20k",
"lr": 1e-05,
"max_answer_length": 210,
"max_question_length": 260,
"max_sequence_length": 500,
"max_title_length": 26,
"model_type": "roberta",
"warmup": 200
}
with open(os.path.join(ROBERTA_EXPERIMENT_DIR, "config.json"), "w")as fp:
json.dump(ROBERTA_CONFIG, fp)
 !echo kek > $ROBERTA_EXPERIMENT_DIR/command
arr_counts = grouped_prefixes["count"].tolist()
y_no = []
y_yes = []
for i in range(0, len(arr_counts)- 1):
if i % 2 == 0:
y_no.append(arr_counts[i])
 else:
y_yes.append(arr_counts[i])
 y_yes.append(arr_counts[i+1])
 y_no.append(0)
 x_labels = ['Dr', 'Mr.', 'Mrs', 'Ms', 'Teacher', 'None']
N = len(x_labels)
 ind = np.arange(N)
 width = 0.4
fig = plt.figure()
ax = fig.add_subplot(111)
 rects1 = ax.bar(ind, y_yes, width, color='
rects2 = ax.bar(ind+width, y_no, width, color='
ax.set_ylabel('Submissions Count')
 ax.set_xticks(ind+width)
 ax.set_xticklabels(x_labels)
 ax.legend(( rects1[0], rects2[0]),('Approved', 'NOT Approved'))
def autolabel(rects):
for rect in rects:
h = rect.get_height()
ax.text(rect.get_x() +rect.get_width() /2., 1.05*h, '%d'%int(h),
ha='center', va='bottom')
 autolabel(rects1)
 autolabel(rects2)
 plt.title('Submissions Approved or NOT Approved Based on Name Prefix')
 plt.show()<define_variables>
%%time
!python.. /input/roberta-base-code/infer.py \
--experiment $ROBERTA_EXPERIMENT_DIR \
--checkpoint=best_model.pth \
--bert_model=/kaggle/input/roberta-base-model \
--dataframe=/kaggle/input/google-quest-challenge/test.csv \
--output_dir=roberta-base-output
print('Chance to get approved by prefix')
 arr_total_applications = []
for i in range(0, len(y_yes)) :
arr_total_applications.append(y_yes[i] + y_no[i])
 print(x_labels[i] + ': ' + "%f" %(y_yes[i] / arr_total_applications[i]))<groupby>
%%time
!python.. /input/bart-code/run.py \
--sub_file=model4_bart_large_pred.csv \
--data_path=/kaggle/input/google-quest-challenge/ \
--max_sequence_length=500 \
--max_title_length=26 \
--max_question_length=260 \
--max_answer_length=210 \
--batch_size=4 \
--bert_model=bart.large
bins = [0, 150, 200, 250, 300, 350, 400, 500, 600, 750, 1000, 1000000]
combined_training_dataset['binned_costs'] = pd.cut(combined_training_dataset['total_cost'], bins)
 grouped_costs = combined_training_dataset.groupby(["binned_costs", "project_is_approved"])
 grouped_costs = grouped_costs.agg({'binned_costs' : 'count'} ).rename(columns={'binned_costs' : 'count'})
 arr_percent_yes = []
for i in range(0,(len(bins)- 1)* 2):
if i % 2 == 0:
k = i // 2
label = 'no'
no_count = grouped_costs["count"].values[i]
print("%d+ - %s: %d" %(bins[k], label, no_count))
else:
label = 'yes'
yes_count = grouped_costs["count"].values[i]
percent_yes = yes_count /(no_count + yes_count)
 arr_percent_yes.append(percent_yes)
 print("%d+ - %s: %d, percent: %f" %(bins[k], label, yes_count, percent_yes))<groupby>
sample_submission_df = pd.read_csv("/kaggle/input/google-quest-challenge/sample_submission.csv",
index_col='qa_id')
 target_columns = sample_submission_df.columns
print(f'There are {len(target_columns)} targets to predict')
 train_df = pd.read_csv("/kaggle/input/google-quest-challenge/train.csv" )
grouped_previous_submissions = combined_training_dataset.groupby(["teacher_number_of_previously_posted_projects", "project_is_approved"])
 grouped_previous_submissions = grouped_previous_submissions.agg({'teacher_number_of_previously_posted_projects' : 'count'} ).rename(columns={'teacher_number_of_previously_posted_projects' : 'count'})
 grouped_previous_submissions[0:202]<groupby>
model1_bert_base_uncased_pred_df = pd.read_csv("model1_bert_base_uncased_pred.csv")
 model2_bert_base_cased_pred_df = pd.read_csv("model2_bert_base_cased_pred.csv")
 model4_bart_large_pred_df = pd.read_csv("model4_bart_large_pred.csv" )
arr_percents_yes = []
for i in range(0,202):
if i % 2 == 0:
k = i / 2
label = 'no'
no_count = grouped_previous_submissions["count"].values[i]
print("%d - %s: %d" %(k, label, no_count))
else:
label = 'yes'
yes_count = grouped_previous_submissions["count"].values[i]
percent_yes = yes_count /(no_count + yes_count)
 arr_percents_yes.append(percent_yes)
 print("%d - %s: %d, percent: %f" %(k, label, yes_count, percent_yes)) 
<groupby>
roberta_base_dfs = [pd.read_csv(
 os.path.join("roberta-base-output", "fold-{}.csv".format(fold)))
 for fold in range(5)]
model3_roberta_pred_df = roberta_base_dfs[0].copy()
for col in target_columns:
model3_roberta_pred_df[col] = np.mean([df[col] for df in roberta_base_dfs], axis=0 )
grouped_states = combined_training_dataset.groupby(["school_state", "project_is_approved"])
 grouped_states = grouped_states.size().rename('count' ).reset_index()
arr_percents = []
for i in range(0,102):
if i % 2 == 0:
no_count = grouped_states["count"].values[i]
else:
yes_count = grouped_states["count"].values[i]
total_count = no_count + yes_count
percent_no = no_count / total_count
percent_yes = yes_count / total_count
arr_percents.append(percent_no)
 arr_percents.append(percent_yes)
 grouped_states['chances'] = pd.Series(arr_percents, index=grouped_states.index)
 grouped_states 
<filter>
blended_df = model3_roberta_pred_df.copy()
for col in target_columns:
blended_df[col] =(
 model1_bert_base_uncased_pred_df[col] * 0.1 +
model2_bert_base_cased_pred_df[col] * 0.2 +
model3_roberta_pred_df[col] * 0.1 +
model4_bart_large_pred_df[col] * 0.3
)
grouped_states.loc[(grouped_states['project_is_approved'] == 1)&(grouped_states['chances'] <.83)]<filter>
def postprocess_single(target, ref):

 ids = np.argsort(target)
 counts = sorted(Counter(ref ).items() , key=lambda s: s[0])
 scores = np.zeros_like(target)
 last_pos = 0
v = 0
for value, count in counts:
next_pos = last_pos + int(round(count / len(ref)* len(target)))
 if next_pos == last_pos:
next_pos += 1
cond = ids[last_pos:next_pos]
scores[cond] = v
last_pos = next_pos
v += 1
return scores / scores.max()
grouped_states.loc[(grouped_states['project_is_approved'] == 1)&(grouped_states['chances'] >.868)]<groupby>
def postprocess_prediction(prediction, actual):
postprocessed = prediction.copy()
for col in target_columns:
scores = postprocess_single(prediction[col].values, actual[col].values)
 if col in(
 "question_conversational",
"question_type_compare",
"question_type_definition",
"question_type_entity",
"question_has_commonly_accepted_answer",
"question_type_consequence",
"question_type_spelling" 
):
postprocessed[col] = scores
v = postprocessed[col].values
postprocessed[col] =(v - v.min())/(v.max() - v.min())
 return postprocessed
grouped_grades = combined_training_dataset.groupby(["project_grade_category", "project_is_approved"])
 grouped_grades = grouped_grades.size().rename('count' ).reset_index()
grouped_grades<groupby>
postprocessed = postprocess_prediction(blended_df, train_df )
<groupby><EOS> 
postprocessed.to_csv("submission.csv", index=False )