Kaggle competition: Mechanisms of Action (MoA) Prediction
<SOS> metric: MeanColumnwiseLogLoss Kaggle data source: mechanisms-of-action-(moa)-prediction<categorify>
import numpy as np
import pandas as pd
import tensorflow as tf
import tensorflow.keras.backend as K
import tensorflow.keras.layers as L
import tensorflow.keras.models as M
from tensorflow.keras.callbacks import ReduceLROnPlateau
import tensorflow_addons as tfa
from sklearn.model_selection import KFold
from sklearn.metrics import log_loss
from tqdm.notebook import tqdm
diagnostic_u_dict=defaultdict(int)
 train=add_is_diagnostic(train, diagnostic_u_dict)
 valid=add_is_diagnostic(valid, diagnostic_u_dict )<compute_train_metric>
train_features = pd.read_csv('.. /input/lish-moa/train_features.csv')
 train_targets = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 test_features = pd.read_csv('.. /input/lish-moa/test_features.csv')
 ss = pd.read_csv('.. /input/lish-moa/sample_submission.csv' )
def win_probability(team1, team2):
delta_mu = team1.mu - team2.mu
sum_sigma = sum([team1.sigma ** 2, team2.sigma ** 2])
 size = 2
denom = math.sqrt(size *(0.05 * 0.05)+ sum_sigma)
 ts = trueskill.global_env()
return ts.cdf(delta_mu / denom)
 def add_trueskill(df, user_trueskill_dict, question_trueskill_dict, verbose=True):
wp = np.zeros(len(df), dtype=np.float32)
 umu = np.zeros(len(df), dtype=np.float32)
 usigma = np.zeros(len(df), dtype=np.float32)
 qmu = np.zeros(len(df), dtype=np.float32)
 qsigma = np.zeros(len(df), dtype=np.float32)
 for cnt,row in(enumerate(tqdm(df[['user_id','content_id','answered_correctly']].values)) if verbose else enumerate(df[['user_id','content_id','answered_correctly']].values)) :
user_id=int(row[0])
 content_id=int(row[1])
 answered_correctly=int(row[2])
 old_user_rating = user_trueskill_dict[user_id]
old_question_rating = question_trueskill_dict[content_id]
wp[cnt] = win_probability(old_user_rating,old_question_rating)
 umu[cnt] = old_user_rating.mu
usigma[cnt] = old_user_rating.sigma
qmu[cnt] = old_question_rating.mu
qsigma[cnt] = old_question_rating.sigma
if answered_correctly == 1:
new_user_rating,new_question_rating = rate_1vs1(old_user_rating,old_question_rating)
 else:
new_question_rating,new_user_rating = rate_1vs1(old_question_rating,old_user_rating)
 user_trueskill_dict[user_id] = new_user_rating
question_trueskill_dict[content_id] = new_question_rating
df['wp_ts']=wp
df['umu_ts']=umu
df['usigma_ts']=usigma
df['qmu_ts']=qmu
df['qsigma_ts']=qsigma
return df
def update_trueskill(df, user_trueskill_dict, question_trueskill_dict, verbose=True):
for cnt,row in(enumerate(tqdm(df[['user_id','content_id','answered_correctly']].values)) if verbose else enumerate(df[['user_id','content_id','answered_correctly']].values)) :
user_id=int(row[0])
 content_id=int(row[1])
 answered_correctly=int(row[2])
 old_user_rating = user_trueskill_dict[user_id]
old_question_rating = question_trueskill_dict[content_id]
if answered_correctly == 1:
new_user_rating,new_question_rating = rate_1vs1(old_user_rating,old_question_rating)
 else:
new_question_rating,new_user_rating = rate_1vs1(old_question_rating,old_user_rating)
 user_trueskill_dict[user_id] = new_user_rating
