Kaggle competition: COVID19 Global Forecasting (Week 2)
model = build(( X_train.shape[1],),402 )<train_model>
train_df = pd.read_csv(sorted(paths)[10])
 test_df = pd.read_csv(sorted(paths)[9])
 submission = pd.read_csv(sorted(paths)[8])
 population = pd.read_csv(sorted(paths)[11])
 country_stats = pd.read_csv(sorted(paths)[7])
 age_by_countries = pd.read_csv(sorted(paths)[1])
 health_index = pd.read_csv(sorted(paths)[3] )
save_weight = ModelCheckpoint('model.learned.hdf5',save_best_only=True,save_weights_only=False,monitor = 'val_loss',mode='min')
 reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, verbose=1, epsilon=1e-4, mode='min')
 early = EarlyStopping(monitor='val_loss',patience=5,mode='min')
 model.fit(X_train,Y_train,
epochs=50,
batch_size=128,
validation_data =(X_val,Y_val),
callbacks=[save_weight,reduce_lr_loss,early])
 model.load_weights('model.learned.hdf5' )<load_from_csv>
not_in_countries = []
for country in countries:
if country not in population_countries:
not_in_countries.append(country)
 not_in_countries_stats = []
for country in countries:
if country not in stats_countries:
not_in_countries_stats.append(country)
 not_in_countries_age = []
for country in countries:
if country not in age_countries:
not_in_countries_age.append(country)
 not_in_countries_health = []
for country in countries:
if country not in health_countries:
not_in_countries_health.append(country )
ss = pd.read_csv('.. /input/lish-moa/sample_submission.csv')
 ss.loc[:,'sig_id'] = test_features['sig_id'].values
ss.iloc[:,1:]=0<split>
stats_countries_map = {'Antigua&Barbuda': 'Antigua and Barbuda',
'Bahamas,The': 'Bahamas',
'Bosnia&Herzegovina': 'Bosnia and Herzegovina',
'BurkinaFaso': 'Burkina Faso',
'CapeVerde': 'Cabo Verde',
'CentralAfricanRep.': 'Central African Republic',
'Congo,Dem.Rep.': 'Congo(Kinshasa)',
'Congo,Repub.ofthe': 'Congo(Brazzaville)',
'CostaRica': 'Costa Rica',
"Coted'Ivoire": "Cote d'Ivoire",
'CzechRepublic': 'Czechia',
'DominicanRepublic': 'Dominican Republic',
'ElSalvador': 'El Salvador',
'EquatorialGuinea': 'Equatorial Guinea',
'Swaziland': 'Eswatini',
'Gambia,The': 'Gambia',
'Korea,South': 'Korea, South',
'NewZealand': 'New Zealand',
'Macedonia': 'North Macedonia',
'PapuaNewGuinea': 'Papua New Guinea',
'SaintKitts&Nevis': 'Saint Kitts and Nevis',
'SaintLucia': 'Saint Lucia',
'SaintVincentandtheGrenadines': 'Saint Vincent and the Grenadines',
'SanMarino': 'San Marino',
'SaudiArabia': 'Saudi Arabia',
'SouthAfrica': 'South Africa',
'SriLanka': 'Sri Lanka',
'Taiwan': 'Taiwan*',
'EastTimor': 'Timor-Leste',
'Trinidad&Tobago': 'Trinidad and Tobago',
'UnitedStates': 'US',
'UnitedArabEmirates': 'United Arab Emirates',
'UnitedKingdom': 'United Kingdom'}
map_state_rev_stat = {k: v for k, v in stats_countries_map.items() }
mskf = MultilabelStratifiedKFold(n_splits = 7,shuffle=True)
 seeds = [42,58,132,456,789]
histories = []
scores = []
for seed in seeds:
seedAll(seed_value=seed)
 print(f"Training seed {seed}")
 print('='*50)
 for idx,(tr_,val_)in enumerate(mskf.split(train,train_targets)) :
print(f'
Fold {idx}')
 print('-'*50)
 K.clear_session()
X_train,X_val,Y_train,Y_val = train.iloc[tr_,:],train.iloc[val_,:],train_targets.iloc[tr_,:],train_targets.iloc[val_,:]
path = f'model.{seed}_{idx}.hdf5'
save_weight = ModelCheckpoint(path,save_best_only=True,save_weights_only=False,monitor = 'val_loss',mode='min')
 reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, verbose=1, epsilon=1e-4, mode='min')
 early = EarlyStopping(monitor='val_loss',patience=5,mode='min')
 model_fin = build(shape=(X_train.shape[1],))
model_fin = transfer_weight(model,model_fin)
 for layer in model_fin.layers:
layer.trainable=True
history = model_fin.fit(X_train.values,Y_train,
batch_size=128,
epochs=50,
validation_data=(X_val,Y_val),
callbacks=[early,save_weight,reduce_lr_loss] 
)
 histories.append(history)
 model_fin= tf.keras.models.load_model(path, custom_objects={'leaky_relu': tf.nn.leaky_relu})
 val_pred = model_fin.predict(X_val)
 score = metric(Y_val.values,val_pred)
 scores.append(score)
 print(f"Validation Score: {score}")
 pred = model_fin.predict(test.values)
 ss.loc[:,train_targets.columns]+= pred<feature_engineering>
