Kaggle competition: Mechanisms of Action (MoA) Prediction
<SOS> metric: MeanColumnwiseLogLoss Kaggle data source: mechanisms-of-action-(moa)-prediction<load_from_csv>
import numpy as np
import pandas as pd
del X_train
X_test = pickle.load(open(MELS_TEST, 'rb'))
CUR_X_FILES, CUR_X = list(test_df.fname.values), X_test
test = ImageList.from_csv(WORK, Path('.. ')/CSV_SUBMISSION, folder='test')
 learn = load_learner(WORK, test=test)
 preds, _ = learn.TTA(ds_type=DatasetType.Test )<save_to_csv>
TN_sub = pd.read_csv('.. /input/moa-tabnet-inference/submission.csv')
 NN2_sub = pd.read_csv('.. /input/fork2-tabnet-nn1ee0a1/NN2_submission.csv')
 NN3_sub = pd.read_csv('.. /input/pytorch-transfer-learning-with-k-folds-by-drug-ids/submission.csv' )
test_df[learn.data.classes] = preds
test_df.to_csv('submission.csv', index=False)
 test_df.head()<import_modules>
wei_list = [0.333333333333333,0.333333333333333,0.333333333333333]
model_weight = [wei_list[0]/sum(wei_list),wei_list[0]/sum(wei_list),wei_list[0]/sum(wei_list)]
import gc
import os
import pickle
import random
import time
from collections import Counter, defaultdict
from functools import partial
from pathlib import Path
from psutil import cpu_count
import librosa
import numpy as np
import pandas as pd
from PIL import Image
from sklearn.model_selection import train_test_split
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from torch.optim import Adam
from torch.optim.lr_scheduler import CosineAnnealingLR, ReduceLROnPlateau, MultiStepLR
from fastprogress import master_bar, progress_bar
from torch.utils.data import Dataset, DataLoader
from torchvision.transforms import transforms
import torchvision.models as models<set_options>
submission = pd.read_csv('/kaggle/input/lish-moa/sample_submission.csv')
 public_ids = list(submission['sig_id'].values)
 submission.shape
torch.cuda.is_available() 
<set_options>
test = pd.read_csv('/kaggle/input/lish-moa/test_features.csv')
 test_ids = list(test['sig_id'].values)
 private_ids = list(set(test_ids)-set(public_ids))
len(private_ids )
def seed_everything(seed):
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 = 1314
seed_everything(SEED )<feature_engineering>
predictions = [TN_sub, NN2_sub, NN3_sub]
N_JOBS = cpu_count()
used = int(4)
 print(N_JOBS, used)
 os.environ['MKL_NUM_THREADS'] = str(used)
 os.environ['OMP_NUM_THREADS'] = str(used)
 DataLoader = partial(DataLoader, num_workers=used )<define_variables>
target_columns = list(submission.columns)
 target_columns.remove('sig_id' )
def _one_sample_positive_class_precisions(scores, truth):

 num_classes = scores.shape[0]
pos_class_indices = np.flatnonzero(truth > 0)
 if not len(pos_class_indices):
return pos_class_indices, np.zeros(0)
 retrieved_classes = np.argsort(scores)[::-1]
class_rankings = np.zeros(num_classes, dtype=np.int)
 class_rankings[retrieved_classes] = range(num_classes)
 retrieved_class_true = np.zeros(num_classes, dtype=np.bool)
 retrieved_class_true[class_rankings[pos_class_indices]] = True
retrieved_cumulative_hits = np.cumsum(retrieved_class_true)
 precision_at_hits =(
 retrieved_cumulative_hits[class_rankings[pos_class_indices]] / 
(1 + class_rankings[pos_class_indices].astype(np.float)))
 return pos_class_indices, precision_at_hits
def calculate_per_class_lwlrap(truth, scores):

 assert truth.shape == scores.shape
num_samples, num_classes = scores.shape
precisions_for_samples_by_classes = np.zeros(( num_samples, num_classes))
for sample_num in range(num_samples):
pos_class_indices, precision_at_hits =(
 _one_sample_positive_class_precisions(scores[sample_num, :],
truth[sample_num, :]))
precisions_for_samples_by_classes[sample_num, pos_class_indices] =(
 precision_at_hits)
 labels_per_class = np.sum(truth > 0, axis=0)
 weight_per_class = labels_per_class / float(np.sum(labels_per_class))
per_class_lwlrap =(np.sum(precisions_for_samples_by_classes, axis=0)/
np.maximum(1, labels_per_class))
return per_class_lwlrap, weight_per_class<define_variables>
y_pred = pd.DataFrame()
y_pred['sig_id'] = predictions[0]['sig_id']
for column in target_columns:
column_data = []
for i in range(len(predictions)) :
column_data.append(predictions[i][column])
 y_pred[column] = np.mean(column_data, axis=0)
 y_pred.shape
dataset_dir = Path('.. /input/freesound-audio-tagging-2019')
 print(os.listdir(dataset_dir))
preprocessed_dir = Path('.. /input/fat2019_prep_mels1')
 print(os.listdir(preprocessed_dir))<define_variables>
submission = pd.DataFrame(index = public_ids + private_ids, columns=target_columns)
 submission.index.name = 'sig_id'
submission[:] = 0
for column in target_columns:
submission[column] = np.array(y_pred[column])
 submission = np.clip(submission,0.0001,0.9999)
 submission.loc[test[test.cp_type == 'ctl_vehicle'].sig_id] = 0
submission.to_csv('submission.csv', index=True)
<load_from_csv><EOS> 
submission.iloc[:,0] = np.array(y_pred["5-alpha_reductase_inhibitor"] )