Kaggle competition: Instacart Market Basket Analysis
<SOS> metric: MeanFScore Kaggle data source: instacart-market-basket-analysis<define_variables>
gc.enable()
class TestDataGenerator(keras.utils.Sequence):
def __init__(self, X, batch_size = 16, img_size =(512, 512, 3), *args, **kwargs):
self.X = X
self.indices = np.arange(len(self.X))
self.batch_size = batch_size
self.img_size = img_size
def __len__(self):
return int(ceil(len(self.X)/ self.batch_size))
def __getitem__(self, index):
indices = self.indices[index*self.batch_size:(index+1)*self.batch_size]
X = self.__data_generation(indices)
 return X
def __data_generation(self, indices):
X = np.empty(( self.batch_size, *self.img_size))
for i, index in enumerate(indices):
image = self.X[index]
image = np.stack(( image,)*CHANNELS, axis=-1)
 image = image.reshape(-1, HEIGHT_NEW, WIDTH_NEW, CHANNELS)
 X[i,] = image
return X<define_variables>
orders = pd.read_csv('.. /input/orders.csv')
 order_products_train = pd.read_csv('.. /input/order_products__train.csv')
 order_products_prior = pd.read_csv('.. /input/order_products__prior.csv')
 products = pd.read_csv('.. /input/products.csv')
 aisles = pd.read_csv('.. /input/aisles.csv')
 departments = pd.read_csv('.. /input/departments.csv' )
tgt_cols = ['grapheme_root','vowel_diacritic','consonant_diacritic']
row_ids, targets = [], []
for i in range(0, 4):
test_files = []
df = pd.read_parquet(os.path.join(DIR, 'test_image_data_'+str(i)+'.parquet'))
image_ids = df['image_id'].values
df = df.drop(['image_id'], axis = 1)
 X = []
for image_id, index in zip(image_ids, range(df.shape[0])) :
test_files.append(image_id)
 X.append(resize_image(df.loc[df.index[index]].values, WIDTH_NEW, HEIGHT_NEW))
data_generator_test = TestDataGenerator(X, batch_size = BATCH_SIZE, img_size =(HEIGHT_NEW, WIDTH_NEW, CHANNELS))
preds1 = model1.predict_generator(data_generator_test, verbose = 1)
 preds2 = model2.predict_generator(data_generator_test, verbose = 1)
 preds3 = model3.predict_generator(data_generator_test, verbose = 1)
 preds4 = model4.predict_generator(data_generator_test, verbose = 1)
 preds5 = model5.predict_generator(data_generator_test, verbose = 1)
 for i, image_id in zip(range(len(test_files)) , test_files):
for subi, col in zip(range(len(preds1)) , tgt_cols):
sub_preds1 = preds1[subi]
sub_preds2 = preds2[subi]
sub_preds3 = preds3[subi]
sub_preds4 = preds4[subi]
sub_preds5 = preds5[subi]
row_ids.append(str(image_id)+'_'+col)
 sub_pred_value = np.argmax(( sub_preds1[i] + sub_preds2[i] + sub_preds3[i] + sub_preds4[i] + sub_preds5[i])/ 5)
 targets.append(sub_pred_value)
 del df
gc.collect()<save_to_csv>
aisles['aisle'] = aisles['aisle'].astype('category')
 departments['department'] = departments['department'].astype('category')
 orders['eval_set'] = orders['eval_set'].astype('category')
 products['product_name'] = products['product_name'].astype('category' )
submit_df = pd.DataFrame({'row_id':row_ids,'target':targets}, columns = ['row_id','target'])
 submit_df.to_csv('submission.csv', index = False)
 print(submit_df.head(40))<define_variables>
op = orders.merge(order_products_prior, on='order_id', how='inner')
 op.head()
PATH = '/kaggle/input/bengali-grapheme-second-try'<define_search_model>
user= orders.groupby('user_id')[['days_since_prior_order']].mean()
user.columns=['avg_days_since_prior_order']
user.head()
class MyRn50(nn.Module):
def __init__(self):
super(MyRn50, self ).__init__()
self.model_resnet = torchvision.models.resnet50()
num_ftrs = self.model_resnet.fc.in_features
self.model_resnet.fc = nn.Identity()
self.fc_graph = nn.Linear(num_ftrs, 168)
 self.fc_vowel = nn.Linear(num_ftrs, 11)
 self.fc_conso = nn.Linear(num_ftrs, 7)
 def forward(self, x):