question_trueskill_dict[content_id] = new_question_rating
def add_trueskill_without_update(df, user_trueskill_dict, question_trueskill_dict, verbose=True):
wp = np.zeros(len(df), dtype=np.float32)
 umu = np.zeros(len(df), dtype=np.float32)
 usigma = np.zeros(len(df), dtype=np.float32)
 qmu = np.zeros(len(df), dtype=np.float32)
 qsigma = np.zeros(len(df), dtype=np.float32)
 for cnt,row in(enumerate(tqdm(df[['user_id','content_id']].values)) if verbose else enumerate(df[['user_id','content_id']].values)) :
user_id=int(row[0])
 content_id=int(row[1])
 old_user_rating = user_trueskill_dict[user_id]
old_question_rating = question_trueskill_dict[content_id]
wp[cnt] = win_probability(old_user_rating,old_question_rating)
 umu[cnt] = old_user_rating.mu
usigma[cnt] = old_user_rating.sigma
qmu[cnt] = old_question_rating.mu
qsigma[cnt] = old_question_rating.sigma
df['wp_ts']=wp
df['umu_ts']=umu
df['usigma_ts']=usigma
df['qmu_ts']=qmu
df['qsigma_ts']=qsigma
return df<feature_engineering>
def preprocess(df):
df.loc[:, 'cp_type'] = df.loc[:, 'cp_type'].map({'trt_cp': 0, 'ctl_vehicle': 1})
 df.loc[:, 'cp_dose'] = df.loc[:, 'cp_dose'].map({'D1': 0, 'D2': 1})
 del df['sig_id']
return df
train = preprocess(train_features)
 test = preprocess(test_features)
 del train_targets['sig_id']
user_trueskill_dict = defaultdict(lambda:Rating())
 question_trueskill_dict = defaultdict(lambda:Rating())
 train=add_trueskill(train, user_trueskill_dict, question_trueskill_dict)
 valid=add_trueskill(valid, user_trueskill_dict, question_trueskill_dict )<data_type_conversions>
def create_model(num_columns):
model = tf.keras.Sequential([
tf.keras.layers.Input(num_columns),
tf.keras.layers.BatchNormalization() ,
tfa.layers.WeightNormalization(tf.keras.layers.Dense(6144, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.4),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(6144, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.4),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(206, activation="sigmoid"))
])
 model.compile(optimizer=tfa.optimizers.Lookahead(tf.optimizers.Adam(lr=1e-4), sync_period=10),
loss='binary_crossentropy' 
)
 return model
def add_new_columns(df):
df['count_u_nondiagnostic']=df['count_u'] - df['is_diagnostic_sum']
df['exp_avg_u_corrected']=(df['exp_sum_u'] /(df['count_u_nondiagnostic'])).replace(np.inf, 0 ).fillna(value=0 ).astype('float32')
 df['pp_count']=df['count_up1']+df['count_up3']+df['count_up4']+df['count_up6']+df['count_up7']
df['pp_ratio']=(df['pp_count']/df['count_u'] ).replace(np.inf, np.nan)
 df['listening_count']=df['count_up1']+df['count_up2']+df['count_up3']+df['count_up4']
df['listening_ratio']=(df['listening_count']/df['count_u'] ).replace(np.inf, np.nan)
 df['relative_dif']=(( 1-df['acc_avg_c'])/ df['dif_avg_u'])
 df['relative_dpt']=(( 1-df['acc_avg_c'])/ df['dpt_avg_u'] ).replace(np.inf, np.nan)
 df['time_per_count_u']=(df['timestamp'] / df['count_u'] ).replace(np.inf, np.nan)
 df['notacc_sum_u']=df['count_u'] - df['acc_sum_u']