country_map = {'United States': 'US',
'Czech Republic(Czechia)': 'Czechia',
'Congo': 'Congo(Brazzaville)',
'DR Congo': 'Congo(Kinshasa)',
'South Korea': 'Korea, South',
'Taiwan': 'Taiwan*',
"Côte d'Ivoire": "Cote d'Ivoire",
'Saint Kitts & Nevis': 'Saint Kitts and Nevis',
'St.Vincent & Grenadines': 'Saint Vincent and the Grenadines'}
map_state_rev = {k: v for k, v in country_map.items() }
ss.loc[:,train_targets.columns] /= 7*len(seeds)
 ss.loc[test_features[test_features.cp_type=='ctl_vehicle'].index,train_targets.columns] = 0<save_to_csv>
country_age_map = {'Cape Verde': 'Cabo Verde',
'Democratic Republic of the Congo': 'Congo(Kinshasa)',
'Republic of the Congo': 'Congo(Brazzaville)',
'Czech Republic': 'Czechia',
'Eswatini(Swaziland)': 'Eswatini',
'South Korea': 'Korea, South',
'Taiwan': 'Taiwan*',
'United States': 'US'}
map_state_rev_age = {k: v for k, v in country_age_map.items() }
ss.to_csv('./submission.csv',index=False )<load_from_csv>
health_countries_map = {'Bosnia And Herzegovina': 'Bosnia and Herzegovina',
'Czech Republic': 'Czechia',
'South Korea': 'Korea, South',
'Taiwan': 'Taiwan*',
'Trinidad And Tobago': 'Trinidad and Tobago'}
map_state_rev_health = {k: v for k, v in health_countries_map.items() }
df1 = pd.read_csv('.. /input/notebookf3b99a8d3d/submission.csv')
 df2 = pd.read_csv('.. /input/keras-nn-pca/submission.csv')
 df3 = pd.read_csv('.. /input/notebook-v3-0/submission.csv' )<sort_values>
population['New_country_name'] = population['Country(or dependency)'].apply(lambda x: country_map[x] if x in
map_state_rev else x)
 country_stats['New_country_name'] = country_stats['Country'].apply(lambda x: stats_countries_map[x] if x in
map_state_rev_stat else x)
 age_by_countries['New_country_name'] = age_by_countries['Country'].apply(lambda x: country_age_map[x] if x in
map_state_rev_age else x)
 health_index['New_country_name'] = health_index['Country'].apply(lambda x: health_countries_map[x] if x in
map_state_rev_health else x )
def order_sub(sub):
return sub.sort_values('sig_id' ).reset_index(drop=True )<sort_values>
health_care_idx = {}
health_care_exp_idx = {}
for item in set(health_index['New_country_name']):
health_care_idx[item] = health_index.loc[health_index['New_country_name'] == item, 'Health Care Index'].values[0]
health_care_exp_idx[item] = health_index.loc[health_index['New_country_name'] == item, 'Health Care Exp.Index'].values[0]
df1 = order_sub(df1)
 df2 = order_sub(df2)
 df3 = order_sub(df3 )<prepare_output>