x = self.model_resnet(x)
 out1 = self.fc_graph(x)
 out2 = self.fc_vowel(x)
 out3 = self.fc_conso(x)
 return out1, out2, out3
mymodel = MyRn50()
mymodel;<load_pretrained>
user1 = op.groupby('user_id')['order_number'].max().to_frame('u_total_orders')
 user1.head()
model=MyRn50()
weights = torch.load('/kaggle/input/bgraph-rn50-weights/model_wts1.pth',
map_location=torch.device('cpu'))
model.load_state_dict(weights['state_dict'])
 model.eval() ;<data_type_conversions>
prd = pd.merge(orders, order_products_prior, on='order_id', how='inner')
 prd.head(10 )
class GraphemeDataset(Dataset):
def __init__(self, fname):
self.df = pd.read_parquet(fname)
 self.data = 255 - self.df.iloc[:, 1:].values.reshape(-1, HEIGHT, WIDTH ).astype(np.uint8)
 def __len__(self):
return len(self.data)
 def __getitem__(self, idx):
name = self.df.iloc[idx,0]
img =(self.data[idx]*(255.0/self.data[idx].max())).astype(np.uint8)
 img = crop_resize(img)
 img =(img.astype(np.float32)/255.0 - imagenet_stats[0][0])/imagenet_stats[1][0]
return img, name<prepare_x_and_y>
order_size= prd.groupby(['user_id', 'order_id'])['product_id'].count()
order_size = order_size.to_frame()
order_size.columns=['size']
order_size.head()
row_id,target,bs = [],[],128
for fname in TEST:
ds = GraphemeDataset(fname)
 dl = DataLoader(ds, batch_size=bs, num_workers=nworkers, shuffle=False)
 with torch.no_grad() :
for x,y in dl:
x = x.unsqueeze(1)
 x = x.repeat(1,3,1,1)
 p1,p2,p3 = model(x)
 p1 = p1.argmax(-1 ).view(-1 ).cpu()
p2 = p2.argmax(-1 ).view(-1 ).cpu()
p3 = p3.argmax(-1 ).view(-1 ).cpu()
for idx,name in enumerate(y):
row_id += [f'{name}_grapheme_root',f'{name}_vowel_diacritic',
f'{name}_consonant_diacritic']
target += [p1[idx].item() ,p2[idx].item() ,p3[idx].item() ]
sub_df = pd.DataFrame({'row_id': row_id, 'target': target})
 sub_df.to_csv('submission.csv', index=False)
 sub_df<import_modules>
results= order_size.groupby('user_id' ).mean()
results.columns= ['order_size_avg']
results.head()
import torch
from torch import nn
import pandas as pd
import numpy as np
import cv2
from torch.utils.data import Dataset, DataLoader
from collections import OrderedDict
import torch.nn as nn
import torch.nn.functional as F
import torch
import gc<define_variables>
user = user.merge(u_reorder, on='user_id', how='inner')
 del u_reorder
gc.collect()
user.head()
BATCH_SIZE=512
TEST_FILE_PATH='/kaggle/input/bengaliai-cv19/test.csv'
DATA_DIR='/kaggle/input/bengaliai-cv19/'
MODEL_PATH='/kaggle/input/12345678/checkpoint-epoch50.pth'<data_type_conversions>
user = user.merge(user1, on='user_id', how='inner')
 del user1
gc.collect()
user.head()
class test_dataset(torch.utils.data.Dataset):
def __init__(self,test_dir):
self.dir=test_dir
self.data=pd.read_parquet(self.dir)
 def __len__(self):
return self.data.shape[0]
def __getitem__(self,idx):
data=(self.data.iloc[idx].to_numpy() [1:].reshape([1, 137, 236])/255 ).astype('float')
 data=torch.from_numpy(data ).cuda().float()
return {'data':data,'id':torch.tensor(idx ).cuda() }<import_modules>
user = user.merge(results, on='user_id', how='inner')
 del results
gc.collect()
user.head()
class efficientnet_b1(nn.Module):
def __init__(self):
super(efficientnet_b1, self ).__init__()
model = EfficientNet.from_name('efficientnet-b1')
 self.conv1 = torch.nn.Conv2d(1, 32,(3, 3))
self.layer0 = model._bn0
self.layer1 = model._blocks
self.layer2 = model._conv_head
self.layer3 = model._bn1
self.layer4 = model._avg_pooling
self.layer5 = model._dropout
self.fc1 = torch.nn.Linear(1280, 168)
 self.fc2 = torch.nn.Linear(1280, 11)
 self.fc3 = torch.nn.Linear(1280, 7)
 def forward(self, x):