df['notacc_sum_up']=df['count_up'] - df['acc_sum_up']
df['notacc_sum_uc']=df['count_uc'] - df['acc_sum_uc']
df['notacc_sum_c']=df['count_c'] - df['acc_sum_c']
return df
def fillna_df(df):
df['acc_avg_u']=df['acc_avg_u'].fillna(value=0.6432132412788166 ).astype('float32')
 df['exp_avg_u']=df['exp_avg_u'].fillna(value=0.8200458704602632 ).astype('float32')
 df['time_avg_u']=df['time_avg_u'].fillna(value=25497.400011014917 ).astype('float32')
 df['acc_avg_up']=df['acc_avg_up'].fillna(value=0.6416028023438022 ).astype('float32')
 df['exp_avg_up']=df['exp_avg_up'].fillna(value=0.8654814629737391 ).astype('float32')
 df['time_avg_up']=df['time_avg_up'].fillna(value=26303.666782974677 ).astype('float32')
 df['acc_avg_c']=df['acc_avg_c'].fillna(value=0.6572577059673576 ).astype('float32')
 df['exp_avg_b']=df['exp_avg_b'].fillna(value=0.9077998180208283 ).astype('float32')
 df['time_avg_b']=df['time_avg_b'].fillna(value=26036.627957044348 ).astype('float32')
 df['acc_avg_uc']=df['acc_avg_uc'].fillna(value=0.36168508143684996 ).astype('float32')
 df['exp_avg_ub']=df['exp_avg_ub'].fillna(value=0.8596988233077392 ).astype('float32')
 df['time_avg_ub']=df['time_avg_ub'].fillna(value=26215.743786481285 ).astype('float32')
 df['time_per_count_u']=df['time_per_count_u'].fillna(value=18856.430915581953 ).astype('float32')
 df['dpt_avg_u']=df['dpt_avg_u'].fillna(value=0.19122234479970104 ).astype('float32')
 df['dif_avg_u']=df['dif_avg_u'].fillna(value=0.348174949891143 ).astype('float32')
 df['relative_dif']=df['relative_dif'].fillna(value=0.9982924288104749 ).astype('float32')
 df['relative_dpt']=df['relative_dpt'].fillna(value=1.924812794869729 ).astype('float32')
 df['pp_ratio']=df['pp_ratio'].fillna(value=0.4008204857577879 ).astype('float32')
 df['listening_ratio']=df['listening_ratio'].fillna(value=0.4540805930573127 ).astype('float32')
 return df<data_type_conversions>
