Kaggle competition: Digit Recognizer
features_map = {
'row_id' : 0,
'timestamp' : 1,
'user_id' : 2,
'content_id' : 3,
'content_type_id' : 4,
'task_container_id' : 5,
'prior_question_elapsed_time' : 6,
'prior_question_had_explanation' : 7,
'prior_group_answers_correct' : 8,
'prior_group_responses' : 9,
'prior_question_1_timedelta_min' : 10,
'prior_lecture_1_timedelta_min' : 11,
'prior_question_2_timedelta_min' : 12,
'prior_lecture_2_timedelta_min' : 13,
'prior_question_3_timedelta_min' : 14,
'prior_lecture_3_timedelta_min' : 15,
'user_slice_accuracy_5' : 16,
'user_slice_accuracy_20' : 17,
'user_slice_accuracy_50' : 18,
'user_slice_accuracy_session_3' : 19,
'user_slice_accuracy_session_12' : 20,
'user_slice_accuracy_session_48' : 21,
'task_container_freq' : 22,
'task_container_counter' : 23,
'user_question_attempt_cnt' : 24,
'lecture_cnt' : 25,
'lecture_concept_cnt' : 26,
'lecture_solving_question_cnt' : 27,
'part_l_q_cnt' : 28,
'tag_l_q_equal_cnt' : 29,
'user_question_part_accuracy' : 30,
'user_question_tag_accuracy' : 31,
'prior_question_incorrect_timedelta_min' : 32,
'prior_question_correct_timedelta_min' : 33,
'user_question_time_mean_n_session' : 34,
'user_question_time_mean_all_session' : 35,
'user_question_time_delta_n_session' : 36,
'user_question_time_delta_all_session' : 37,
'user_prior_correct_expl_prc' : 38,
'user_prior_correct_noexpl_prc' : 39,
'user_prior_wrong_expl_prc' : 40,
'user_prior_wrong_noexpl_prc' : 41,
'user_prior_answer_expl_type' : 42,
'user_answer_mode_10' : 43,
'user_answer_mode_50' : 44,
'first_bundle_id_cluster' : 45,
'bundle_id_all_accuracy' : 46,
'bundle_id_cluster_accuracy' : 47,
'question_user_cnt' : 48,
'correct_answers_user_cnt' : 49,
'correct_answers_user_prc' : 50,
'prior_question_had_explanation_user_cnt' : 51,
'prior_question_had_explanation_user_prc' : 52,
'content_id_mean' : 53,
'part' : 54,
'part_mean' : 55,
'tag_1_mean' : 56,
'tag_2_mean' : 57,
'user_to_question_accuracy' : 58,
'hmean_user_content_accuracy' : 59
}
dict_questionid_part_tag12_avgtarget_map = {
'content_id_cnt' : 0,
'content_correct_cnt' : 1,
'answered_correctly_avg_content_smooth' : 2,
'part' : 3,
'answered_correctly_avg_part' : 4,
'answered_correctly_avg_tag_1' : 5,
'answered_correctly_avg_tag_2' : 6,
'answered_correctly_avg_part_tag_1' : 7,
'answered_correctly_avg_part_tag_2' : 8,
'tags_encoded' : 9
}
train_cols_clf = [features_map['prior_question_1_timedelta_min'],
features_map['prior_question_2_timedelta_min'],
features_map['prior_question_3_timedelta_min'],
features_map['prior_lecture_1_timedelta_min'],
features_map['prior_lecture_2_timedelta_min'],
features_map['prior_lecture_3_timedelta_min'],
features_map['task_container_freq'],
features_map['task_container_counter'],
features_map['user_question_attempt_cnt'],
features_map['prior_question_elapsed_time'],
features_map['prior_question_had_explanation'],
features_map['question_user_cnt'],
features_map['correct_answers_user_cnt'],
features_map['correct_answers_user_prc'],
features_map['prior_question_had_explanation_user_cnt'],
features_map['prior_question_had_explanation_user_prc'],
features_map['user_slice_accuracy_5'],
features_map['user_slice_accuracy_20'],
features_map['user_slice_accuracy_50'],
features_map['user_slice_accuracy_session_3'],
features_map['user_slice_accuracy_session_12'],
features_map['user_slice_accuracy_session_48'],
features_map['lecture_cnt'],
features_map['lecture_concept_cnt'],
features_map['lecture_solving_question_cnt'],
features_map['part_l_q_cnt'],
features_map['tag_l_q_equal_cnt'],
features_map['user_question_part_accuracy'],
features_map['user_question_tag_accuracy'],
features_map['prior_question_incorrect_timedelta_min'],
features_map['prior_question_correct_timedelta_min'],
features_map['user_question_time_mean_n_session'],
features_map['user_question_time_mean_all_session'],
features_map['user_question_time_delta_n_session'],
features_map['user_question_time_delta_all_session'],
features_map['user_prior_correct_expl_prc'],
features_map['user_prior_correct_noexpl_prc'],
features_map['user_prior_wrong_expl_prc'],
features_map['user_prior_wrong_noexpl_prc'],
features_map['user_prior_answer_expl_type'],
features_map['user_answer_mode_10'],
features_map['user_answer_mode_50'],
features_map['first_bundle_id_cluster'],
features_map['bundle_id_all_accuracy'],
features_map['bundle_id_cluster_accuracy'],
features_map['content_id_mean'],
features_map['part'],
features_map['part_mean'],
features_map['tag_1_mean'],
features_map['tag_2_mean'],
features_map['user_to_question_accuracy'],
features_map['hmean_user_content_accuracy'],
]
train_cols_lgb = [features_map['prior_question_1_timedelta_min'],
features_map['prior_question_2_timedelta_min'],
features_map['prior_question_3_timedelta_min'],
features_map['prior_lecture_1_timedelta_min'],
features_map['prior_lecture_2_timedelta_min'],
features_map['prior_lecture_3_timedelta_min'],
features_map['task_container_freq'],
features_map['task_container_counter'],
features_map['user_question_attempt_cnt'],
features_map['prior_question_elapsed_time'],
features_map['prior_question_had_explanation'],
features_map['question_user_cnt'],
features_map['correct_answers_user_cnt'],
features_map['correct_answers_user_prc'],
features_map['prior_question_had_explanation_user_cnt'],
features_map['prior_question_had_explanation_user_prc'],
features_map['user_slice_accuracy_5'],
features_map['user_slice_accuracy_20'],
features_map['user_slice_accuracy_50'],
features_map['user_slice_accuracy_session_3'],
features_map['user_slice_accuracy_session_12'],
features_map['user_slice_accuracy_session_48'],
features_map['lecture_cnt'],
features_map['lecture_concept_cnt'],
features_map['lecture_solving_question_cnt'],
features_map['part_l_q_cnt'],
features_map['tag_l_q_equal_cnt'],
features_map['user_question_part_accuracy'],
features_map['user_question_tag_accuracy'],
features_map['prior_question_incorrect_timedelta_min'],
features_map['prior_question_correct_timedelta_min'],
features_map['user_question_time_mean_n_session'],
features_map['user_question_time_mean_all_session'],
features_map['user_question_time_delta_n_session'],
features_map['user_question_time_delta_all_session'],
features_map['user_prior_correct_expl_prc'],
features_map['user_prior_correct_noexpl_prc'],
features_map['user_prior_wrong_expl_prc'],
features_map['user_prior_wrong_noexpl_prc'],
features_map['user_prior_answer_expl_type'],
features_map['user_answer_mode_10'],
features_map['user_answer_mode_50'],
features_map['first_bundle_id_cluster'],
features_map['bundle_id_all_accuracy'],
features_map['bundle_id_cluster_accuracy'],
features_map['content_id_mean'],
features_map['part'],
features_map['part_mean'],
features_map['tag_1_mean'],
features_map['tag_2_mean'],
features_map['user_to_question_accuracy'],
features_map['hmean_user_content_accuracy'],
]<define_variables>
df_train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 df_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv' )
idx = 86867031<split>
x_train = np.array(df_train.iloc[:,1:])
 x_train = np.array([np.reshape(i,(28, 28, 1)) for i in x_train])
 y_train = np.array(df_train.iloc[:,0] )