x = self.conv1(x)
 x = self.layer0(x)
 for layer in self.layer1:
x=layer(x)
 x = self.layer2(x)
 x = self.layer3(x)
 x = self.layer4(x)
 x = self.layer5(x)
 x = torch.flatten(x, 1)
 x1 = self.fc1(x)
 x2 = self.fc2(x)
 x3 = self.fc3(x)
 return [x1, x2, x3]<train_on_grid>
prd = op.groupby('product_id')['order_id'].count().to_frame('p_total_purchases')
 prd.head()
torch.cuda.empty_cache()
with torch.no_grad() :
model1=efficientnet_b1()
model1=torch.nn.DataParallel(model1,device_ids=[0])
 model1.eval()
model1.load_state_dict(torch.load('/kaggle/input/all-models/checkpoint-epoch23.pth')) 
<load_from_csv>
p_reorder = op.groupby('product_id' ).filter(lambda x: x.shape[0] >40)
 p_reorder.head()
gc.collect()
test_data=['test_image_data_0.parquet','test_image_data_1.parquet','test_image_data_2.parquet','test_image_data_3.parquet']
test_id_answer={}
grapheme_root_list=[]
vowel_diacritic_list=[]
consonant_diacritic_list=[]
with torch.no_grad() :
for data_dir in test_data:
dataset=pd.read_parquet(DATA_DIR+data_dir ).iloc[:,1:].to_numpy().reshape([-1,1,137,236])
 dataset=np.array_split(dataset, 50210//128)
 for data in dataset:
input_data=torch.from_numpy(data/255 ).float().cuda()
print(input_data.size())
 output=model1(input_data)
 grapheme_root_list.extend(np.argmax(output[0].cpu().detach().numpy() ,axis=1))
vowel_diacritic_list.extend(np.argmax(output[1].cpu().detach().numpy() ,axis=1))
consonant_diacritic_list.extend(np.argmax(output[2].cpu().detach().numpy() ,axis=1))
del input_data,output
gc.collect()
torch.cuda.empty_cache()
del dataset
gc.collect()<define_variables>
avg_pos = op.groupby('product_id')[['add_to_cart_order']].mean()
avg_pos.columns=['mean_add_to_cart_order']
avg_pos.head()
z=len(consonant_diacritic_list)
 row_id=[]
target=[]
for i in range(z):
row_id.append('Test_{}_consonant_diacritic'.format(i))
row_id.append('Test_{}_grapheme_root'.format(i))
row_id.append('Test_{}_vowel_diacritic'.format(i))
target.append(consonant_diacritic_list[i])
 target.append(grapheme_root_list[i])
 target.append(vowel_diacritic_list[i] )<create_dataframe>
prd = prd.merge(p_reorder, on='product_id', how='left')
 del p_reorder
gc.collect()
prd.head()
final_file=pd.DataFrame({'row_id':row_id, 'target':target},columns = ['row_id','target'] )<save_to_csv>
prd['p_reorder_ratio'] = prd['p_reorder_ratio'].fillna(value=0)
 prd.head()
final_file.to_csv('submission.csv',index=False )<import_modules>
prd = prd.merge(avg_pos, on='product_id', how='left')
 del avg_pos
gc.collect()
prd.head()
from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping
from sklearn.model_selection import KFold, StratifiedKFold
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import log_loss
import tensorflow.keras.backend as K
import tensorflow.keras.layers as L
import tensorflow.keras.models as M
import tensorflow_addons as tfa
from tqdm.notebook import tqdm
import tensorflow as tf
import pandas as pd
import numpy as np
import random
import time<load_from_csv>
uxp = op.groupby(['user_id', 'product_id'])['order_id'].count().to_frame('uxp_total_bought')
 uxp.head()
test_features = pd.read_csv('/kaggle/input/lish-moa/test_features.csv')
 train_targets = pd.read_csv('/kaggle/input/lish-moa/train_targets_scored.csv')
 train_features = pd.read_csv("/kaggle/input/lish-moa/train_features.csv")
 submission = pd.read_csv('/kaggle/input/lish-moa/sample_submission.csv')
 del test_features['sig_id'], train_features["sig_id"], train_targets['sig_id']<load_from_csv>
times = op.groupby(['user_id', 'product_id'])[['order_id']].count()
times.columns = ['Times_Bought_N']
times.head()
df1 = pd.read_csv('.. /input/moa-blending/0.01832.csv')
 df2 = pd.read_csv('.. /input/mypredpublic/my_predictions_0000001.csv')
 df3 = pd.read_csv('.. /input/moa-blending/0.01833.csv' )<categorify>
total_orders = op.groupby('user_id')['order_number'].max().to_frame('total_orders')
 total_orders.head()
for feature in ['cp_type', 'cp_dose']:
trans = LabelEncoder()
trans.fit(list(train_features[feature].astype(str ).values)+ list(test_features[feature].astype(str ).values))
train_features[feature] = trans.transform(list(train_features[feature].astype(str ).values))
test_features[feature] = trans.transform(list(test_features[feature].astype(str ).values))
train = train_features.copy()<choose_model_class>
span = pd.merge(total_orders, first_order_no, on='user_id', how='right')
 span.head(20 )
def create_model(num_columns):
model = tf.keras.Sequential([
tf.keras.layers.Input(num_columns),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.1),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(1400, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.1),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(1400, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.1),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(1400, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.1),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(1400, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.1),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(1400, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.1),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(1400, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.1),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(1400, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.1),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(1000, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.1),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(700, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.1),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(400, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.1),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(206, activation="sigmoid"))
])
 model.compile(optimizer=tfa.optimizers.Lookahead(tf.optimizers.Adam() , sync_period=5),
loss='binary_crossentropy', metrics=["accuracy"] 
)
 return model<predict_on_test>