top_feats = [ 0, 1, 2, 3, 5, 6, 8, 9, 10, 11, 12, 14, 15,
16, 18, 19, 20, 21, 23, 24, 25, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 39, 40, 41, 42, 44, 45, 46,
48, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
63, 64, 65, 66, 68, 69, 70, 71, 72, 73, 74, 75, 76,
78, 79, 80, 81, 82, 83, 84, 86, 87, 88, 89, 90, 92,
93, 94, 95, 96, 97, 99, 100, 101, 103, 104, 105, 106, 107,
108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120,
121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 132, 133, 134,
135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147,
149, 150, 151, 152, 153, 154, 155, 157, 159, 160, 161, 163, 164,
165, 166, 167, 168, 169, 170, 172, 173, 175, 176, 177, 178, 180,
181, 182, 183, 184, 186, 187, 188, 189, 190, 191, 192, 193, 195,
197, 198, 199, 202, 203, 205, 206, 208, 209, 210, 211, 212, 213,
214, 215, 218, 219, 220, 221, 222, 224, 225, 227, 228, 229, 230,
231, 232, 233, 234, 236, 238, 239, 240, 241, 242, 243, 244, 245,
246, 248, 249, 250, 251, 253, 254, 255, 256, 257, 258, 259, 260,
261, 263, 265, 266, 268, 270, 271, 272, 273, 275, 276, 277, 279,
282, 283, 286, 287, 288, 289, 290, 294, 295, 296, 297, 299, 300,
301, 302, 303, 304, 305, 306, 308, 309, 310, 311, 312, 313, 315,
316, 317, 320, 321, 322, 324, 325, 326, 327, 328, 329, 330, 331,
332, 333, 334, 335, 338, 339, 340, 341, 343, 344, 345, 346, 347,
349, 350, 351, 352, 353, 355, 356, 357, 358, 359, 360, 361, 362,
363, 364, 365, 366, 368, 369, 370, 371, 372, 374, 375, 376, 377,
378, 379, 380, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391,
392, 393, 394, 395, 397, 398, 399, 400, 401, 403, 405, 406, 407,
408, 410, 411, 412, 413, 414, 415, 417, 418, 419, 420, 421, 422,
423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435,
436, 437, 438, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450,
452, 453, 454, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465,
466, 468, 469, 471, 472, 473, 474, 475, 476, 477, 478, 479, 482,
483, 485, 486, 487, 488, 489, 491, 492, 494, 495, 496, 500, 501,
502, 503, 505, 506, 507, 509, 510, 511, 512, 513, 514, 516, 517,
518, 519, 521, 523, 525, 526, 527, 528, 529, 530, 531, 532, 533,
534, 536, 537, 538, 539, 540, 541, 542, 543, 544, 545, 546, 547,
549, 550, 553, 554, 555, 556, 557, 558, 559, 560, 561, 562, 563,
564, 565, 566, 567, 569, 570, 571, 572, 573, 574, 575, 577, 580,
581, 582, 583, 586, 587, 590, 591, 592, 593, 595, 596, 597, 598,
599, 600, 601, 602, 603, 605, 607, 608, 609, 611, 612, 613, 614,
615, 616, 617, 619, 622, 623, 625, 627, 630, 631, 632, 633, 634,
635, 637, 638, 639, 642, 643, 644, 645, 646, 647, 649, 650, 651,
652, 654, 655, 658, 659, 660, 661, 662, 663, 664, 666, 667, 668,
669, 670, 672, 674, 675, 676, 677, 678, 680, 681, 682, 684, 685,
686, 687, 688, 689, 691, 692, 694, 695, 696, 697, 699, 700, 701,
702, 703, 704, 705, 707, 708, 709, 711, 712, 713, 714, 715, 716,
717, 723, 725, 727, 728, 729, 730, 731, 732, 734, 736, 737, 738,
739, 740, 741, 742, 743, 744, 745, 746, 747, 748, 749, 750, 751,
752, 753, 754, 755, 756, 758, 759, 760, 761, 762, 763, 764, 765,
766, 767, 769, 770, 771, 772, 774, 775, 780, 781, 782, 783, 784,
785, 787, 788, 790, 793, 795, 797, 799, 800, 801, 805, 808, 809,
811, 812, 813, 816, 819, 820, 821, 822, 823, 825, 826, 827, 829,
831, 832, 833, 834, 835, 837, 838, 839, 840, 841, 842, 844, 845,
846, 847, 848, 850, 851, 852, 854, 855, 856, 858, 860, 861, 862,
864, 867, 868, 870, 871, 873, 874]
print(len(top_feats))
train=add_new_columns(train)
 valid=add_new_columns(valid)
 train=fillna_df(train)
 valid=fillna_df(valid )<drop_column>
N_STARTS = 3
res = train_targets.copy()
ss.loc[:, train_targets.columns] = 0
res.loc[:, train_targets.columns] = 0
for seed in range(N_STARTS):
for n,(tr, te)in enumerate(KFold(n_splits=5, random_state=seed, shuffle=True ).split(train_targets)) :
print(f'Fold {n+1}')
 model = create_model(len(top_feats))
reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, verbose=1, epsilon=1e-4, mode='min')
 model.fit(train.values[tr][:, top_feats],
train_targets.astype(float ).values[tr],
validation_data=(train.values[te][:, top_feats], train_targets.astype(float ).values[te]),
epochs=45, batch_size=128,
callbacks=[reduce_lr_loss], verbose=2 
)
 ss.loc[:, train_targets.columns] += model.predict(test_features.values[:, top_feats])
 res.loc[te, train_targets.columns] += model.predict(train.values[te][:, top_feats])
 print('')
 ss.loc[:, train_targets.columns] /=(( n+1)* N_STARTS)
 res.loc[:, train_targets.columns] /= N_STARTS
dbname = 'feat.db'
conn = sqlite3.connect(dbname)
 cur = conn.cursor()
