Kaggle competition: Elo Merchant Category Recommendation
<SOS> metric: RMSE Kaggle data source: elo-merchant-category-recommendation<train_model>
import numpy as np
import pandas as pd
import time
import lightgbm as lgb
from sklearn.model_selection import StratifiedKFold, KFold
from sklearn.metrics import mean_squared_error
from sklearn.metrics import log_loss
def train() :
STEPS_PER_EPOCH = count_data_items(train_filenames)// BATCH_SIZE
history = model.fit(
 train_dataset,
epochs=EPOCHS,
callbacks=[lr_schedule],
steps_per_epoch=STEPS_PER_EPOCH,
validation_data=valid_dataset)
 string = 'Train acc:{:.4f} Train loss:{:.4f} AUC: {:.4f}, Val acc:{:.4f} Val loss:{:.4f} Val AUC: {:.4f}'.format(\
model.history.history['accuracy'][-1],model.history.history['loss'][-1],\
model.history.history['auc'][-1],\
model.history.history['val_accuracy'][-1],model.history.history['val_loss'][-1],\
model.history.history['val_auc'][-1])
 return string<train_model>
%%time
df_train = pd.read_csv('.. /input/predicting-outliers-to-improve-your-score/train_clean.csv')
 df_test = pd.read_csv('.. /input/predicting-outliers-to-improve-your-score/test_clean.csv' )
train()<load_from_csv>
df_train = df_train[df_train['outliers'] == 0]
target = df_train['target']
del df_train['target']
features = [c for c in df_train.columns if c not in ['card_id', 'first_active_month','outliers']]
categorical_feats = [c for c in features if 'feature_' in c]
num_test_images = count_data_items(test_filenames)
 submission_df = pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/sample_submission.csv')
 for i in range(TTA):
test_dataset =(load_dataset(test_filenames, labeled=False,ordered=True)
.map(data_augment, num_parallel_calls=AUTO)
.batch(BATCH_SIZE))
test_dataset_images = test_dataset.map(lambda image, image_name: image)
 test_dataset_image_name = test_dataset.map(lambda image, image_name: image_name ).unbatch()
test_ids = next(iter(test_dataset_image_name.batch(num_test_images)) ).numpy().astype('U')
 test_pred = model.predict(test_dataset_images, verbose=1)
 pred_df = pd.DataFrame({'image_name': test_ids, 'target': np.concatenate(test_pred)})
 temp = submission_df.copy()
del temp['target']
submission_df['target'] += temp.merge(pred_df,on="image_name")['target']/TTA<save_to_csv>
param = {'objective':'regression',
'num_leaves': 31,
'min_data_in_leaf': 25,
'max_depth': 7,
'learning_rate': 0.01,
'lambda_l1':0.13,
"boosting": "gbdt",
"feature_fraction":0.85,
'bagging_freq':8,
"bagging_fraction": 0.9 ,
"metric": 'rmse',
"verbosity": -1,
"random_state": 2333}
submission_df.to_csv('submission.csv', index=False)
 pd.Series(np.round(submission_df['target'].values)).value_counts() 
<import_modules>
%%time
folds = StratifiedKFold(n_splits=5, shuffle=True, random_state=2333)
 oof = np.zeros(len(df_train))
predictions = np.zeros(len(df_test))
feature_importance_df = pd.DataFrame()
for fold_,(trn_idx, val_idx)in enumerate(folds.split(df_train,df_train['outliers'].values)) :
print("fold {}".format(fold_))
trn_data = lgb.Dataset(df_train.iloc[trn_idx][features], label=target.iloc[trn_idx])
 val_data = lgb.Dataset(df_train.iloc[val_idx][features], label=target.iloc[val_idx])
 num_round = 10000
clf = lgb.train(param, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval= 100, early_stopping_rounds = 200)
 oof[val_idx] = clf.predict(df_train.iloc[val_idx][features], num_iteration=clf.best_iteration)
 fold_importance_df = pd.DataFrame()
fold_importance_df["Feature"] = features
fold_importance_df["importance"] = clf.feature_importance()
fold_importance_df["fold"] = fold_ + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 predictions += clf.predict(df_test[features], num_iteration=clf.best_iteration)/ folds.n_splits
print("CV score: {:<8.5f}".format(mean_squared_error(oof, target)**0.5))
import numpy as np
import pandas as pd
import os<define_variables>
model_without_outliers = pd.DataFrame({"card_id":df_test["card_id"].values})
 model_without_outliers["target"] = predictions
sub_path = ".. /input/top-my-submission"
all_files = os.listdir(sub_path )<feature_engineering>
%%time
df_train = pd.read_csv('.. /input/predicting-outliers-to-improve-your-score/train_clean.csv')
 df_test = pd.read_csv('.. /input/predicting-outliers-to-improve-your-score/test_clean.csv' )