env = riiideducation.make_env()
iter_test = env.iter_test()<feature_engineering>
x_train = x_train/255.0
y_train = keras.utils.to_categorical(y_train )
previous_test_df = pd.DataFrame()
prev_test_df = None
for(test_df, sample_prediction_df)in iter_test:
test_df_saint = test_df.copy()
if(prev_test_df is not None)&(psutil.virtual_memory().percent < 90):
prev_test_df['answered_correctly'] = eval(test_df['prior_group_answers_correct'].iloc[0])
 prev_test_df = prev_test_df[prev_test_df.content_type_id == False]
prev_test_df = feature_time_lag(prev_test_df, time_dict)
 prev_group = prev_test_df[['user_id', 'content_id', 'answered_correctly', 'part', 'prior_question_elapsed_time', 'time_lag', 'prior_question_had_explanation']].groupby('user_id' ).apply(lambda r:(
 r['content_id'].values,
r['answered_correctly'].values,
r['part'].values,
r['prior_question_elapsed_time'].values,
r['time_lag'].values,
r['prior_question_had_explanation'].values))
for prev_user_id in prev_group.index:
if prev_user_id in group.index:
group[prev_user_id] =(
 np.append(group[prev_user_id][0], prev_group[prev_user_id][0])[-MAX_SEQ:],
np.append(group[prev_user_id][1], prev_group[prev_user_id][1])[-MAX_SEQ:],
np.append(group[prev_user_id][2], prev_group[prev_user_id][2])[-MAX_SEQ:],
np.append(group[prev_user_id][3], prev_group[prev_user_id][3])[-MAX_SEQ:],
np.append(group[prev_user_id][4], prev_group[prev_user_id][4])[-MAX_SEQ:],
np.append(group[prev_user_id][5], prev_group[prev_user_id][5])[-MAX_SEQ:] 
)
 else:
group[prev_user_id] =(
 prev_group[prev_user_id][0],
prev_group[prev_user_id][1],
prev_group[prev_user_id][2],
prev_group[prev_user_id][3],
prev_group[prev_user_id][4],
prev_group[prev_user_id][5] 
)
 test_df_saint.prior_question_elapsed_time = test_df_saint.prior_question_elapsed_time.fillna(0)
 test_df_saint['prior_question_had_explanation'] = test_df_saint['prior_question_had_explanation'].fillna(value = False ).astype(int)
 test_df_saint = test_df_saint.merge(questions_df[["question_id","part"]], how = "left",left_on = 'content_id', right_on = 'question_id')
 prev_test_df = test_df_saint.copy()
test_df_saint = test_df_saint[test_df_saint.content_type_id == False]
test_df_saint = feature_time_lag(test_df_saint, time_dict)
 test_dataset = TestDataset(group, test_df_saint, n_skill)
 test_dataloader = DataLoader(test_dataset, batch_size=51200, shuffle=False)
 outs1 = []
outs2 = []
outs3 = []
for item in test_dataloader:
exercise = item[0].to(device ).long()
part = item[1].to(device ).long()
response = item[2].to(device ).long()
elapsed_time = item[3].to(device ).long()
lag_time = item[4].to(device ).long()
pri_exp = item[5].to(device ).long()
with torch.no_grad() :
output1 = model1(exercise, part, response, elapsed_time, lag_time, pri_exp)
 output2 = model2(exercise, part, response, elapsed_time, lag_time, pri_exp)
 output3 = model3(exercise, part, response, elapsed_time, lag_time, pri_exp)
 outs1.extend(torch.sigmoid(output1)[:, -1].view(-1 ).data.cpu().numpy())
 outs2.extend(torch.sigmoid(output2)[:, -1].view(-1 ).data.cpu().numpy())
 outs3.extend(torch.sigmoid(output3)[:, -1].view(-1 ).data.cpu().numpy())
 if previous_test_df.shape[0] != 0:
previous_test_df['answered_correctly'] = eval(test_df['prior_group_answers_correct'].iloc[0])
 previous_test_df['user_answer'] = eval(test_df['prior_group_responses'].iloc[0])
 previous_test_df = previous_test_df[previous_test_df['content_type_id'] == 0]
previous_test_df['q_counter'] = 1
update_dict(previous_test_df, dict_question_user_cnt, 'user_id', 'q_counter')
 update_dict(previous_test_df, dict_correct_answers_user_cnt, 'user_id', 'answered_correctly')
 previous_test_df['prior_question_had_explanation'] = previous_test_df['prior_question_had_explanation'].fillna(0 ).astype(int)
 update_dict(previous_test_df, dict_question_explonation_user_cnt, 'user_id', 'prior_question_had_explanation')
 update_dict(previous_test_df, dict_questionid_part_tag12_avgtarget, 'content_id', 'q_counter', 0)
 update_dict(previous_test_df, dict_questionid_part_tag12_avgtarget, 'content_id', 'answered_correctly', 1)
 answered_correctly_avg_content_global = 0.5
trust_border = 5
for c in previous_test_df['content_id'].unique() :
answered_correctly_avg_content = dict_questionid_part_tag12_avgtarget[c][1] / dict_questionid_part_tag12_avgtarget[c][0]
if dict_questionid_part_tag12_avgtarget[c][0] >= trust_border:
dict_questionid_part_tag12_avgtarget[c][2] = answered_correctly_avg_content
else:
border_calc_K = np.minimum(dict_questionid_part_tag12_avgtarget[c][0], trust_border)
 border_calc_L =(trust_border - border_calc_K)/ trust_border
dict_questionid_part_tag12_avgtarget[c][2] =(
 answered_correctly_avg_content * border_calc_K / trust_border +
answered_correctly_avg_content_global * border_calc_L 
)
 dict_user_question_part_accuracy = dict_user_question_part_accuracy_update(previous_test_df, dict_user_question_part_accuracy)
 dict_user_slice_accuracy_5 = user_slice_accuracy_n_update(previous_test_df, dict_user_slice_accuracy_5, border=5)
 dict_user_slice_accuracy_20 = user_slice_accuracy_n_update(previous_test_df, dict_user_slice_accuracy_20, border=20)
 dict_user_slice_accuracy_50 = user_slice_accuracy_n_update(previous_test_df, dict_user_slice_accuracy_50, border=50)
 dict_user_slice_accuracy_session_3 = user_slice_accuracy_session_update(previous_test_df, dict_user_slice_accuracy_session_3, session_max_time=3)
 dict_user_slice_accuracy_session_12 = user_slice_accuracy_session_update(previous_test_df, dict_user_slice_accuracy_session_12, session_max_time=12)
 dict_user_slice_accuracy_session_48 = user_slice_accuracy_session_update(previous_test_df, dict_user_slice_accuracy_session_48, session_max_time=48)
 dict_user_correct_incorrect_timestamp = user_correct_incorrect_timestamp_update(previous_test_df, dict_user_correct_incorrect_timestamp)
 dict_user_question_tag_accuracy = user_question_tag_accuracy_update(previous_test_df, dict_user_question_tag_accuracy)
 dict_user_priorq_expl_types = user_priorq_expl_types_update(previous_test_df, dict_user_priorq_expl_types)
 dict_user_answer_mode_10 = user_answer_mode_n_update(previous_test_df, dict_user_answer_mode_10, border=10)
 dict_user_answer_mode_50 = user_answer_mode_n_update(previous_test_df, dict_user_answer_mode_50, border=50)
 dict_question_bundle_accuracy = question_bundle_accuracy_update(previous_test_df, dict_question_bundle_accuracy)
 update_user_feats(previous_test_df)
 for user_id, answered_correctly, t in zip(previous_test_df['user_id'].values, previous_test_df['answered_correctly'].values, previous_test_df['timestamp'].values):