cur.execute('CREATE TABLE u_feats(user_id INTEGER, \
acc_sum_u INTEGER, \
acc_std_u INTEGER, \
acc_hist_u REAL, \
acc_hist2_u REAL, \
exp_sum_u INTEGER, \
exp_std_u INTEGER, \
exp_hist_u REAL, \
exp_hist2_u REAL, \
time_sum_u INTEGER, \
time_std_u INTEGER, \
time_hist_u REAL, \
time_hist2_u REAL, \
dpt_sum_u INTEGER, \
dpt_std_u INTEGER, \
dpt_hist_u REAL, \
dpt_hist2_u REAL, \
dif_sum_u INTEGER, \
dif_std_u INTEGER, \
dif_hist_u REAL, \
dif_hist2_u REAL, \
count_u INTEGER, \
count_lecture_u INTEGER, \
last_time_u INTEGER, \
last_time2_u INTEGER, \
last_time3_u INTEGER, \
last_time4_u INTEGER, \
last_time5_u INTEGER, \
last_time6_u INTEGER, \
tfl_std_u REAL, \
tfl_hist_u REAL, \
tfl_hist2_u REAL, \
PRIMARY KEY(user_id)) ')
 conn.commit()
cur = conn.cursor()
user_ids=list(count_u_dict.keys())
 for user_id in tqdm(user_ids):
acc_sum_u=acc_sum_u_dict[user_id]
acc_std_u=acc_std_u_dict[user_id]
acc_hist_u=acc_hist_u_dict[user_id]
acc_hist2_u=acc_hist2_u_dict[user_id]
exp_sum_u=exp_sum_u_dict[user_id]
exp_std_u=exp_std_u_dict[user_id]
exp_hist_u=exp_hist_u_dict[user_id]
exp_hist2_u=exp_hist2_u_dict[user_id]
time_sum_u=time_sum_u_dict[user_id]
time_std_u=time_std_u_dict[user_id]
time_hist_u=time_hist_u_dict[user_id]
time_hist2_u=time_hist2_u_dict[user_id]
dpt_sum_u=dpt_sum_u_dict[user_id]
dpt_std_u=dpt_std_u_dict[user_id]
dpt_hist_u=dpt_hist_u_dict[user_id]
dpt_hist2_u=dpt_hist2_u_dict[user_id]
dif_sum_u=dif_sum_u_dict[user_id]
dif_std_u=dif_std_u_dict[user_id]
dif_hist_u=dif_hist_u_dict[user_id]
dif_hist2_u=dif_hist2_u_dict[user_id]
count_u=count_u_dict[user_id]
count_lecture_u=count_lecture_u_dict[user_id]
last_time_u=last_time_u_dict[user_id]
last_time2_u=last_time2_u_dict[user_id]
last_time3_u=last_time3_u_dict[user_id]
last_time4_u=last_time4_u_dict[user_id]
last_time5_u=last_time5_u_dict[user_id]
last_time6_u=last_time6_u_dict[user_id]
tfl_std_u=tfl_std_u_dict[user_id]
tfl_hist_u=tfl_hist_u_dict[user_id]
tfl_hist2_u=tfl_hist2_u_dict[user_id]
cur.execute("INSERT INTO u_feats VALUES(:user_id,\
:acc_sum_u,:acc_std_u,:acc_hist_u,:acc_hist2_u,\
:exp_sum_u,:exp_std_u,:exp_hist_u,:exp_hist2_u,\
:time_sum_u,:time_std_u,:time_hist_u,:time_hist2_u,\
:dpt_sum_u,:dpt_std_u,:dpt_hist_u,:dpt_hist2_u,\
:dif_sum_u,:dif_std_u,:dif_hist_u,:dif_hist2_u,\
:count_u,:count_lecture_u,\
:last_time_u,:last_time2_u,:last_time3_u,:last_time4_u,:last_time5_u,:last_time6_u,\
:tfl_std_u,:tfl_hist_u,:tfl_hist2_u)",(user_id,acc_sum_u,acc_std_u,acc_hist_u,acc_hist2_u,exp_sum_u,exp_std_u,exp_hist_u,exp_hist2_u,time_sum_u,time_std_u,time_hist_u,time_hist2_u,dpt_sum_u,dpt_std_u,dpt_hist_u,dpt_hist2_u,dif_sum_u,dif_std_u,dif_hist_u,dif_hist2_u,count_u,count_lecture_u,last_time_u,last_time2_u,last_time3_u,last_time4_u,last_time5_u,last_time6_u,tfl_std_u,tfl_hist_u,tfl_hist2_u))
conn.commit()
cur = conn.cursor()
cur.execute('CREATE TABLE c_feats(content_id INTEGER,\
acc_sum_c INTEGER, \
acc_std_c REAL, \
count_c INTEGER, \
PRIMARY KEY(content_id)) ')
 conn.commit()