span['Order_Range_D'] = span.total_orders - span.first_order_number + 1
span.head(30 )
N_STARTS = 3
tf.random.set_seed(43)
 res = train_targets.copy()
submission.loc[:, train_targets.columns] = 0
res.loc[:, train_targets.columns] = 0
model = create_model(len(train.columns))
model.fit(train.values,train_targets.values,validation_data=(train.values, train_targets.values),epochs=1, batch_size=128)
 test_predict = model.predict(test_features.values)
 val_predict = model.predict(train.values)
 submission.loc[:, train_targets.columns] += test_predict
res.loc[:, train_targets.columns] += val_predict
print('')
 submission.loc[:, train_targets.columns] /= N_STARTS
res.loc[:, train_targets.columns] /= N_STARTS<compute_test_metric>
uxp_ratio = pd.merge(times, span, on=['user_id', 'product_id'], how='left')
 uxp_ratio.head()
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)}' )<define_variables>
uxp_ratio['uxp_order_ratio'] = uxp_ratio.Times_Bought_N / uxp_ratio.Order_Range_D
uxp_ratio.head()
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)
 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 = submission.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 )<load_from_csv>
uxp_ratio = uxp_ratio.drop(['Times_Bought_N', 'total_orders', 'first_order_number', 'Order_Range_D'], axis=1)
 uxp_ratio.head()
def order_sub(sub):
return sub.sort_values('sig_id' ).reset_index(drop=True)
 df1 = order_sub(df1)
 df2 = order_sub(df2)
 df3 = order_sub(df3)
 BLEND=df1.copy()
BLEND.iloc[:,1:] = df1.iloc[:,1:]*0.8 +df2.iloc[:,1:]*0.1 +df3.iloc[:,1:]*0.1
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 )<save_to_csv>
del [times, first_order_number, first_order_no, span]
sub = pd.read_csv('.. /input/moa-submission/submission.csv')
 sub.to_csv('submission.csv', index=False )<set_options>
uxp = uxp.merge(uxp_ratio, on=['user_id', 'product_id'], how='left')
 del uxp_ratio
uxp.head()
sys.path.append('.. /input/iterativestratification')
 warnings.filterwarnings('ignore')
 os.listdir('.. /input/lish-moa')
 pd.set_option('max_columns', 2000 )<define_variables>
data = uxp.merge(user, on='user_id', how='left')
 data.head()
n_comp_GENES = 600
n_comp_CELLS = 80
VarianceThreshold_for_FS = 0.95
Dropout_Model = 0.25
print('n_comp_GENES', n_comp_GENES, 'n_comp_CELLS', n_comp_CELLS, 'total', n_comp_GENES + n_comp_CELLS )<create_dataframe>
data = data.merge(prd, on='product_id', how='left')
 data.head()