pop = {}
dens = {}
l_area = {}
age = {}
urban_pop = {}
for item in set(population['New_country_name']):
pop[item] = population.loc[population['New_country_name'] == item, 'Population(2020)'].values[0]
dens[item] = population.loc[population['New_country_name'] == item, 'Density(P/Km²)'].values[0]
l_area[item] = population.loc[population['New_country_name'] == item, 'Land Area(Km²)'].values[0]
age[item] = population.loc[population['New_country_name'] == item, 'Med.Age'].values[0]
urban_pop[item] = population.loc[population['New_country_name'] == item, 'Urban Pop %'].values[0]
BLEND=df1.copy()
BLEND.iloc[:,1:] = df1.iloc[:,1:]*0.40 +df2.iloc[:,1:]*0.45 +df3.iloc[:,1:]*0.15<define_variables>
for key, val in urban_pop.items() :
if val != 'N.A.':
urban_pop[key] = int(val.replace('%', '')) / 100
else:
urban_pop[key] = -1
TARGET_COL = ['5-alpha_reductase_inhibitor', '11-beta-hsd1_inhibitor', 'acat_inhibitor', 'acetylcholine_receptor_agonist', 'acetylcholine_receptor_antagonist', 'acetylcholinesterase_inhibitor', 'adenosine_receptor_agonist', 'adenosine_receptor_antagonist', 'adenylyl_cyclase_activator', 'adrenergic_receptor_agonist', 'adrenergic_receptor_antagonist', 'akt_inhibitor', 'aldehyde_dehydrogenase_inhibitor', 'alk_inhibitor', 'ampk_activator', 'analgesic', 'androgen_receptor_agonist', 'androgen_receptor_antagonist', 'anesthetic_-_local', 'angiogenesis_inhibitor', 'angiotensin_receptor_antagonist', 'anti-inflammatory', 'antiarrhythmic', 'antibiotic', 'anticonvulsant', 'antifungal', 'antihistamine', 'antimalarial', 'antioxidant', 'antiprotozoal', 'antiviral', 'apoptosis_stimulant', 'aromatase_inhibitor', 'atm_kinase_inhibitor', 'atp-sensitive_potassium_channel_antagonist', 'atp_synthase_inhibitor', 'atpase_inhibitor', 'atr_kinase_inhibitor', 'aurora_kinase_inhibitor', 'autotaxin_inhibitor', 'bacterial_30s_ribosomal_subunit_inhibitor', 'bacterial_50s_ribosomal_subunit_inhibitor', 'bacterial_antifolate', 'bacterial_cell_wall_synthesis_inhibitor', 'bacterial_dna_gyrase_inhibitor', 'bacterial_dna_inhibitor', 'bacterial_membrane_integrity_inhibitor', 'bcl_inhibitor', 'bcr-abl_inhibitor', 'benzodiazepine_receptor_agonist', 'beta_amyloid_inhibitor', 'bromodomain_inhibitor', 'btk_inhibitor', 'calcineurin_inhibitor', 'calcium_channel_blocker', 'cannabinoid_receptor_agonist', 'cannabinoid_receptor_antagonist', 'carbonic_anhydrase_inhibitor', 'casein_kinase_inhibitor', 'caspase_activator', 'catechol_o_methyltransferase_inhibitor', 'cc_chemokine_receptor_antagonist', 'cck_receptor_antagonist', 'cdk_inhibitor', 'chelating_agent', 'chk_inhibitor', 'chloride_channel_blocker', 'cholesterol_inhibitor', 'cholinergic_receptor_antagonist', 'coagulation_factor_inhibitor', 'corticosteroid_agonist', 'cyclooxygenase_inhibitor', 'cytochrome_p450_inhibitor', 'dihydrofolate_reductase_inhibitor', 'dipeptidyl_peptidase_inhibitor', 'diuretic', 'dna_alkylating_agent', 'dna_inhibitor', 'dopamine_receptor_agonist', 'dopamine_receptor_antagonist', 'egfr_inhibitor', 'elastase_inhibitor', 'erbb2_inhibitor', 'estrogen_receptor_agonist', 'estrogen_receptor_antagonist', 'faah_inhibitor', 'farnesyltransferase_inhibitor', 'fatty_acid_receptor_agonist', 'fgfr_inhibitor', 'flt3_inhibitor', 'focal_adhesion_kinase_inhibitor', 'free_radical_scavenger', 'fungal_squalene_epoxidase_inhibitor', 'gaba_receptor_agonist', 'gaba_receptor_antagonist', 'gamma_secretase_inhibitor', 'glucocorticoid_receptor_agonist', 'glutamate_inhibitor', 'glutamate_receptor_agonist', 'glutamate_receptor_antagonist', 'gonadotropin_receptor_agonist', 'gsk_inhibitor', 'hcv_inhibitor', 'hdac_inhibitor', 'histamine_receptor_agonist', 'histamine_receptor_antagonist', 'histone_lysine_demethylase_inhibitor', 'histone_lysine_methyltransferase_inhibitor', 'hiv_inhibitor', 'hmgcr_inhibitor', 'hsp_inhibitor', 'igf-1_inhibitor', 'ikk_inhibitor', 'imidazoline_receptor_agonist', 'immunosuppressant', 'insulin_secretagogue', 'insulin_sensitizer', 'integrin_inhibitor', 'jak_inhibitor', 'kit_inhibitor', 'laxative', 'leukotriene_inhibitor', 'leukotriene_receptor_antagonist', 'lipase_inhibitor', 'lipoxygenase_inhibitor', 'lxr_agonist', 'mdm_inhibitor', 'mek_inhibitor', 'membrane_integrity_inhibitor', 'mineralocorticoid_receptor_antagonist', 'monoacylglycerol_lipase_inhibitor', 'monoamine_oxidase_inhibitor', 'monopolar_spindle_1_kinase_inhibitor', 'mtor_inhibitor', 'mucolytic_agent', 'neuropeptide_receptor_antagonist', 'nfkb_inhibitor', 'nicotinic_receptor_agonist', 'nitric_oxide_donor', 'nitric_oxide_production_inhibitor', 'nitric_oxide_synthase_inhibitor', 'norepinephrine_reuptake_inhibitor', 'nrf2_activator', 'opioid_receptor_agonist', 'opioid_receptor_antagonist', 'orexin_receptor_antagonist', 'p38_mapk_inhibitor', 'p-glycoprotein_inhibitor', 'parp_inhibitor', 'pdgfr_inhibitor', 'pdk_inhibitor', 'phosphodiesterase_inhibitor', 'phospholipase_inhibitor', 'pi3k_inhibitor', 'pkc_inhibitor', 'potassium_channel_activator', 'potassium_channel_antagonist', 'ppar_receptor_agonist', 'ppar_receptor_antagonist', 'progesterone_receptor_agonist', 'progesterone_receptor_antagonist', 'prostaglandin_inhibitor', 'prostanoid_receptor_antagonist', 'proteasome_inhibitor', 'protein_kinase_inhibitor', 'protein_phosphatase_inhibitor', 'protein_synthesis_inhibitor', 'protein_tyrosine_kinase_inhibitor', 'radiopaque_medium', 'raf_inhibitor', 'ras_gtpase_inhibitor', 'retinoid_receptor_agonist', 'retinoid_receptor_antagonist', 'rho_associated_kinase_inhibitor', 'ribonucleoside_reductase_inhibitor', 'rna_polymerase_inhibitor', 'serotonin_receptor_agonist', 'serotonin_receptor_antagonist', 'serotonin_reuptake_inhibitor', 'sigma_receptor_agonist', 'sigma_receptor_antagonist', 'smoothened_receptor_antagonist', 'sodium_channel_inhibitor', 'sphingosine_receptor_agonist', 'src_inhibitor', 'steroid', 'syk_inhibitor', 'tachykinin_antagonist', 'tgf-beta_receptor_inhibitor', 'thrombin_inhibitor', 'thymidylate_synthase_inhibitor', 'tlr_agonist', 'tlr_antagonist', 'tnf_inhibitor', 'topoisomerase_inhibitor', 'transient_receptor_potential_channel_antagonist', 'tropomyosin_receptor_kinase_inhibitor', 'trpv_agonist', 'trpv_antagonist', 'tubulin_inhibitor', 'tyrosine_kinase_inhibitor', 'ubiquitin_specific_protease_inhibitor', 'vegfr_inhibitor', 'vitamin_b', 'vitamin_d_receptor_agonist', 'wnt_inhibitor']
NUM_TARGET = len(TARGET_COL )<load_from_csv>
coastline = {}
inf_mort = {}
gdp = {}
literacy = {}
phones = {}
arable = {}
crops = {}
other = {}
climate = {}
birhrate = {}
deathrate = {}
agri = {}
industry = {}
service = {}
for item in set(country_stats['New_country_name']):
coastline[item] = country_stats.loc[country_stats['New_country_name'] == item, 'Coastline(coast/area ratio)'].values[0]
inf_mort[item] = country_stats.loc[country_stats['New_country_name'] == item, 'Infant mortality(per 1000 births)'].values[0]
gdp[item] = country_stats.loc[country_stats['New_country_name'] == item, 'GDP($ per capita)'].values[0]
literacy[item] = country_stats.loc[country_stats['New_country_name'] == item, 'Literacy(%)'].values[0]
phones[item] = country_stats.loc[country_stats['New_country_name'] == item, 'Phones(per 1000)'].values[0]
arable[item] = country_stats.loc[country_stats['New_country_name'] == item, 'Arable(%)'].values[0]
crops[item] = country_stats.loc[country_stats['New_country_name'] == item, 'Crops(%)'].values[0]
other[item] = country_stats.loc[country_stats['New_country_name'] == item, 'Other(%)'].values[0]
climate[item] = country_stats.loc[country_stats['New_country_name'] == item, 'Climate'].values[0]
birhrate[item] = country_stats.loc[country_stats['New_country_name'] == item, 'Birthrate'].values[0]
deathrate[item] = country_stats.loc[country_stats['New_country_name'] == item, 'Deathrate'].values[0]
agri[item] = country_stats.loc[country_stats['New_country_name'] == item, 'Agriculture'].values[0]
industry[item] = country_stats.loc[country_stats['New_country_name'] == item, 'Industry'].values[0]
service[item] = country_stats.loc[country_stats['New_country_name'] == item, 'Service'].values[0]
submission = pd.read_csv('/kaggle/input/lish-moa/sample_submission.csv')
 df = pd.read_csv("/kaggle/input/lish-moa/sample_submission.csv")
 public_id = list(df['sig_id'].values)
 df_test = pd.read_csv('/kaggle/input/lish-moa/test_features.csv')
 test_id = list(df_test['sig_id'].values)
 private_id = list(set(test_id)-set(public_id))