if user_id in lt_correct_dict['timestamp']:
if t == lt_correct_dict['timestamp'][user_id]:
lt_correct_dict['last_timestamp_correct_cnt'][user_id] += 1
lt_correct_dict['last_timestamp_correct_sum'][user_id] += answered_correctly
else:
lt_correct_dict['timestamp'].update({user_id:t})
 lt_correct_dict['last_timestamp_correct_cnt'][user_id] = 1
lt_correct_dict['last_timestamp_correct_sum'][user_id] = answered_correctly
else:
lt_correct_dict['timestamp'].update({user_id:t})
 lt_correct_dict['last_timestamp_correct_cnt'].update({user_id:1})
 lt_correct_dict['last_timestamp_correct_sum'].update({user_id:answered_correctly})
 lt_correct_dict['last_timestamp_correct_pct'][user_id] = lt_correct_dict['last_timestamp_correct_sum'][user_id] / lt_correct_dict['last_timestamp_correct_cnt'][user_id]
previous_test_df = test_df.copy()
test_df2 = test_df.copy()
prior_question_elapsed_time_mean = 25452.541
test_df2 = test_df[test_df['content_type_id'] == 0].reset_index(drop=True)
 lt_correct_cnt = np.zeros(len(test_df2), dtype=np.int8)
 lt_correct_sum = np.zeros(len(test_df2), dtype=np.int8)
 lt_correct_pct = np.zeros(len(test_df2), dtype=np.float16)
 test_df2 = add_user_feats_without_update(test_df2)
 test_df2 = add_uq_feats_and_update(test_df2)
 test_df2 = lagtime_for_test(test_df2)
 for i,(user_id, t)in enumerate(zip(test_df2['user_id'].values, test_df2['timestamp'].values)) :
if user_id in lt_correct_dict['timestamp']:
lt_correct_cnt[i] = lt_correct_dict['last_timestamp_correct_cnt'][user_id]
lt_correct_sum[i] = lt_correct_dict['last_timestamp_correct_sum'][user_id]
lt_correct_pct[i] = lt_correct_dict['last_timestamp_correct_pct'][user_id]
else:
lt_correct_cnt[i] = -1
lt_correct_sum[i] = -1
lt_correct_pct[i] = -1
test_df2['last_timestamp_correct_cnt'] = lt_correct_cnt
test_df2['last_timestamp_correct_sum'] = lt_correct_sum
test_df2['last_timestamp_correct_pct'] = lt_correct_pct
additional_test_feature = test_df2[['last_timestamp_correct_cnt', 'last_timestamp_correct_sum',
'last_timestamp_correct_pct', 'lag_time', 'lag_time2',
'lag_time3', 'curr_user_time_diff', 'curr_user_time_diff_mean',
'curr_user_elapsed_time_diff', 'curr_uq_time_diff']].values
test_df['prior_question_elapsed_time'] = test_df['prior_question_elapsed_time'].fillna(prior_question_elapsed_time_mean)
 test_df['prior_group_answers_correct'] = 0