cur = conn.cursor()
content_ids=list(count_c_dict.keys())
 for content_id in tqdm(content_ids):
acc_sum_c=acc_sum_c_dict[content_id]
acc_std_c=acc_std_c_dict[content_id]
count_c=count_c_dict[content_id]
cur.execute("INSERT INTO c_feats VALUES(:content_id,:acc_sum_c,:acc_std_c, :count_c)",(content_id,acc_sum_c,acc_std_c, count_c))
conn.commit()
cur = conn.cursor()
cur.execute('CREATE TABLE b_feats(bundle_id INTEGER,\
exp_sum_b INTEGER, \
exp_std_b REAL, \
time_sum_b INTEGER, \
time_std_b REAL, \
count_b INTEGER, \
PRIMARY KEY(bundle_id)) ')
 conn.commit()
cur = conn.cursor()
bundle_ids=list(count_b_dict.keys())
 for bundle_id in tqdm(bundle_ids):
exp_sum_b=exp_sum_b_dict[bundle_id]
exp_std_b=exp_std_b_dict[bundle_id]
time_sum_b=time_sum_b_dict[bundle_id]
time_std_b=time_std_b_dict[bundle_id]
count_b=count_b_dict[bundle_id]
cur.execute("INSERT INTO b_feats VALUES(:bundle_id,:exp_sum_b,:exp_std_b, :time_sum_b,:time_std_b, :count_b)",(bundle_id,exp_sum_b,exp_std_b, time_sum_b,time_std_b, count_b))
conn.commit()
cur = conn.cursor()
cur.execute('CREATE TABLE up_feats(user_id INTEGER, part INTEGER,\
acc_sum_up INTEGER, \
acc_hist_up REAL, \
acc_hist2_up REAL, \
count_up INTEGER, \
count_lecture_up INTEGER, \
last_time_up INTEGER, \
last_time2_up INTEGER, \
tfl_hist_up REAL, \
tfl_hist2_up REAL, \
exp_sum_up INTEGER, \
exp_hist_up REAL, \
exp_hist2_up REAL, \
time_sum_up INTEGER, \
time_hist_up REAL, \
time_hist2_up REAL, \
count_upp INTEGER, \
PRIMARY KEY(user_id, part)) ')
 conn.commit()
cur = conn.cursor()
user_ids=list(count_up_dict.keys())
 for user_id in tqdm(user_ids):
parts=list(count_up_dict[user_id].keys())
 for part in parts:
