Kaggle competition: Santander Customer Transaction Prediction
<SOS> metric: AUC Kaggle data source: santander-customer-transaction-prediction<set_options>
sns.set(font_scale=1)
 warnings.simplefilter(action='ignore', category=FutureWarning)
 warnings.filterwarnings('ignore' )
SEED = 2021
DEBUG = False
IMG_DIM = 512
BATCH_SIZE = 16
NUM_WORKERS = 4
N_FOLDS = 5
FOLDS_IDS = [0]
LR = 1e-4
NUM_EPOCHS = 20
DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 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
torch.backends.cudnn.benchmark = True
seed_everything(SEED )<load_from_csv>
random_state = 42
np.random.seed(random_state)
 train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
source = Path(".. /input/vinbigdata-512-png")
 train_df = pd.read_csv(source / 'train.csv')
 test_df = pd.read_csv(source / 'test.csv')
 submission_df = pd.read_csv(source / 'sample_submission.csv')
 submission_df.head()<categorify>
def augment(x,y,t=2):
xs,xn = [],[]
for i in range(t):
mask = y>0
x1 = x[mask].copy()
ids = np.arange(x1.shape[0])
 for c in range(x1.shape[1]):
np.random.shuffle(ids)
 x1[:,c] = x1[ids][:,c]
xs.append(x1)
 for i in range(t//2):
mask = y==0
x1 = x[mask].copy()
ids = np.arange(x1.shape[0])
 for c in range(x1.shape[1]):
np.random.shuffle(ids)
 x1[:,c] = x1[ids][:,c]
xn.append(x1)
 xs = np.vstack(xs)
 xn = np.vstack(xn)
 ys = np.ones(xs.shape[0])
 yn = np.zeros(xn.shape[0])
 x = np.vstack([x,xs,xn])
 y = np.concatenate([y,ys,yn])
 return x,y
class VinBigDataFilepathParser(parsers.Parser, parsers.FilepathMixin):
pass
VinBigDataFilepathParser.generate_template()<categorify>
lgb_params = {
"objective" : "binary",
"metric" : "auc",
"boosting": 'gbdt',
"max_depth" : -1,
"num_leaves" : 13,
"learning_rate" : 0.01,
"bagging_freq": 5,
"bagging_fraction" : 0.4,
"feature_fraction" : 0.05,
"min_data_in_leaf": 80,
"min_sum_heassian_in_leaf": 10,
"tree_learner": "serial",
"boost_from_average": "false",
"bagging_seed" : random_state,
"verbosity" : 1,
"seed": random_state}
class VinBigDataFilepathParser(parsers.Parser, parsers.FilepathMixin):
def __init__(self, df, source):
self.df = df
self.source = source
def __iter__(self):
yield from self.df.itertuples()
def __len__(self):
return len(self.df)
 def imageid(self, o)-> Hashable:
return o.image_id
def filepath(self, o)-> Union[str, Path]:
return self.source / f"{o.image_id}.png"
def image_width_height(self, o)-> Tuple[int, int]:
return get_image_size(self.filepath(o))<load_pretrained>
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=random_state)
 oof = train[['ID_code', 'target']]
oof['predict'] = 0
predictions = test[['ID_code']]
val_aucs = []
feature_importance = pd.DataFrame()
parser = VinBigDataFilepathParser(submission_df, source / "test")
 records = parser.parse(data_splitter=None, idmap=None, autofix=True, show_pbar=True, cache_filepath=None)
 test_rs = records[0] + records[1]<normalization>
features = [col for col in train.columns if col not in ['target', 'ID_code']]
X_test = test[features].values
presize = 512
size = 384
infer_tfms = tfms.A.Adapter([*tfms.A.resize_and_pad(size), tfms.A.Normalize() ] )<create_dataframe>
for fold,(trn_idx, val_idx)in enumerate(skf.split(train, train['target'])) :
X_train, y_train = train.iloc[trn_idx][features], train.iloc[trn_idx]['target']
X_valid, y_valid = train.iloc[val_idx][features], train.iloc[val_idx]['target']
N = 5
p_valid,yp = 0,0
for i in range(N):
X_t, y_t = augment(X_train.values, y_train.values)
 X_t = pd.DataFrame(X_t)
 X_t = X_t.add_prefix('var_')
 trn_data = lgb.Dataset(X_t, label=y_t)
 val_data = lgb.Dataset(X_valid, label=y_valid)
 evals_result = {}
lgb_clf = lgb.train(lgb_params,
trn_data,
100000,
valid_sets = [trn_data, val_data],
early_stopping_rounds=3000,
verbose_eval = 1000,
evals_result=evals_result 
)
 p_valid += lgb_clf.predict(X_valid)
 yp += lgb_clf.predict(X_test)
 fold_importance = pd.DataFrame()
fold_importance["feature"] = features
fold_importance["importance"] = lgb_clf.feature_importance()
fold_importance["fold"] = fold + 1
feature_importance = pd.concat([feature_importance, fold_importance], axis=0)
 oof['predict'][val_idx] = p_valid/N
val_score = roc_auc_score(y_valid, p_valid)
 val_aucs.append(val_score)
 predictions['fold{}'.format(fold+1)] = yp/N
infer_ds = Dataset(test_rs, infer_tfms )<load_pretrained>
predictions['target'] = np.mean(predictions[[col for col in predictions.columns if col not in ['ID_code', 'target']]].values, axis=1)
 predictions.to_csv('lgb_all_predictions.csv', index=None)
 sub = pd.DataFrame({"ID_code":test["ID_code"].values})
 sub["target"] = predictions['target']
sub.to_csv("lgb_submission.csv", index=False)
 oof.to_csv('lgb_oof.csv', index=False )