commits_df = pd.DataFrame(columns = ['n_commit', 'n_comp_GENES', 'n_comp_CELLS', 'train_features','VarianceThreshold_for_FS', 'Dropout_Model', 'LB_score', 'CV_logloss'] )<feature_engineering>
del op, user, prd, uxp
gc.collect()
n=0
commits_df.loc[n, 'n_commit'] = 0
commits_df.loc[n, 'n_comp_GENES'] = 600
commits_df.loc[n, 'n_comp_CELLS'] = 50
commits_df.loc[n, 'VarianceThreshold_for_FS'] = 0.8
commits_df.loc[n, 'train_features'] = 1245
commits_df.loc[n, 'Dropout_Model'] = 0.2619422201258426
commits_df.loc[n, 'CV_logloss'] = 0.01458269555140327
commits_df.loc[n, 'LB_score'] = 0.01839<feature_engineering>
orders_future = orders[(( orders.eval_set=='train')|(orders.eval_set=='test')) ]
orders_future = orders_future[ ['user_id', 'eval_set', 'order_id'] ]
orders_future.head(10)
n=1
commits_df.loc[n, 'n_commit'] = 4
commits_df.loc[n, 'n_comp_GENES'] = 610
commits_df.loc[n, 'n_comp_CELLS'] = 55
commits_df.loc[n, 'VarianceThreshold_for_FS'] = 0.82
commits_df.loc[n, 'train_features'] = 1240
commits_df.loc[n, 'Dropout_Model'] = 0.25
commits_df.loc[n, 'CV_logloss'] = 0.014584545081734047
commits_df.loc[n, 'LB_score'] = 0.01839<feature_engineering>
data = data.merge(orders_future, on='user_id', how='left')
 data.head(10 )
n=2
commits_df.loc[n, 'n_commit'] = 5
commits_df.loc[n, 'n_comp_GENES'] = 670
commits_df.loc[n, 'n_comp_CELLS'] = 67
commits_df.loc[n, 'VarianceThreshold_for_FS'] = 0.67
commits_df.loc[n, 'train_features'] = 1298
commits_df.loc[n, 'Dropout_Model'] = 0.25
commits_df.loc[n, 'CV_logloss'] = 0.014588561242139069
commits_df.loc[n, 'LB_score'] = 0.01840<feature_engineering>
data_train = data[data.eval_set=='train']
data_train.head()
n=3
commits_df.loc[n, 'n_commit'] = 6
commits_df.loc[n, 'n_comp_GENES'] = 450
commits_df.loc[n, 'n_comp_CELLS'] = 45
commits_df.loc[n, 'VarianceThreshold_for_FS'] = 0.67
commits_df.loc[n, 'train_features'] = 1297
commits_df.loc[n, 'Dropout_Model'] = 0.25
commits_df.loc[n, 'CV_logloss'] = 0.014586229676302227
commits_df.loc[n, 'LB_score'] = 0.01840<feature_engineering>
data_train = data_train.merge(order_products_train[['product_id','order_id', 'reordered']], on=['product_id','order_id'], how='left')
 data_train.head(15 )
n=4
commits_df.loc[n, 'n_commit'] = 9
commits_df.loc[n, 'n_comp_GENES'] = 463
commits_df.loc[n, 'n_comp_CELLS'] = 60
commits_df.loc[n, 'VarianceThreshold_for_FS'] = 0.9
commits_df.loc[n, 'train_features'] = 1219
commits_df.loc[n, 'Dropout_Model'] = 0.25
commits_df.loc[n, 'CV_logloss'] = 0.014572358066092783
commits_df.loc[n, 'LB_score'] = 0.01839<feature_engineering>
data_train['reordered'] = data_train['reordered'].fillna(0)
 data_train.head(15 )
n=5
commits_df.loc[n, 'n_commit'] = 10
commits_df.loc[n, 'n_comp_GENES'] = 463
commits_df.loc[n, 'n_comp_CELLS'] = 80
commits_df.loc[n, 'VarianceThreshold_for_FS'] = 0.92
commits_df.loc[n, 'train_features'] = 1214
commits_df.loc[n, 'Dropout_Model'] = 0.25
commits_df.loc[n, 'CV_logloss'] = 0.014571552074579226
commits_df.loc[n, 'LB_score'] = 0.01841<feature_engineering>
data_train = data_train.set_index(['user_id', 'product_id'])
 data_train.head(15 )
n=6
commits_df.loc[n, 'n_commit'] = 12
commits_df.loc[n, 'n_comp_GENES'] = 450
commits_df.loc[n, 'n_comp_CELLS'] = 65
commits_df.loc[n, 'VarianceThreshold_for_FS'] = 0.9
commits_df.loc[n, 'train_features'] = 1219
commits_df.loc[n, 'Dropout_Model'] = 0.25
commits_df.loc[n, 'CV_logloss'] = 0.01458043214513875
commits_df.loc[n, 'LB_score'] = 0.01840<feature_engineering>
data_train = data_train.drop(['eval_set', 'order_id'], axis=1)
 data_train.head(15 )