df_submit = pd.DataFrame(index = public_id+private_id, columns=TARGET_COL)
 df_submit.index.name = 'sig_id'
df_submit[:] = 0
df_predict = BLEND.copy()
df_submit.loc[df_predict.sig_id,:] = df_predict[TARGET_COL].values
df_submit.loc[df_test[df_test.cp_type=='ctl_vehicle'].sig_id]= 0
df_submit.to_csv('submission.csv',index=True )<install_modules>
train_df['Population'] = train_df['Country_Region'].map(pop)
 train_df['Density'] = train_df['Country_Region'].map(dens)
 train_df['Land_Area'] = train_df['Country_Region'].map(l_area)
 train_df['Med_Age'] = train_df['Country_Region'].map(age)
 train_df['Urban_Pop'] = train_df['Country_Region'].map(urban_pop)
 train_df['Coastline'] = train_df['Country_Region'].map(coastline)
 train_df['Infant_mortality'] = train_df['Country_Region'].map(inf_mort)
 train_df['GDP'] = train_df['Country_Region'].map(gdp)
 train_df['Literacy'] = train_df['Country_Region'].map(literacy)
 train_df['Phones'] = train_df['Country_Region'].map(phones)
 train_df['Arable'] = train_df['Country_Region'].map(arable)
 train_df['Crops'] = train_df['Country_Region'].map(crops)
 train_df['Other'] = train_df['Country_Region'].map(other)
 train_df['Climate'] = train_df['Country_Region'].map(climate)
 train_df['Birthrate'] = train_df['Country_Region'].map(birhrate)
 train_df['Deathrate'] = train_df['Country_Region'].map(deathrate)
 train_df['Agriculture'] = train_df['Country_Region'].map(agri)
 train_df['Industry'] = train_df['Country_Region'].map(industry)
 train_df['Service'] = train_df['Country_Region'].map(service)
 train_df['Health_Care_Index'] = train_df['Country_Region'].map(health_care_idx)
 train_df['Health_Care_Exp_Index'] = train_df['Country_Region'].map(health_care_exp_idx)
 test_df['Population'] = test_df['Country_Region'].map(pop)
 test_df['Density'] = test_df['Country_Region'].map(dens)
 test_df['Land_Area'] = test_df['Country_Region'].map(l_area)
 test_df['Med_Age'] = test_df['Country_Region'].map(age)
 test_df['Urban_Pop'] = test_df['Country_Region'].map(urban_pop)
 test_df['Coastline'] = test_df['Country_Region'].map(coastline)
 test_df['Infant_mortality'] = test_df['Country_Region'].map(inf_mort)
 test_df['GDP'] = test_df['Country_Region'].map(gdp)
 test_df['Literacy'] = test_df['Country_Region'].map(literacy)
 test_df['Phones'] = test_df['Country_Region'].map(phones)
 test_df['Arable'] = test_df['Country_Region'].map(arable)
 test_df['Crops'] = test_df['Country_Region'].map(crops)
 test_df['Other'] = test_df['Country_Region'].map(other)
 test_df['Climate'] = test_df['Country_Region'].map(climate)
 test_df['Birthrate'] = test_df['Country_Region'].map(birhrate)
 test_df['Deathrate'] = test_df['Country_Region'].map(deathrate)
 test_df['Agriculture'] = test_df['Country_Region'].map(agri)
 test_df['Industry'] = test_df['Country_Region'].map(industry)
 test_df['Service'] = test_df['Country_Region'].map(service)
 test_df['Health_Care_Index'] = test_df['Country_Region'].map(health_care_idx)
 test_df['Health_Care_Exp_Index'] = test_df['Country_Region'].map(health_care_exp_idx )