acc_sum_up=acc_sum_up_dict[user_id][part]
acc_hist_up=acc_hist_up_dict[user_id][part]
acc_hist2_up=acc_hist2_up_dict[user_id][part]
count_up=count_up_dict[user_id][part]
count_lecture_up=count_lecture_up_dict[user_id][part]
last_time_up=last_time_up_dict[user_id][part]
last_time2_up=last_time2_up_dict[user_id][part]
tfl_hist_up=tfl_hist_up_dict[user_id][part]
tfl_hist2_up=tfl_hist2_up_dict[user_id][part]
exp_sum_up=exp_sum_up_dict[user_id][part]
exp_hist_up=exp_hist_up_dict[user_id][part]
exp_hist2_up=exp_hist2_up_dict[user_id][part]
time_sum_up=time_sum_up_dict[user_id][part]
time_hist_up=time_hist_up_dict[user_id][part]
time_hist2_up=time_hist2_up_dict[user_id][part]
count_upp=count_upp_dict[user_id][part]
cur.execute("INSERT INTO up_feats VALUES(:user_id,:part,\
:acc_sum_up,:acc_hist_up, :acc_hist2_up,\
:count_up,:count_lecture_up,\
:last_time_up,:last_time2_up,\
:tfl_hist_up,:tfl_hist2_up,\
:exp_sum_up,:exp_hist_up, :exp_hist2_up,\
:time_sum_up,:time_hist_up, :time_hist2_up,\
:count_upp)",(user_id,part,acc_sum_up, acc_hist_up, acc_hist2_up,count_up,count_lecture_up,last_time_up,last_time2_up,tfl_hist_up,tfl_hist2_up,exp_sum_up,exp_hist_up, exp_hist2_up,time_sum_up,time_hist_up, time_hist2_up,count_upp))
conn.commit()
cur = conn.cursor()
cur.execute('CREATE TABLE uc_feats(user_id INTEGER, content_id INTEGER,\
acc_sum_uc INTEGER, \
acc_hist_uc REAL, \
acc_hist2_uc REAL, \
count_uc INTEGER, \
count_lecture_uc INTEGER, \
last_time_uc INTEGER, \
last_time2_uc INTEGER, \
tfl_hist_uc REAL, \
tfl_hist2_uc REAL, \
PRIMARY KEY(user_id, content_id)) ')
 conn.commit()
cur = conn.cursor()
user_ids=list(count_uc_dict.keys())
 for user_id in tqdm(user_ids):
content_ids=list(count_uc_dict[user_id].keys())
 for content_id in content_ids:
acc_sum_uc=acc_sum_uc_dict[user_id][content_id]
acc_hist_uc=acc_hist_uc_dict[user_id][content_id]
acc_hist2_uc=acc_hist2_uc_dict[user_id][content_id]
count_uc=count_uc_dict[user_id][content_id]
count_lecture_uc=count_lecture_uc_dict[user_id][content_id]
last_time_uc=last_time_uc_dict[user_id][content_id]
last_time2_uc=last_time2_uc_dict[user_id][content_id]
tfl_hist_uc=tfl_hist_uc_dict[user_id][content_id]
tfl_hist2_uc=tfl_hist2_uc_dict[user_id][content_id]
cur.execute("INSERT INTO uc_feats VALUES(:user_id,:content_id,:acc_sum_uc,:acc_hist_uc, :acc_hist2_uc,:count_uc,:count_lecture_uc,:last_time_uc,:last_time2_uc,:tfl_hist_uc,:tfl_hist2_uc)",(user_id,content_id,acc_sum_uc, acc_hist_uc, acc_hist2_uc,count_uc,count_lecture_uc,last_time_uc,last_time2_uc,tfl_hist_uc,tfl_hist2_uc))
conn.commit()
cur = conn.cursor()
cur.execute('CREATE TABLE ub_feats(user_id INTEGER, bundle_id INTEGER,\
exp_sum_ub INTEGER, \
exp_hist_ub REAL, \
exp_hist2_ub REAL, \
time_sum_ub INTEGER, \
time_hist_ub REAL, \
time_hist2_ub REAL, \
count_ub REAL, \
PRIMARY KEY(user_id, bundle_id)) ')
 conn.commit()
cur = conn.cursor()
user_ids=list(count_ub_dict.keys())
 for user_id in tqdm(user_ids):
bundle_ids=list(count_ub_dict[user_id].keys())
 for bundle_id in bundle_ids:
exp_sum_ub=exp_sum_ub_dict[user_id][bundle_id]
exp_hist_ub=exp_hist_ub_dict[user_id][bundle_id]
exp_hist2_ub=exp_hist2_ub_dict[user_id][bundle_id]
time_sum_ub=time_sum_ub_dict[user_id][bundle_id]
time_hist_ub=time_hist_ub_dict[user_id][bundle_id]
time_hist2_ub=time_hist2_ub_dict[user_id][bundle_id]
count_ub=count_ub_dict[user_id][bundle_id]
cur.execute("INSERT INTO ub_feats VALUES(:user_id,:bundle_id,:exp_sum_ub,:exp_hist_ub, :exp_hist2_ub,:time_sum_ub,:time_hist_ub,:time_hist2_ub,:count_ub)",(user_id,bundle_id,exp_sum_ub,exp_hist_ub, exp_hist2_ub,time_sum_ub,time_hist_ub,time_hist2_ub,count_ub))
conn.commit()
conn.close()<define_variables>
metrics = []
for _target in train_targets.columns:
metrics.append(log_loss(train_targets.loc[:, _target], res.loc[:, _target]))
print(f'OOF Metric: {np.mean(metrics)}' )
TARGET = 'answered_correctly'
FEATS = ['timestamp', 'prior_question_elapsed_time','prior_question_had_explanation', 'part',
'acc_avg_t','exp_avg_t', 'time_avg_t',
'acc_avg_tg','exp_avg_tg', 'time_avg_tg',
'acc_sum_tg', 'exp_sum_tg','time_sum_tg',
'acc_std_tg','exp_std_tg','time_std_tg',
'acc_avg_p', 'exp_avg_p','time_avg_p',
'acc_sum_p', 'exp_sum_p','time_sum_p',
'acc_std_p','exp_std_p','time_std_p',
'acc_avg_c', 'exp_avg_b', 'time_avg_b',
'acc_sum_c', 'exp_sum_b','time_sum_b',
'acc_std_c','exp_std_b','time_std_b',
'count_c',
'acc_avg_u', 'exp_avg_u','time_avg_u',
'acc_sum_u','exp_sum_u', 'time_sum_u',
'acc_hist_u', 'exp_hist_u', 'time_hist_u',
'acc_hist2_u','exp_hist2_u', 'time_hist2_u',
'tfl_u','tfl2_u','tfl3_u','tfl4_u','tfl5_u',
'tfl_hist_u','tfl_hist2_u',
'dpt_avg_u','dpt_sum_u', 'dpt_hist_u', 'dpt_hist2_u',
'dif_avg_u', 'dif_sum_u', 'dif_hist_u','dif_hist2_u',
'count_u','count_lecture_u',
'acc_avg_up','exp_avg_up', 'time_avg_up',
'acc_sum_up', 'exp_sum_up', 'time_sum_up',
'acc_hist_up', 'exp_hist_up', 'time_hist_up',
'acc_hist2_up', 'time_hist2_up','exp_hist2_up',
'tfl_up','tfl_hist_up','tfl_hist2_up',
'count_up',
'count_lecture_up',
'acc_avg_uc','exp_avg_ub', 'time_avg_ub',
'acc_sum_uc','exp_sum_ub', 'time_sum_ub',
'acc_hist_uc', 'exp_hist_ub','time_hist_ub',
'acc_hist2_uc', 'exp_hist2_ub','time_hist2_ub',
'tfl_uc','tfl_hist_uc','tfl_hist2_uc',
'count_uc','time_per_count_u','pp_ratio',
'exp_avg_u_corrected',
'count_u_nondiagnostic','pp_count',
'is_diagnostic_sum','listening_ratio',
'relative_dif',
'notacc_sum_u','notacc_sum_up','notacc_sum_uc',
'wp_ts','umu_ts','usigma_ts','qmu_ts','qsigma_ts']<data_type_conversions>
metrics = []
res.loc[train['cp_type']==1, train_targets.columns] = 0
for _target in train_targets.columns:
metrics.append(log_loss(train_targets.loc[:, _target], res.loc[:, _target]))
print(f'OOF Metric with postprocessing: {np.mean(metrics)}' )
X_train=np.ndarray(( len(train),len(FEATS)) ,dtype='float32')
 for idx,feature in enumerate(FEATS):
X_train[:,idx]=train[feature].values.astype(np.float32)
 Y_train=train[TARGET].values.astype('float32')
 X_valid=valid[FEATS].values.astype('float32')
 Y_valid=valid[TARGET].values.astype('float32' )<create_dataframe>
ss.loc[test['cp_type']==1, train_targets.columns] = 0
<train_model><EOS> 
ss.to_csv('submission.csv', index=False )