test_df['prior_group_responses'] = 0
test_df = test_df.replace([np.inf, -np.inf], np.nan)
 test_df = test_df.fillna(0)
 test_df = dict_user_timestampsdelta_get_update_3(test_df, dict_user_timestampsdelta_3)
 test_df['user_slice_accuracy_5'] = user_slice_accuracy_n_get(test_df, dict_user_slice_accuracy_5)
 test_df['user_slice_accuracy_20'] = user_slice_accuracy_n_get(test_df, dict_user_slice_accuracy_20)
 test_df['user_slice_accuracy_50'] = user_slice_accuracy_n_get(test_df, dict_user_slice_accuracy_50)
 test_df['user_slice_accuracy_session_3'] = user_slice_accuracy_session_get(test_df, dict_user_slice_accuracy_session_3, session_max_time=3)
 test_df['user_slice_accuracy_session_12'] = user_slice_accuracy_session_get(test_df, dict_user_slice_accuracy_session_12, session_max_time=12)
 test_df['user_slice_accuracy_session_48'] = user_slice_accuracy_session_get(test_df, dict_user_slice_accuracy_session_48, session_max_time=48)
 test_df['task_container_freq'] = test_df.groupby(['user_id', 'task_container_id'])['task_container_id'].transform('count')
 test_df['task_container_counter'] = test_df[['user_id', 'task_container_id', 'content_id']].groupby(['user_id', 'task_container_id'], as_index=False ).agg(['cumcount'])+ 1
test_df['user_question_attempt_cnt'] = user_question_attempt_cnt_get_update(test_df, dict_user_question_attempt_cnt ).astype(np.int16)
 test_df['lecture_cnt'] = user_lecture_cnt(test_df, dict_user_lectures_part)
 test_df = user_lectures_typeof_cnt(test_df, dict_user_lectures_typeof_cnt)
 test_df['part_q'] = get_q_l(test_df, dict_questions, 'part')
 test_df['tags_list'] = get_q_l(test_df, dict_questions, 'tags_list')
 test_df['part_l'] = get_q_l(test_df, dict_lectures, 'part')
 test_df['tag_l'] = get_q_l(test_df, dict_lectures, 'tag')
 test_df['part_l_q_cnt'] = user_lectures_part(test_df, dict_user_lectures_part ).astype(np.int16)
 test_df['tag_l_q_equal_cnt'] = user_l_q_tag_equal(test_df, dict_user_l_q_tag_equal ).astype(np.int16)
 test_df['user_question_part_accuracy'] = dict_user_question_part_accuracy_get(test_df, dict_user_question_part_accuracy)
 test_df['user_question_tag_accuracy'] = user_question_tag_accuracy_get(test_df, dict_user_question_tag_accuracy)
 test_df = user_correct_incorrect_timestamp_get(test_df, dict_user_correct_incorrect_timestamp)
 test_df = user_slice_question_time_mean_session(test_df, dict_user_slice_question_time_mean_session)
 test_df = user_priorq_expl_types_get(test_df, dict_user_priorq_expl_types)
 test_df['user_answer_mode_10'] = user_answer_mode_n_get(test_df, dict_user_answer_mode_10, border=10)
 test_df['user_answer_mode_50'] = user_answer_mode_n_get(test_df, dict_user_answer_mode_50, border=50)
 test_df = question_bundle_id_get(test_df, dict_questions)
 test_df = user_bundle_cluster_get_update(test_df, dict_user_bundle_cluster)
 test_df = question_bundle_accuracy_get(test_df, dict_question_bundle_accuracy)
 previous_test_df['part_q'] = test_df['part_q'].values
previous_test_df['tags_list'] = test_df['tags_list'].values
previous_test_df['task_container_freq'] = test_df['task_container_freq'].values
previous_test_df['bundle_id'] = test_df['bundle_id'].values
previous_test_df['first_bundle_id_cluster'] = test_df['first_bundle_id_cluster'].values
test_df.drop(columns=['part_l', 'part_q', 'tag_l', 'tags_list', 'bundle_id'], inplace=True)
 test_df = test_df[test_df['content_type_id'] == 0]