!pip install --no-index --find-links /kaggle/input/pytorchtabnet/pytorch_tabnet-2.0.0-py3-none-any.whl pytorch-tabnet
!pip install /kaggle/input/iterative-stratification/iterative-stratification-master/<feature_engineering>
train_df['Urban_pop_num'] = train_df[['Population', 'Urban_Pop']].apply(lambda x: x[0]*x[1], axis=1)
 test_df['Urban_pop_num'] = test_df[['Population', 'Urban_Pop']].apply(lambda x: x[0]*x[1], axis=1 )
os.environ["CUDA_LAUNCH_BLOCKING"] = "1" 
<set_options>
age_cols = ['Age 0 to 14 Years', 'Age 15 to 64 Years', 'Age above 65 Years']
for item in age_cols:
for i in range(len(age_by_countries)) :
if type(age_by_countries[item][i])== str:
age_by_countries[item][i] = age_by_countries[item][i].replace('%','')
 age_by_countries[item][i] = float(age_by_countries[item][i])/ 100
def seed_everything(seed_value):
random.seed(seed_value)
 np.random.seed(seed_value)
 torch.manual_seed(seed_value)
 os.environ['PYTHONHASHSEED'] = str(seed_value)
 if torch.cuda.is_available() :
torch.cuda.manual_seed(seed_value)
 torch.cuda.manual_seed_all(seed_value)
 torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
seed_everything(42 )<load_from_csv>
age_014 = {}
age_1564 = {}
age_65plus = {}
for item in set(age_by_countries['New_country_name']):
age_014[item] = age_by_countries.loc[age_by_countries['New_country_name'] == item, 'Age 0 to 14 Years'].values[0]
age_1564[item] = age_by_countries.loc[age_by_countries['New_country_name'] == item, 'Age 15 to 64 Years'].values[0]
age_65plus[item] = age_by_countries.loc[age_by_countries['New_country_name'] == item, 'Age above 65 Years'].values[0]
data_path = ".. /input/lish-moa/"
train = pd.read_csv(data_path+'train_features.csv')
 train.drop(columns=["sig_id"], inplace=True)
 train_targets = pd.read_csv(data_path+'train_targets_scored.csv')
 train_targets.drop(columns=["sig_id"], inplace=True)
 test = pd.read_csv(data_path+'test_features.csv')
 test.drop(columns=["sig_id"], inplace=True)
 submission = pd.read_csv(data_path+'sample_submission.csv')
 remove_vehicle = True
if remove_vehicle:
kept_index = train['cp_type']=='trt_cp'
train = train.loc[kept_index].reset_index(drop=True)
 train_targets = train_targets.loc[kept_index].reset_index(drop=True)
 train["cp_type"] =(train["cp_type"]=="trt_cp")+ 0
train["cp_dose"] =(train["cp_dose"]=="D1")+ 0
test["cp_type"] =(test["cp_type"]=="trt_cp")+ 0
test["cp_dose"] =(test["cp_dose"]=="D1")+ 0
X_test = test.values<import_modules>
train_df['age_0-14'] = train_df['Country_Region'].map(age_014)
 train_df['age_15-64'] = train_df['Country_Region'].map(age_1564)
 train_df['age_65plus'] = train_df['Country_Region'].map(age_65plus)
 test_df['age_0-14'] = test_df['Country_Region'].map(age_014)
 test_df['age_15-64'] = test_df['Country_Region'].map(age_1564)
 test_df['age_65plus'] = test_df['Country_Region'].map(age_65plus )
from iterstrat.ml_stratifiers import MultilabelStratifiedKFold, MultilabelStratifiedShuffleSplit
from sklearn.metrics import roc_auc_score<choose_model_class>
train_df = train_df.fillna(-1)
 test_df = test_df.fillna(-1 )
MAX_EPOCH=200
tabnet_params = dict(n_d=24, n_a=24, n_steps=1, gamma=1.3,
lambda_sparse=0, optimizer_fn=torch.optim.Adam,
optimizer_params=dict(lr=2e-2, weight_decay=1e-5),
mask_type='entmax',
scheduler_params=dict(mode="min",
patience=5,
min_lr=1e-5,
factor=0.9,),
scheduler_fn=torch.optim.lr_scheduler.ReduceLROnPlateau,
verbose=10,
)<import_modules>
def time_feat(df, start_day):
start_day = datetime.strptime(start_day, '%Y-%m-%d' ).date()
df['Date_time'] = df['Date'].apply(lambda x: datetime.strptime(x, '%Y-%m-%d' ).date())
 df['Time_delta'] = df['Date_time'].apply(lambda x:(x - start_day ).days)
 df['Month'] = df['Date'].apply(lambda x: datetime.strptime(x, '%Y-%m-%d' ).month)
 return df
