Kaggle competition: Mechanisms of Action (MoA) Prediction
def convert_train_to_json(train_set):
outer_list = []
for row in train_set:
qid = row[0]
context = row[1]
answer = row[2]
question = row[-1]
inner_list = []
answers = []
if type(context)!= str or type(answer)!= str or type(question)!= str:
continue
answer_starts = search(context, answer)
 for answer_start in answer_starts:
answers.append({'answer_start': answer_start, 'text': answer.lower() })
 break
inner_list.append({'question': question, 'id': qid, 'is_impossible': False, 'answers': answers})
 outer_list.append({'context': context.lower() , 'qas': inner_list})
 return outer_list<data_type_conversions>
submission = pd.read_csv('/kaggle/input/lish-moa/sample_submission.csv')
 public_ids = list(submission['sig_id'].values)
 submission.shape
train = convert_train_to_json(train )<categorify>
test = pd.read_csv('/kaggle/input/lish-moa/test_features.csv')
 test_ids = list(test['sig_id'].values)
 private_ids = list(set(test_ids)-set(public_ids))
len(private_ids )
def convert_test_to_json(test_set):
outer_list = []
for row in test_set:
qid = row[0]
context = row[1]
question = row[-1]
inner_list = []
if type(context)!= str or type(question)!= str:
continue
answers = []
answers.append({'answer_start': 1000000, 'text': '__None__'})
 inner_list.append({'question': question, 'id': qid, 'is_impossible': False, 'answers': answers})
 outer_list.append({'context': context.lower() , 'qas': inner_list})
 return outer_list<data_type_conversions>
kernels = pd.read_csv('/kaggle/input/moa-public-kernels/kernels.csv', index_col='id' )
test = convert_test_to_json(test )<install_modules>
def make_filename(idx):
return glob.glob('/kaggle/input/moa-public-kernels/' + str(idx)+ '__submission.csv')[0]
def read_predictions(idx):
temp = pd.read_csv(make_filename(idx))
return temp
predictions = [read_predictions(idx)for idx in kernels.index]
len(predictions )
!pip install '/kaggle/input/simple-transformers-pypi/seqeval-0.0.12-py3-none-any.whl' -q
!pip install '/kaggle/input/simple-transformers-pypi/simpletransformers-0.22.1-py3-none-any.whl' -q<import_modules>
y_pred = pd.DataFrame()
y_pred['sig_id'] = predictions[0]['sig_id']
for column in target_columns:
column_data = []
for i in range(len(predictions)) :
column_data.append(predictions[i][column])
 y_pred[column] = np.mean(column_data, axis=0)
 y_pred.shape
from simpletransformers.question_answering import QuestionAnsweringModel<choose_model_class>
scores = 1.- kernels['score']
sum_scores = sum(scores)
 weights = [x / sum_scores for x in scores]
MODEL = '/kaggle/input/transformers-pretrained-distilbert/distilbert-base-uncased-distilled-squad/'
model = QuestionAnsweringModel('distilbert',
MODEL,
args={'reprocess_input_data': True,
'overwrite_output_dir': True,
'learning_rate': 5e-5,
'num_train_epochs': 2,
'max_seq_length': 192,
'doc_stride': 64,
'fp16': False
},
use_cuda=True
)<train_model>
weighted_y_pred = pd.DataFrame()
weighted_y_pred['sig_id'] = predictions[0]['sig_id']
for column in target_columns:
column_data = []
for i in range(len(predictions)) :
column_data.append(predictions[i][column] * weights[i])
 weighted_y_pred[column] = np.sum(column_data, axis=0)
 weighted_y_pred.shape
model.train_model('data/train.json' )<predict_on_test>
y_pred = weighted_y_pred
pred_df = model.predict(test)
 pred_df = pd.DataFrame.from_dict(pred_df )<feature_engineering>
submission = pd.DataFrame(index = public_ids + private_ids, columns=target_columns)
 submission.index.name = 'sig_id'
submission[:] = 0
submission.loc[y_pred.sig_id,:] = y_pred[target_columns].values
submission.loc[test[test.cp_type == 'ctl_vehicle'].sig_id] = 0
submission.to_csv('submission.csv', index=True)
 submission.head().T