if test_df.shape[0] > 0:
np_test_df = test_df.to_numpy(dtype=np.float64, na_value=0)
 col_idx = features_map['user_id']
np_test_df = add_feats_from_dict(np_test_df, dict_question_user_cnt, col_idx)
 np_test_df = add_feats_from_dict(np_test_df, dict_correct_answers_user_cnt, col_idx)
 col_numerator = features_map['correct_answers_user_cnt']
col_denominator = features_map['question_user_cnt']
np_test_df = np.c_[ np_test_df, np.divide(np_test_df[:,col_numerator], np_test_df[:,col_denominator],
out=np.zeros_like(np_test_df[:,col_denominator]),
where=np_test_df[:,col_denominator]!=0)]
np_test_df = add_feats_from_dict(np_test_df, dict_question_explonation_user_cnt, col_idx)
 col_numerator = features_map['prior_question_had_explanation_user_cnt']
col_denominator = features_map['question_user_cnt']
np_test_df = np.c_[ np_test_df, np.divide(np_test_df[:,col_numerator], np_test_df[:,col_denominator],
out=np.zeros_like(np_test_df[:,col_denominator]),
where=np_test_df[:,col_denominator]!=0)]
for i in [dict_questionid_part_tag12_avgtarget_map['answered_correctly_avg_content_smooth'],
dict_questionid_part_tag12_avgtarget_map['part'],
dict_questionid_part_tag12_avgtarget_map['answered_correctly_avg_part'],
dict_questionid_part_tag12_avgtarget_map['answered_correctly_avg_tag_1'],
dict_questionid_part_tag12_avgtarget_map['answered_correctly_avg_tag_2'],
]:
np_test_df = add_feats_from_dict_got_new_user(np_test_df, dict_questionid_part_tag12_avgtarget, col_idx=features_map['content_id'], col_dict=i)
 col_numerator = features_map['correct_answers_user_prc']
col_denominator = features_map['content_id_mean']
np_test_df = np.c_[ np_test_df, np.divide(np_test_df[:,col_numerator], np_test_df[:,col_denominator], out=np.zeros_like(np_test_df[:,col_denominator]), where=np_test_df[:,col_denominator]!=0)]
user_acc = features_map['correct_answers_user_prc']
question_acc = features_map['content_id_mean']
np_test_df = np.c_[ np_test_df, 2*(np_test_df[:,user_acc] * np_test_df[:,question_acc])/(np_test_df[:,user_acc] + np_test_df[:,question_acc])]
pred_feature = np_test_df[:,train_cols_clf]
pred_feature = np.concatenate(( pred_feature, additional_test_feature), axis=1)
 pred = pd.DataFrame()
pred['score_lgbm'] = lgb_model.predict(pred_feature)
 pred['score_cat'] = cat_model.predict_proba(pred_feature)[:,1]
pred['score_saint1'] = np.array(outs1)
 pred['score_saint2'] = np.array(outs2)
 pred['score_saint3'] = np.array(outs3)
 test_df['answered_correctly'] =(c1*pred['score_lgbm'] + c1_2 * pred['score_cat'] + c2*pred['score_saint1'] + c3*pred['score_saint2']+ c4*pred['score_saint3'] ).values
else:
test_df['answered_correctly'] = np.array([], dtype = np.float32)
 env.predict(test_df[['row_id', 'answered_correctly']] )<import_modules>
x_test = np.array(df_test)
 x_test = np.array([np.reshape(i,(28, 28, 1)) for i in x_test])
 x_test = x_test/255.0
import torch
import pandas as pd
from saintmodel import SaintModel, SaintLightningModule, SaintHistory
from saintsubmit import load_saint_config, SaintPredictor, make_submission<choose_model_class>
X_train, X_test, Y_train, Y_test = train_test_split(x_train, y_train, test_size=0.2, stratify=y_train )
args = load_saint_config()
model = SaintModel(
 seq_len=args.seq_len, n_dim=args.n_dim, std=args.std, dropout=args.dropout, nhead=args.nhead, n_layers=args.n_layers 
)
 module = SaintLightningModule(args, model )<load_pretrained>