class LogitsLogLoss(Metric):
def __init__(self):
self._name = "logits_ll"
self._maximize = False
def __call__(self, y_true, y_pred):
logits = 1 /(1 + np.exp(-y_pred))
ll =(1-y_true)*np.log(1-logits + 1e-15)+ y_true*np.log(logits + 1e-15)
 return np.mean(-ll)
 class PartialLogitsLogLoss(Metric):
def __init__(self):
self._name = "logits_ll(partial)"
self._maximize = False
def __call__(self, y_true, y_pred):
y_true = y_true
y_pred = y_pred
logits = 1 /(1 + np.exp(-y_pred))
logloss =(1-y_true)*np.log(1-logits + 1e-15)+ y_true*np.log(logits + 1e-15)
 return np.mean(-logloss )<define_variables>
train_df = time_feat(train_df, '2020-01-22')
 test_df = time_feat(test_df, '2020-01-22' )
use_cols = [col for col in train_targets.columns if train_targets[col].mean() > 0.01]<prepare_x_and_y>
def to_str(x):
if x == -1:
return str(x)
 return x
scores_auc_all= []
test_cv_preds = []
NB_SPLITS = 10
mskf = MultilabelStratifiedKFold(n_splits=NB_SPLITS, random_state=0, shuffle=True)
 oof_preds = []
oof_targets = []
scores = []
scores_auc = []
for fold_nb,(train_idx, val_idx)in enumerate(mskf.split(train, train_targets)) :
print("FOLDS : ", fold_nb)
 X_train, y_train = train.values[train_idx, :], train_targets[use_cols].values[train_idx, :]
X_val, y_val = train.values[val_idx, :], train_targets[use_cols].values[val_idx, :]
model = TabNetRegressor(**tabnet_params)
 model.fit(X_train=X_train,
y_train=y_train,
eval_set=[(X_val, y_val)],
eval_name = ["val"],
eval_metric = ['logits_ll', 'logits_ll(partial)'],
max_epochs=MAX_EPOCH,
patience=20, batch_size=1024, virtual_batch_size=128,
num_workers=1, drop_last=False,
loss_fn=torch.nn.functional.binary_cross_entropy_with_logits)
 preds_val = model.predict(X_val)
 preds = 1 /(1 + np.exp(-preds_val))
score = np.min(model.history["val_logits_ll(partial)"])
 oof_preds.append(preds_val)
 oof_targets.append(y_val)
 scores.append(score)
 preds_test = model.predict(X_test)
 test_cv_preds.append(1 /(1 + np.exp(-preds_test)))
 oof_preds_all = np.concatenate(oof_preds)
 oof_targets_all = np.concatenate(oof_targets)
 test_preds_all = np.stack(test_cv_preds )<compute_test_metric>
train_df[cols[1]] = train_df[cols[1]].apply(lambda x: to_str(x))
test_df[cols[1]] = test_df[cols[1]].apply(lambda x: to_str(x))
aucs = []
for task_id in range(oof_preds_all.shape[1]):
aucs.append(roc_auc_score(y_true=oof_targets_all[:, task_id],
y_score=oof_preds_all[:, task_id]))
print(f"Overall AUC : {np.mean(aucs)}")
 print(f"Average CV : {np.mean(scores)}" )<save_to_csv>
features=['Province_State', 'Country_Region', 'Population', 'Density', 'Land_Area', 'Med_Age',
'Urban_Pop', 'Coastline', 'Infant_mortality', 'GDP', 'Literacy',
'Phones', 'Arable', 'Crops', 'Other', 'Climate', 'Birthrate', 'Health_Care_Index',
'Health_Care_Exp_Index','Deathrate', 'Agriculture', 'Industry', 'Service', 'Urban_pop_num',
'Time_delta', 'Month', 'age_0-14', 'age_15-64', 'age_65plus']
train_X = train_df[features + ['ConfirmedCases', 'Fatalities']].copy()
train_yc = train_df['ConfirmedCases'].copy()
train_yf = train_df['Fatalities'].copy()
all_feat = [col for col in submission.columns if col not in ["sig_id"]]
other = [col for col in all_feat if col not in use_cols]
submission[use_cols] = test_preds_all.mean(axis=0)
 submission[other] = train_targets[other].mean().values
submission.loc[test['cp_type']==0, submission.columns[1:]] = 0
submission.to_csv('submission.csv', index=None )<set_options>
for i in range(len(train_X)) :
if train_X['Med_Age'][i] == 'N.A.':
train_X['Med_Age'][i] = -1
for feature in features:
for i in range(len(test_df)) :
if test_df[feature][i] == 'N.A.':
test_df[feature][i] = -1
warnings.filterwarnings('ignore' )<load_from_csv>
enc1 = OrdinalEncoder()
enc2 = OrdinalEncoder()
enc1.fit(train_X[cols[1]].to_numpy().reshape(-1, 1))
enc2.fit(train_X[cols[2]].to_numpy().reshape(-1, 1))
train_X[cols[1]] = enc1.transform(train_X[cols[1]].to_numpy().reshape(-1, 1))
train_X[cols[2]] = enc2.transform(train_X[cols[2]].to_numpy().reshape(-1, 1))
test_df[cols[1]] = enc1.transform(test_df[cols[1]].to_numpy().reshape(-1, 1))
test_df[cols[2]] = enc2.transform(test_df[cols[2]].to_numpy().reshape(-1, 1))
train_features = pd.read_csv('.. /input/lish-moa/train_features.csv')
 train_targets_scored = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 train_targets_nonscored = pd.read_csv('.. /input/lish-moa/train_targets_nonscored.csv')
 test_features = pd.read_csv('.. /input/lish-moa/test_features.csv')
 sample_submission = pd.read_csv('.. /input/lish-moa/sample_submission.csv' )<define_variables>
