Kaggle competition: Mechanisms of Action (MoA) Prediction
%matplotlib inline
sns.set(palette=sns.color_palette('Set2',9)) 
<load_from_csv>
!pip install /kaggle/input/whl-datasets/iterative_stratification-0.1.6-py3-none-any.whl
warnings.simplefilter("ignore" )
train_path='/kaggle/input/competitive-data-science-predict-future-sales/sales_train.csv'
test_path='/kaggle/input/competitive-data-science-predict-future-sales/test.csv'
train=pd.read_csv(train_path)
 test=pd.read_csv(test_path)
 items_path='/kaggle/input/competitive-data-science-predict-future-sales/items.csv'
item_categories_path='/kaggle/input/competitive-data-science-predict-future-sales/item_categories.csv'
shops_path='/kaggle/input/competitive-data-science-predict-future-sales/shops.csv'
items=pd.read_csv(items_path)
 item_cat=pd.read_csv(item_categories_path)
 shops=pd.read_csv(shops_path)
 sample_submission_path='/kaggle/input/competitive-data-science-predict-future-sales/sample_submission.csv'
sample_submission=pd.read_csv(sample_submission_path )<filter>
trainFeatures = pd.read_csv('/kaggle/input/lish-moa/train_features.csv')
 testFeatures = pd.read_csv('/kaggle/input/lish-moa/test_features.csv')
 trainTargetsS = pd.read_csv('/kaggle/input/lish-moa/train_targets_scored.csv')
 trainTargetsNS = pd.read_csv('/kaggle/input/lish-moa/train_targets_nonscored.csv')
 submission = pd.read_csv('/kaggle/input/lish-moa/sample_submission.csv')
 for file in(trainFeatures, testFeatures, trainTargetsS, trainTargetsNS):
file.columns = file.columns.str.lower().str.strip().str.replace(' ', '_' ).str.replace('(', '' ).str.replace(')', '')
 print('Train features shape: ', trainFeatures.shape)
 print('Test features shape: ', testFeatures.shape)
 print('Train Targets(Scored)shape: ', trainTargetsS.shape)
 print('Train Targets(Non Scored)shape: ', trainTargetsNS.shape)
 print('------- Train Features view -------')
 trainFeatures.head()
train=train[train.item_price<100000]
train=train[train.item_cnt_day<1100]<filter>
print('Compound Treatment', round(trainFeatures['cp_type'].value_counts() [0]/len(trainFeatures)* 100, 2), '% of the dataset')
 print('Control Perturbation Treatment', round(trainFeatures['cp_type'].value_counts() [1]/len(trainFeatures)* 100, 2), '% of the dataset' )
train[train.item_price<0]<feature_engineering>
plt.figure(figsize =(20, 6))
targetTypes = []
for column in trainTargetsS.columns:
try:
targetTypes.append(column.rsplit('_', 1)[1])
 except:
targetTypes.append(column.rsplit('_', 1)[0])
 targetTypes = list(dict.fromkeys(targetTypes))
targetTypes.remove('id')
 targetTypes.remove('b')
 targets = {}
for i in targetTypes:
targets[i] = 0
for column in trainTargetsS.columns:
try:
col = column.rsplit('_', 1)[1]
if col not in ['id', 'b']:
targets[col] += 1
else: pass
except:
col = column.rsplit('_', 1)[0]
if col not in ['id', 'b']:
targets[col] += 1
else: pass
targets = pd.DataFrame.from_dict(targets, orient = 'index')
 targets = targets.reset_index()
targets.columns = ['target_type', 'types']
sns.barplot(x = 'target_type', y = 'types', data = targets)
 plt.xticks(rotation = 90)
 plt.title('Types of Target variables')
 plt.show()
median=train[(train.shop_id==32)&(train.item_id==2973)&(train.date_block_num==4)&(train.item_price>0)].median()
train.loc[train.item_price<0,'item_price']=median<feature_engineering>
def preprocess(df):
df = df.copy()
df.loc[:, 'cp_type'] = df.loc[:, 'cp_type'].map({'trt_cp': 0, 'ctl_vehicle': 1})
 df.loc[:, 'cp_dose'] = df.loc[:, 'cp_dose'].map({'D1': 0, 'D2': 1})
 del df['sig_id']
return df
train = preprocess(trainFeatures)
 test = preprocess(testFeatures)
 del trainTargetsS['sig_id']
trainTargetsS = trainTargetsS.loc[train['cp_type'] == 0].reset_index(drop = True)
 train = train.loc[train['cp_type'] == 0].reset_index(drop = True )
train.loc[train.shop_id == 0, 'shop_id'] = 57
test.loc[test.shop_id == 0, 'shop_id'] = 57
train.loc[train.shop_id == 1, 'shop_id'] = 58
test.loc[test.shop_id == 1, 'shop_id'] = 58
train.loc[train.shop_id == 10, 'shop_id'] = 11
test.loc[test.shop_id == 10, 'shop_id'] = 11<filter>
def create_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(2048, activation="relu")) ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.5),
tfa.layers.WeightNormalization(tf.keras.layers.Dense(1048, 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
train[train.item_price.isnull() ]<filter>
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].astype(float)))
 return np.mean(metrics)
 perm_imp = np.zeros(train.shape[1])
 all_res = []
for n,(tr, te)in enumerate(KFold(n_splits=7, random_state=0, shuffle=True ).split(trainTargetsS)) :
print(f'Fold {n}')
 model = create_model(len(train.columns))
checkpoint_path = f'repeat:{seed}_Fold:{n}.hdf5'
reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, verbose=1, epsilon=1e-4, mode='min')
 cb_checkpt = ModelCheckpoint(checkpoint_path, monitor = 'val_loss', verbose = 0, save_best_only = True,
save_weights_only = True, mode = 'min')
 model.fit(train.values[tr],
trainTargetsS.values[tr],
validation_data=(train.values[te], trainTargetsS.values[te]),
epochs=35, batch_size=128,
callbacks=[reduce_lr_loss, cb_checkpt], verbose=2 
)
 model.load_weights(checkpoint_path)
 def _score(X, y):
pred = model.predict(X)
 return metric(y, pred)
 base_score, local_imp = get_score_importances(_score, train.values[te], trainTargetsS.values[te], n_iter=1, random_state=0)
 all_res.append(local_imp)
 perm_imp += np.mean(local_imp, axis=0)
 print('')
 top_feats = np.argwhere(perm_imp < 0 ).flatten()
top_feats
train[(train.item_id==2973)&(train.shop_id==32)]<feature_engineering>
def metric(y_true, y_pred):
metrics = []
for _target in trainTargetsS.columns:
metrics.append(log_loss(y_true.loc[:, _target], y_pred.loc[:, _target].astype(float), labels = [0,1]))
return np.mean(metrics )
train.loc[train.item_price.isnull() ,'item_price']=1249.0<categorify>
print(f'OOF Metric: {metric(trainTargetsS, res)}')
 submission.loc[test['cp_type'] == 1, trainTargetsS.columns] = 0
submission.to_csv('submission.csv', index = False)
 submission.head()