Kaggle competition: Mechanisms of Action (MoA) Prediction
grouped6 = train_match.groupby(['date_block_num','type_code'] ).agg({'item_cnt_month':['mean']})
 grouped6.columns = ['type_avg_item_cnt_month']
grouped6.reset_index(inplace = True)
 train_match = pd.merge(train_match, grouped6, on=['date_block_num','type_code'], how ='left')
 train_match['type_avg_item_cnt_month'] = train_match['type_avg_item_cnt_month'].astype('float16')
 train_match = create_lag_feature(train_match, [1], 'type_avg_item_cnt_month')
 train_match.drop(['type_avg_item_cnt_month'], axis =1, inplace =True )<merge>
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, ModelCheckpoint, EarlyStopping
import tensorflow_addons as tfa
from sklearn.model_selection import KFold
from sklearn.metrics import log_loss
from tqdm.notebook import tqdm
grouped7 = train_match.groupby(['date_block_num','subtype_code'] ).agg({'item_cnt_month':['mean']})
 grouped7.columns = ['subtype_avg_item_cnt_month']
grouped7.reset_index(inplace = True)
 train_match = pd.merge(train_match, grouped7, on=['date_block_num','subtype_code'], how ='left')
 train_match['subtype_avg_item_cnt_month'] = train_match['subtype_avg_item_cnt_month'].astype('float16')
 train_match = create_lag_feature(train_match, [1], 'subtype_avg_item_cnt_month')
 train_match.drop(['subtype_avg_item_cnt_month'], axis =1, inplace =True )<merge>
train = pd.read_csv('.. /input/prep-lishmoa/data/train_features.csv')
 train_targets = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 test = pd.read_csv('.. /input/prep-lishmoa/data/test_features.csv')
 ss = pd.read_csv('.. /input/lish-moa/sample_submission.csv' )
grouped8 = train_match.groupby(['date_block_num','shop_id','item_category_id'] ).agg({'item_cnt_month':['mean']})
 grouped8.columns = ['shop_ic_avg_item_cnt_month']
grouped8.reset_index(inplace = True)
 train_match = pd.merge(train_match, grouped8, on=['date_block_num','shop_id','item_category_id'], how ='left')
 train_match['shop_ic_avg_item_cnt_month'] = train_match['shop_ic_avg_item_cnt_month'].astype('float16')
 train_match = create_lag_feature(train_match, [1], 'shop_ic_avg_item_cnt_month')
 train_match.drop(['shop_ic_avg_item_cnt_month'], axis =1, inplace =True )<merge>
del train_targets['sig_id']
del train['sig_id']
del test['sig_id']
grouped9 = train_match.groupby(['date_block_num','shop_id','type_code'] ).agg({'item_cnt_month':['mean']})
 grouped9.columns = ['shop_type_avg_item_cnt_month']
grouped9.reset_index(inplace = True)
 train_match = pd.merge(train_match, grouped9, on=['date_block_num','shop_id','type_code'], how ='left')
 train_match['shop_type_avg_item_cnt_month'] = train_match['shop_type_avg_item_cnt_month'].astype('float16')
 train_match = create_lag_feature(train_match, [1], 'shop_type_avg_item_cnt_month')
 train_match.drop(['shop_type_avg_item_cnt_month'], axis =1, inplace =True )<merge>
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(2048, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.5),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(2048, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.5),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(206, activation="sigmoid"))
])
 model.compile(optimizer=tfa.optimizers.Lookahead(tf.optimizers.Adam() , sync_period=10),
loss='binary_crossentropy' 
)
 return model
grouped10 = train.groupby(['item_id'] ).agg({'item_price':['mean']})
 grouped10.columns = ['item_avg_item_price']
grouped10.reset_index(inplace = True)
 train_match = pd.merge(train_match, grouped10, on=['item_id'], how='left')
 train_match['item_avg_item_price'] = train_match['item_avg_item_price'].astype('float16')
 grouped11 = train.groupby(['date_block_num','item_id'] ).agg({'item_price':['mean']})
 grouped11.columns = ['item_avg_item_price_month']
grouped11.reset_index(inplace = True)
 train_match = pd.merge(train_match, grouped11, on=['date_block_num','item_id'], how='left')
 train_match['item_avg_item_price_month'] = train_match['item_avg_item_price_month'].astype('float16')
 all_lags = [1,2,3,4,5,6]