commits_df['n_comp_total'] = commits_df['n_comp_GENES'] + commits_df['n_comp_CELLS']
commits_df['seed'] = 42<feature_engineering>
data_test = data[data.eval_set=='test']
data_test.head()
commits_df['LB_score'] = pd.to_numeric(commits_df['LB_score'])
 commits_df = commits_df.sort_values(by=['LB_score', 'CV_logloss'], ascending = True ).reset_index(drop=True)
 commits_df['min'] = 0
commits_df.loc[0, 'min'] = 1
commits_df<sort_values>
data_test = data_test.set_index(['user_id', 'product_id'])
 data_test.head()
commits_df.sort_values(by=['CV_logloss'], ascending = True )<load_from_csv>
data_test = data_test.drop(['eval_set','order_id'], axis=1)
 data_test.head()
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>
X_train, y_train = data_train.drop('reordered', axis=1), data_train.reordered
rfc = RandomForestClassifier(n_estimators=10, n_jobs=-1 ,random_state=42)
 model = rfc.fit(X_train, y_train )
GENES = [col for col in train_features.columns if col.startswith('g-')]
CELLS = [col for col in train_features.columns if col.startswith('c-')]<categorify>
prediction = model.predict(data_test)
 prediction =(rfc.predict_proba(data_test)[:,1] >= 0.3 ).astype(int )
for col in(GENES + CELLS):
transformer = QuantileTransformer(n_quantiles=100,random_state=0, output_distribution="normal")
 vec_len = len(train_features[col].values)
 vec_len_test = len(test_features[col].values)
 raw_vec = train_features[col].values.reshape(vec_len, 1)
 transformer.fit(raw_vec)
 train_features[col] = transformer.transform(raw_vec ).reshape(1, vec_len)[0]
test_features[col] = transformer.transform(test_features[col].values.reshape(vec_len_test, 1)).reshape(1, vec_len_test)[0]<set_options>
data_test['prediction'] = prediction
data_test.tail(50 )
def seed_everything(seed=42):
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=42 )<create_dataframe>
orders_test = orders.loc[orders.eval_set=='test',("user_id", "order_id")]
orders_test.head()
data = pd.concat([pd.DataFrame(train_features[GENES]), pd.DataFrame(test_features[GENES])])
 data2 =(PCA(n_components=n_comp_GENES, random_state=42 ).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_GENES)])
 test2 = pd.DataFrame(test2, columns=[f'pca_G-{i}' for i in range(n_comp_GENES)])
 train_features = pd.concat(( train_features, train2), axis=1)
 test_features = pd.concat(( test_features, test2), axis=1 )<concatenate>
final = final.merge(orders_test, on='user_id', how='left')
 final.head()
data = pd.concat([pd.DataFrame(train_features[CELLS]), pd.DataFrame(test_features[CELLS])])
 data2 =(PCA(n_components=n_comp_CELLS, random_state=42 ).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_CELLS)])
 test2 = pd.DataFrame(test2, columns=[f'pca_C-{i}' for i in range(n_comp_CELLS)])
 train_features = pd.concat(( train_features, train2), axis=1)
 test_features = pd.concat(( test_features, test2), axis=1 )<create_dataframe>
final = final.drop('user_id', axis=1)
var_thresh = VarianceThreshold(VarianceThreshold_for_FS)
 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.shape<merge>
final['product_id'] = final.product_id.astype(int)
 del orders
del orders_test
gc.collect()
final.head()
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]<drop_column>
d = dict()
for row in final.itertuples() :
if row.prediction== 1:
try:
d[row.order_id] += ' ' + str(row.product_id)
 except:
d[row.order_id] = str(row.product_id)
 for order in final.order_id:
if order not in d:
d[order] = 'None'
gc.collect()
d
train = train.drop('cp_type', axis=1)
 test = test.drop('cp_type', axis=1 )<drop_column>
sub = pd.DataFrame.from_dict(d, orient='index')
 sub.reset_index(inplace=True)
 sub.columns = ['order_id', 'products']
sub.head()
target_cols = target.drop('sig_id', axis=1 ).columns.values.tolist()<split>
sub.to_csv('sub.csv', index=False )