concat_sub['target_mean'] = concat_sub.iloc[:, 1:ncol].mean(axis=1)
 concat_sub['target_median'] = concat_sub.iloc[:, 1:ncol].median(axis=1 )<save_to_csv>
target = df_train['outliers']
del df_train['outliers']
del df_train['target']
concat_sub['target'] = concat_sub['target_mean']
concat_sub[['image_name', 'target']].to_csv('submission_mean.csv',
index=False, float_format='%.6f' )<save_to_csv>
features = [c for c in df_train.columns if c not in ['card_id', 'first_active_month']]
categorical_feats = [c for c in features if 'feature_' in c]
concat_sub['target'] = concat_sub['target_median']
concat_sub[['image_name', 'target']].to_csv('submission_median.csv',
index=False, float_format='%.6f' )<install_modules>
param = {'num_leaves': 31,
'min_data_in_leaf': 30,
'objective':'binary',
'max_depth': 6,
'learning_rate': 0.01,
"boosting": "rf",
"feature_fraction": 0.9,
"bagging_freq": 1,
"bagging_fraction": 0.9 ,
"bagging_seed": 11,
"metric": 'binary_logloss',
"lambda_l1": 0.1,
"verbosity": -1,
"random_state": 2333}
!pip install -q efficientnet<import_modules>
%%time
folds = KFold(n_splits=5, shuffle=True, random_state=15)
 oof = np.zeros(len(df_train))
predictions = np.zeros(len(df_test))
feature_importance_df = pd.DataFrame()
start = time.time()
for fold_,(trn_idx, val_idx)in enumerate(folds.split(df_train.values, target.values)) :
print("fold n°{}".format(fold_))
trn_data = lgb.Dataset(df_train.iloc[trn_idx][features], label=target.iloc[trn_idx], categorical_feature=categorical_feats)
 val_data = lgb.Dataset(df_train.iloc[val_idx][features], label=target.iloc[val_idx], categorical_feature=categorical_feats)
 num_round = 10000
clf = lgb.train(param, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=100, early_stopping_rounds = 200)
 oof[val_idx] = clf.predict(df_train.iloc[val_idx][features], num_iteration=clf.best_iteration)
 fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = features
fold_importance_df["importance"] = clf.feature_importance()
fold_importance_df["fold"] = fold_ + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 predictions += clf.predict(df_test[features], num_iteration=clf.best_iteration)/ folds.n_splits
print("CV score: {:<8.5f}".format(log_loss(target, oof)) )
import re
import cv2
import math
import time
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tqdm import tqdm_notebook as tqdm
from sklearn.model_selection import train_test_split
from kaggle_datasets import KaggleDatasets
import tensorflow as tf
import tensorflow.keras.backend as K
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Model,Sequential
from tensorflow.keras import optimizers
import efficientnet.tfkeras as efn<set_options>
df_outlier_prob = pd.DataFrame({"card_id":df_test["card_id"].values})
 df_outlier_prob["target"] = predictions
df_outlier_prob.head()
use_knockknock = True
if use_knockknock:
!pip install knockknock
user_secrets = UserSecretsClient()
token = user_secrets.get_secret("token")
 chat_id = user_secrets.get_secret("chat_id" )<train_on_grid>
outlier_id = pd.DataFrame(df_outlier_prob.sort_values(by='target',ascending = False ).head(25000)['card_id'] )
AUTO = tf.data.experimental.AUTOTUNE
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync)
 DATASET = '512x512-melanoma-tfrecords-70k-images'
GCS_PATH = KaggleDatasets().get_gcs_path(DATASET )<define_variables>
best_submission = pd.read_csv('.. /input/finaldata/submission_ashish.csv' )
SEED = 42
BATCH_SIZE = 32 * strategy.num_replicas_in_sync
SIZE = [512,512]
LR = 0.00004
EPOCHS = 12
WARMUP = 5
WEIGHT_DECAY = 0
LABEL_SMOOTHING = 0.05
TTA = 4<define_variables>
most_likely_liers = best_submission.merge(outlier_id,how='right')
 most_likely_liers.head()
def seed_everything(SEED):
np.random.seed(SEED)
 tf.random.set_seed(SEED)
 seed_everything(SEED)
 train_filenames = tf.io.gfile.glob(GCS_PATH + '/train*.tfrec')
 test_filenames = tf.io.gfile.glob(GCS_PATH + '/test*.tfrec' )<split>
%%time
for card_id in most_likely_liers['card_id']:
model_without_outliers.loc[model_without_outliers['card_id']==card_id,'target']\
= most_likely_liers.loc[most_likely_liers['card_id']==card_id,'target'].values
<normalization><EOS> 
model_without_outliers.to_csv("combining_submission.csv", index=False )