check_list = ['Coastline', 'Infant_mortality', 'GDP', 'Literacy','Phones', 'Arable', 'Crops', 'Other',
'Climate', 'Birthrate', 'Deathrate', 'Agriculture', 'Industry', 'Service']
for item in check_list:
for i in range(len(train_X)) :
if type(train_X[item][i])== str:
train_X[item][i] = train_X[item][i].replace(',','.')
 train_X[item][i] = float(train_X[item][i])
 for item in check_list:
for i in range(len(test_df)) :
if type(test_df[item][i])== str:
test_df[item][i] = test_df[item][i].replace(',','.')
 test_df[item][i] = float(test_df[item][i] )
GENES = [col for col in train_features.columns if col.startswith('g-')]
CELLS = [col for col in train_features.columns if col.startswith('c-')]<set_options>
train_X['Agri_pop_num'] = train_X[['Population', 'Agriculture']].apply(lambda x: x[0]*x[1], axis=1)
 train_X['Industry_pop_num'] = train_X[['Population', 'Industry']].apply(lambda x: x[0]*x[1], axis=1)
 train_X['Service_pop_num'] = train_X[['Population', 'Service']].apply(lambda x: x[0]*x[1], axis=1)
 train_X['age_0-14_num'] = train_X[['Population', 'age_0-14']].apply(lambda x: x[0]*x[1], axis=1)
 train_X['age_15-64_num'] = train_X[['Population', 'age_15-64']].apply(lambda x: x[0]*x[1], axis=1)
 train_X['age_65plus_num'] = train_X[['Population', 'age_65plus']].apply(lambda x: x[0]*x[1], axis=1)
 test_df['Agri_pop_num'] = test_df[['Population', 'Agriculture']].apply(lambda x: x[0]*x[1], axis=1)
 test_df['Industry_pop_num'] = test_df[['Population', 'Industry']].apply(lambda x: x[0]*x[1], axis=1)
 test_df['Service_pop_num'] = test_df[['Population', 'Service']].apply(lambda x: x[0]*x[1], axis=1)
 test_df['age_0-14_num'] = test_df[['Population', 'age_0-14']].apply(lambda x: x[0]*x[1], axis=1)
 test_df['age_15-64_num'] = test_df[['Population', 'age_15-64']].apply(lambda x: x[0]*x[1], axis=1)
 test_df['age_65plus_num'] = test_df[['Population', 'age_65plus']].apply(lambda x: x[0]*x[1], axis=1 )
def seed_everything(seed=321):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything(seed=321 )<concatenate>
features = features + ['Agri_pop_num', 'Industry_pop_num', 'Service_pop_num',
'age_0-14_num', 'age_15-64_num', 'age_65plus_num']
n_comp = 29
data = pd.concat([pd.DataFrame(train_features[GENES]), pd.DataFrame(test_features[GENES])])
 data2 =(PCA(n_components=n_comp, random_state=321 ).fit_transform(data[GENES]))
train2 = data2[:train_features.shape[0]]; test2 = data2[-test_features.shape[0]:]
train2 = pd.DataFrame(train2, columns=[f'pca_G-{i}' for i in range(n_comp)])
 test2 = pd.DataFrame(test2, columns=[f'pca_G-{i}' for i in range(n_comp)])
 train_features = pd.concat(( train_features, train2), axis=1)
 test_features = pd.concat(( test_features, test2), axis=1)
 n_comp = 5
data = pd.concat([pd.DataFrame(train_features[CELLS]), pd.DataFrame(test_features[CELLS])])
 data2 =(PCA(n_components=n_comp, random_state=321 ).fit_transform(data[CELLS]))
train2 = data2[:train_features.shape[0]]; test2 = data2[-test_features.shape[0]:]
train2 = pd.DataFrame(train2, columns=[f'pca_C-{i}' for i in range(n_comp)])
 test2 = pd.DataFrame(test2, columns=[f'pca_C-{i}' for i in range(n_comp)])
 train_features = pd.concat(( train_features, train2), axis=1)
 test_features = pd.concat(( test_features, test2), axis=1 )<create_dataframe>
features.remove('Agriculture')
 features.remove('Industry')
 features.remove('Service')
 features.remove('Urban_Pop')
 features.remove('age_0-14')
 features.remove('age_15-64')
 features.remove('age_65plus')
var_thresh = VarianceThreshold(threshold=0.4)
 data = train_features.append(test_features)
 data_transformed = var_thresh.fit_transform(data.iloc[:, 4:])
 train_features_transformed = data_transformed[ : train_features.shape[0]]