train_match = create_lag_feature(train_match,all_lags,'item_avg_item_price_month' )<data_type_conversions>
def build_model(num_columns):
model = tf.keras.Sequential([
tf.keras.layers.Input(num_columns),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.2),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(1400, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.4),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(840, activation="sigmoid")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.3),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(588, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.3),
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
for l in all_lags:
train_match['delta_price_lag_'+str(l)]=(train_match['item_avg_item_price_month_lag_'+str(l)] - train_match['item_avg_item_price'])/train_match['item_avg_item_price']
def valid_trend(row):
for l in all_lags:
if row['delta_price_lag_'+str(l)]:
return row['delta_price_lag_'+str(l)]
return 0
train_match['delta_price_lag'] = train_match.apply(valid_trend, axis=1)
 train_match['delta_price_lag'] = train_match['delta_price_lag'].astype('float16')
 train_match['delta_price_lag'].fillna(0, inplace =True)
 to_drop = ['item_avg_item_price','item_avg_item_price_month']
for l in all_lags:
to_drop += ['item_avg_item_price_month_lag_'+str(l)]
to_drop += ['delta_price_lag_'+str(l)]
train_match.drop(to_drop, axis=1, inplace=True )<feature_engineering>
seed = tf.random.set_seed(42)
 def iter_shuffled(X, columns_to_shuffle=None, pre_shuffle=False,
random_state=None):
rng = check_random_state(random_state)
 if columns_to_shuffle is None:
columns_to_shuffle = range(X.shape[1])
 if pre_shuffle:
X_shuffled = X.copy()
rng.shuffle(X_shuffled)
 X_res = X.copy()
for columns in tqdm(columns_to_shuffle):
if pre_shuffle:
X_res[:, columns] = X_shuffled[:, columns]
else:
rng.shuffle(X_res[:, columns])
 yield X_res
X_res[:, columns] = X[:, columns]
def get_score_importances(
 score_func,
X,
y,
n_iter=5,
columns_to_shuffle=None,
random_state=None 
):
rng = check_random_state(random_state)
 base_score = score_func(X, y)
 scores_decreases = []
for i in range(n_iter):
scores_shuffled = _get_scores_shufled(
 score_func, X, y, columns_to_shuffle=columns_to_shuffle,
random_state=rng, base_score=base_score 
)
 scores_decreases.append(scores_shuffled)
 return base_score, scores_decreases
def _get_scores_shufled(score_func, X, y, base_score, columns_to_shuffle=None,
random_state=None):
Xs = iter_shuffled(X, columns_to_shuffle, random_state=random_state)
 res = []
for X_shuffled in Xs:
res.append(-score_func(X_shuffled, y)+ base_score)
 return res
def metric(y_true, y_pred):
metrics = []
for i in range(y_pred.shape[1]):
if y_true[:, i].sum() > 1:
metrics.append(log_loss(y_true[:, i], y_pred[:, i]))
return np.mean(metrics)
 perm_imp = np.zeros(train.shape[1])
 for n,(tr, te)in enumerate(KFold(n_splits=2, random_state=seed, shuffle=True ).split(train_targets)) :
print(f'Fold {n}')
 model = create_model(len(list(train.columns)))
 reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, verbose=1, epsilon=1e-4, mode='min')
 model.fit(train.values[tr],
train_targets.values[tr],
validation_data=(train.values[te], train_targets.values[te]),
epochs=40, batch_size=128,
callbacks=[reduce_lr_loss], verbose=2 
)
 def _score(X, y):
pred = model.predict(X)
 return metric(y, pred)
 base_score, local_imp = get_score_importances(_score, train.values[te], train_targets.values[te], n_iter=1, random_state=0)
 perm_imp += np.mean(local_imp, axis=0)
 print('')
 break
top_feats = np.argwhere(perm_imp < 0 ).flatten()
train_match['month'] =(train_match['date_block_num']%12)+1<data_type_conversions>
def metric(y_true, y_pred):
metrics = []
for _target in train_targets.columns:
metrics.append(log_loss(y_true.loc[:, _target], y_pred.loc[:, _target].astype(float), labels=[0,1]))
return np.mean(metrics)
sale_record = {}
train_match['last_item_sale'] = -1
train_match['last_item_sale'] = train_match['last_item_sale'].astype('int8')
 for idx,row in train_match.iterrows() :
key = row.item_id
if key not in sale_record.keys() :
if row.item_cnt_month != 0:
sale_record[key] = row.date_block_num
else:
last_date_block_num = sale_record[key]
if row.date_block_num > last_date_block_num:
train_match.at[idx,'last_item_sale'] = row.date_block_num - last_date_block_num
sale_record[key] = row.date_block_num 
<filter>
print(f'OOF Metric: {metric(train_targets, res)}' )
train_match = train_match[train_match['date_block_num']>11]<correct_missing_values>
ss.loc[test['cp_type']==1, train_targets.columns] = 0
<load_pretrained><EOS> 
ss.to_csv('submission.csv', index=False )