Kaggle competition: Mechanisms of Action (MoA) Prediction
DATA_FOLDER = '/kaggle/input/'
sales = pd.read_csv(os.path.join(DATA_FOLDER, 'sales_train.csv'))
test = pd.read_csv(os.path.join(DATA_FOLDER, 'test.csv'))
submission = pd.read_csv(os.path.join(DATA_FOLDER, 'sample_submission.csv'))
sales['dateTime'] = pd.to_datetime(sales['date'], format='%d.%m.%Y')
 sales = sales.drop(columns='date' )<load_pretrained>
import torch
import torch.nn as nn
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
def SaveDF(dataFrame, name):
print("Saving", name)
 dataFrame.to_hdf(name+".h5", key=name, mode='w')
 def SaveExist(name):
return os.path.isfile(name+".h5")
 def LoadDF(name):
print("Loading", name)
 return pd.read_hdf(name+".h5", key=name)
 def RemoveSavedDF(name):
if(SaveExist(name)) :
os.remove("/kaggle/working/"+name+".h5" )<prepare_output>
data = pd.read_csv('/kaggle/input/lish-moa/train_features.csv')
 target = pd.read_csv('/kaggle/input/lish-moa/train_targets_scored.csv')
 del data['sig_id']
categorical_cols = ['cp_type', 'cp_dose']
categorical_features = data[categorical_cols]
data.drop(categorical_cols, axis=1, inplace=True)
 categorical_features = OneHotEncoder(sparse=False ).fit_transform(categorical_features)
 data = StandardScaler().fit_transform(data)
 X = np.concatenate(( categorical_features, data), axis=1)
 y = target.to_numpy() [:,1:].astype(float )
<count_unique_values>
def k_folds_gen(*arrs, test_size=0.1):
indeces = np.random.permutation(arrs[0].shape[0])
 batch_size = int(arrs[0].shape[0] * 0.1)
 for start in range(0, len(indeces), batch_size):
indx_val = indeces[start: start+batch_size]
indx_train = np.concatenate(( indeces[:start], indeces[start+batch_size:]), axis=0)
 to_return = []
for arr in arrs:
to_return.append(arr[indx_train])
 to_return.append(arr[indx_val])
 yield tuple(to_return )
print('`---sales---')
 print('num shops', sales['shop_id'].nunique())
 print('num items', sales['item_id'].nunique())
 print('`---test---')
 print('num shops', test['shop_id'].nunique())
 print('num items', test['item_id'].nunique() )<split>
def batch_iterator(*arrs, batch_size):
indeces = np.random.permutation(arrs[0].shape[0])
 for start in range(0, len(indeces), batch_size):
indx = indeces[start: start+batch_size]
to_return = []
for arr in arrs:
to_return.append(arr[indx])
 yield tuple(to_return )
train = sales.query('item_cnt_day > 0' )<compute_test_metric>
def create_N_models(N, device):
models = [None] * N
for i in range(len(models)) :
models[i] = nn.Sequential(
 nn.Linear(877, 256),
nn.BatchNorm1d(256),
nn.Dropout(p=0.8),
nn.LeakyReLU() ,
nn.Linear(256, 206),
nn.Sigmoid() , 
)
 models[i].to(device)
 return models
def removeOutlierUsingZScore(dataToClean, zScoreThreshold, columNameToClean):
z = np.abs(stats.zscore(dataToClean[columNameToClean]))
dataCleaned = dataToClean[(z < zScoreThreshold)]
return dataCleaned<drop_column>
device = "cuda" if torch.cuda.is_available() else "cpu"
models = create_N_models(10, device)
 for i in range(len(models)) :
models[i].load_state_dict(torch.load(f"/kaggle/input/k-folds-weights/model_{i}.pth", map_location=device))
batch_size=128
zScorePrice = 5
zScoreCount = 50
train = removeOutlierUsingZScore(train, zScorePrice, 'item_price')
 train = removeOutlierUsingZScore(train, zScoreCount, 'item_cnt_day' )<feature_engineering>
test_data = pd.read_csv('/kaggle/input/lish-moa/test_features.csv')
 sig_id = test_data['sig_id']
del test_data['sig_id']
categorical_features = test_data[categorical_cols]
test_data.drop(categorical_cols, axis=1, inplace=True)
 categorical_features = OneHotEncoder(sparse=False ).fit_transform(categorical_features)
 test_data = StandardScaler().fit_transform(test_data)
 X_test = np.concatenate(( categorical_features, test_data), axis=1 )
train.loc[train.shop_id == 0, 'shop_id'] = 57
train.loc[train.shop_id == 1, 'shop_id'] = 58
train.loc[train.shop_id == 10, 'shop_id'] = 11
train.loc[train.shop_id == 40, 'shop_id'] = 39
test.loc[test.shop_id == 0, 'shop_id'] = 57
test.loc[test.shop_id == 1, 'shop_id'] = 58
test.loc[test.shop_id == 10, 'shop_id'] = 11
test.loc[test.shop_id == 40, 'shop_id'] = 39 
<merge>
for i in range(len(models)) :
models[i].eval()
with torch.no_grad() :
predictions =(sum(map(lambda x: x(torch.tensor(X_test, dtype=torch.float32 ).to(device)) , models)) /
len(models)).to('cpu' ).detach().numpy()
predictions.shape
train = pd.merge(train, test[['shop_id','item_id']], on=['shop_id','item_id'], how='right' )<data_type_conversions>
submission = pd.DataFrame(predictions)
 submission.columns = target.columns[1:]
submission['sig_id'] = sig_id
submission.head()
test['date_block_num'] = 34
test['date_block_num'] = test['date_block_num'].astype(np.int8)
 test['shop_id'] = test['shop_id'].astype(np.int8)
 test['item_id'] = test['item_id'].astype(np.int16)
 df = pd.concat([df, test], ignore_index=True, sort=False, keys=index_cols)
 df.fillna(0, inplace=True)
<load_from_csv>
submission.to_csv('submission.csv', index=False )
<categorify><EOS> 
submission.to_csv('submission.csv', index=False )