test_features_transformed = data_transformed[-test_features.shape[0] : ]
train_features = pd.DataFrame(train_features[['sig_id','cp_type','cp_time','cp_dose']].values.reshape(-1, 4),\
columns=['sig_id','cp_type','cp_time','cp_dose'])
 train_features = pd.concat([train_features, pd.DataFrame(train_features_transformed)], axis=1)
 test_features = pd.DataFrame(test_features[['sig_id','cp_type','cp_time','cp_dose']].values.reshape(-1, 4),\
columns=['sig_id','cp_type','cp_time','cp_dose'])
 test_features = pd.concat([test_features, pd.DataFrame(test_features_transformed)], axis=1)
 train_features<merge>
scaler = StandardScaler()
train_X = scaler.fit_transform(train_X[features])
 train_X = pd.DataFrame(data=train_X, columns=features)
 test = test_df[features].copy()
test = scaler.transform(test)
 test = pd.DataFrame(data=test, columns=features )
train = train_features.merge(train_targets_scored, on='sig_id')
 train = train[train['cp_type']!='ctl_vehicle'].reset_index(drop=True)
 test = test_features[test_features['cp_type']!='ctl_vehicle'].reset_index(drop=True)
 target = train[train_targets_scored.columns]
train = train.drop('cp_type', axis=1)
 test = test.drop('cp_type', axis=1)
<drop_column>
train = train_X[features]
skf = StratifiedKFold(n_splits=2020, random_state=42)
 score_c = []
preds_c = []
for i,(tdx, vdx)in enumerate(skf.split(train, train_yc)) :
X_train, X_val, y_train, y_val = train.iloc[tdx], train.iloc[vdx], train_yc[tdx], train_yc[vdx]
model_c = KNeighborsRegressor(n_neighbors=1, weights='distance', algorithm='auto', n_jobs=-1)
 model_c.fit(X_train, y_train)
 val_preds_c = model_c.predict(X_val.to_numpy())
 val_score_c = np.sqrt(mean_squared_log_error(y_val, abs(val_preds_c)))
 score_c.append(val_score_c)
 pred_c = model_c.predict(test[features])
 preds_c.append(abs(pred_c))
print(np.mean(score_c))
target_cols = target.drop('sig_id', axis=1 ).columns.values.tolist()<split>
skf = StratifiedKFold(n_splits=2020, random_state=42)
 score_f = []
preds_f = []
for i,(tdx, vdx)in enumerate(skf.split(train, train_yf)) :
X_train, X_val, y_train, y_val = train.iloc[tdx], train.iloc[vdx], train_yf[tdx], train_yf[vdx]
model_f = KNeighborsRegressor(n_neighbors=1, weights='distance', algorithm='auto', n_jobs=-1)
 model_f.fit(X_train, y_train)
 val_preds_f = model_f.predict(X_val)
 val_score_f = np.sqrt(mean_squared_log_error(y_val, abs(val_preds_f)))
 score_f.append(val_score_f)
 pred_f = model_f.predict(test[features])
 preds_f.append(abs(pred_f))
print(np.mean(score_f))
folds = train.copy()
mskf = MultilabelStratifiedKFold(n_splits=10)
 for f,(t_idx, v_idx)in enumerate(mskf.split(X=train, y=target)) :
folds.loc[v_idx, 'kfold'] = int(f)
 folds['kfold'] = folds['kfold'].astype(int )<categorify>
c_mean = np.mean(score_c)
 f_mean = np.mean(score_f)
(c_mean + f_mean)/ 2
class MoADataset:
def __init__(self, features, targets):
self.features = features
self.targets = targets
def __len__(self):
return(self.features.shape[0])
 def __getitem__(self, idx):
dct = {
'x' : torch.tensor(self.features[idx, :], dtype=torch.float),
'y' : torch.tensor(self.targets[idx, :], dtype=torch.float)
 }
return dct
class TestDataset:
def __init__(self, features):
self.features = features
def __len__(self):
return(self.features.shape[0])
 def __getitem__(self, idx):
dct = {
'x' : torch.tensor(self.features[idx, :], dtype=torch.float)
 }
return dct<train_model>
cc_preds = np.vstack(preds_c)
 f_preds = np.vstack(preds_f)
 y_pred_cc = np.around(np.mean([cc_preds],axis=1))
y_pred_f = np.around(np.mean([f_preds],axis=1))
def train_fn(model, optimizer, scheduler, loss_fn, dataloader, device):
model.train()
final_loss = 0
for data in dataloader:
optimizer.zero_grad()
inputs, targets = data['x'].to(device), data['y'].to(device)
 outputs = model(inputs)
 loss = loss_fn(outputs, targets)
 loss.backward()
optimizer.step()
scheduler.step()
final_loss += loss.item()
final_loss /= len(dataloader)
 return final_loss
def valid_fn(model, loss_fn, dataloader, device):
model.eval()
final_loss = 0
valid_preds = []
for data in dataloader:
inputs, targets = data['x'].to(device), data['y'].to(device)
 outputs = model(inputs)
 loss = loss_fn(outputs, targets)
 final_loss += loss.item()
valid_preds.append(outputs.sigmoid().detach().cpu().numpy())
 final_loss /= len(dataloader)
 valid_preds = np.concatenate(valid_preds)
 return final_loss, valid_preds
def inference_fn(model, dataloader, device):
model.eval()
preds = []
for data in dataloader:
inputs = data['x'].to(device)
 with torch.no_grad() :
outputs = model(inputs)
 preds.append(outputs.sigmoid().detach().cpu().numpy())
 preds = np.concatenate(preds)
 return preds 
<normalization>
submission['ConfirmedCases'] = y_pred_cc[0].astype(int)
 submission['Fatalities'] = y_pred_f[0].astype(int)
 submission.to_csv('submission.csv', index=False )