model = keras.models.Sequential()
model.add(keras.layers.Conv2D(filters=32, kernel_size=(3,3), kernel_initializer='random_uniform', padding='same', activation='relu', input_shape=(X_train.shape[1:])))
 model.add(keras.layers.Conv2D(filters=32, kernel_size=(3,3), kernel_initializer='random_uniform', padding='same', activation='relu'))
model.add(keras.layers.MaxPool2D(pool_size=(2,2)))
 model.add(keras.layers.Conv2D(filters=64, kernel_size=(5,5), kernel_initializer='random_uniform', padding='same', activation='relu'))
model.add(keras.layers.Conv2D(filters=64, kernel_size=(5,5), kernel_initializer='random_uniform', padding='same', activation='relu'))
model.add(keras.layers.MaxPool2D(pool_size=(2,2)))
 model.add(keras.layers.Conv2D(filters=128, kernel_size=(7,7), kernel_initializer='random_uniform', padding='same', activation='relu'))
model.add(keras.layers.Conv2D(filters=128, kernel_size=(7,7), kernel_initializer='random_uniform', padding='same', activation='relu'))
model.add(keras.layers.MaxPool2D(pool_size=(3,3)))
 model.add(keras.layers.Conv2D(filters=256, kernel_size=(7,7), padding='same'))
model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.Activation('relu'))
model.add(keras.layers.Dense(units=256, activation='relu'))
model.add(keras.layers.Dropout(0.5))
model.add(keras.layers.Dense(units=100, activation='relu'))
model.add(keras.layers.Dropout(0.5))
model.add(keras.layers.Flatten())
 model.add(keras.layers.Dense(units=y_train.shape[1], activation='softmax'))
print(model.summary())
 model.compile(loss='categorical_crossentropy',optimizer='Adam', metrics=['accuracy'] )
%%time
module.load_state_dict(torch.load('/kaggle/input/riiid-saintp-solution/saint.ckpt')['state_dict'] )<load_pretrained>
es = EarlyStopping(monitor='loss', mode='min', verbose=1, patience=5)
 filepath = "model.h5"
ckpt = ModelCheckpoint(filepath, monitor='loss', verbose=1, save_best_only=True, mode='min')
 rlp = ReduceLROnPlateau(monitor='loss', patience=2, factor=0.2 )
%%time
last_history = pd.read_pickle('/kaggle/input/riiid-saintp-solution/last_history.pickle')
 last_timestamp = pd.read_pickle('/kaggle/input/riiid-saintp-solution/last_timestamp.pickle')
 last_user_count = pd.read_pickle('/kaggle/input/riiid-saintp-solution/last_user_count.pickle')
 dict_lag = pd.read_pickle('/kaggle/input/riiid-saintp-solution/dict_lag.pickle')
 dict_elapsed = pd.read_pickle('/kaggle/input/riiid-saintp-solution/dict_elapsed.pickle')
 dict_user_count = pd.read_pickle('/kaggle/input/riiid-saintp-solution/dict_user_count.pickle' )<set_options>
history = model.fit(X_train, Y_train, batch_size=500, callbacks=[es, ckpt, rlp], epochs=100, validation_data=(X_test,Y_test))
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
 module.to(device )<choose_model_class>
id_img = []
label = []
for i in range(len(x_test)) :
id_img.append(i+1)
 label.append(np.argmax(model.predict(x_test[i:i+1])))
 img_id = np.array(id_img)
 label = np.array(label )
<set_options><EOS> 
op_df = pd.DataFrame()
op_df['ImageId'] = img_id
op_df['Label'] = label
op_df.to_csv("submission.csv", index=False )