Kaggle competition: IEEE-CIS Fraud Detection
<SOS> metric: AUC Kaggle data source: ieee-cis-fraud-detection<feature_engineering>
warnings.filterwarnings('ignore' )
out = out/n_folds
alpha = alpha/n_folds<categorify>
def seed_everything(seed=0):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 def reduce_mem_usage(df, verbose=True):
numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
start_mem = df.memory_usage().sum() / 1024**2
for col in df.columns:
col_type = df[col].dtypes
if col_type in numerics:
c_min = df[col].min()
c_max = df[col].max()
if str(col_type)[:3] == 'int':
if c_min > np.iinfo(np.int8 ).min and c_max < np.iinfo(np.int8 ).max:
df[col] = df[col].astype(np.int8)
 elif c_min > np.iinfo(np.int16 ).min and c_max < np.iinfo(np.int16 ).max:
df[col] = df[col].astype(np.int16)
 elif c_min > np.iinfo(np.int32 ).min and c_max < np.iinfo(np.int32 ).max:
df[col] = df[col].astype(np.int32)
 elif c_min > np.iinfo(np.int64 ).min and c_max < np.iinfo(np.int64 ).max:
df[col] = df[col].astype(np.int64)
 else:
if c_min > np.finfo(np.float16 ).min and c_max < np.finfo(np.float16 ).max:
df[col] = df[col].astype(np.float16)
 elif c_min > np.finfo(np.float32 ).min and c_max < np.finfo(np.float32 ).max:
df[col] = df[col].astype(np.float32)
 else:
df[col] = df[col].astype(np.float64)
 end_mem = df.memory_usage().sum() / 1024**2
if verbose: print('Mem.usage decreased to {:5.2f} Mb({:.1f}% reduction)'.format(end_mem, 100 *(start_mem - end_mem)/ start_mem))
return df
def rle_encode(im):

 pixels = im.flatten(order='F')
 pixels = np.concatenate([[0], pixels, [0]])
 runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
runs[1::2] -= runs[::2]
return ' '.join(str(x)for x in runs )<create_dataframe>
def make_predictions(tr_df, tt_df, features_columns, target, lgb_params, NFOLDS=2):
folds = KFold(n_splits=NFOLDS, shuffle=True, random_state=SEED)
 X,y = tr_df[features_columns], tr_df[target]
P,P_y = tt_df[features_columns], tt_df[target]
tt_df = tt_df[['TransactionID',target]]
predictions = np.zeros(len(tt_df))
for fold_,(trn_idx, val_idx)in enumerate(folds.split(X, y)) :
print('Fold:',fold_)
 tr_x, tr_y = X.iloc[trn_idx,:], y[trn_idx]
vl_x, vl_y = X.iloc[val_idx,:], y[val_idx]
print(len(tr_x),len(vl_x))
tr_data = lgb.Dataset(tr_x, label=tr_y)
 if LOCAL_TEST:
vl_data = lgb.Dataset(P, label=P_y)
 else:
vl_data = lgb.Dataset(vl_x, label=vl_y)
 estimator = lgb.train(
 lgb_params,
tr_data,
valid_sets = [tr_data, vl_data],
verbose_eval = 200, 
)
 pp_p = estimator.predict(P)
 predictions += pp_p/NFOLDS
if LOCAL_TEST:
feature_imp = pd.DataFrame(sorted(zip(estimator.feature_importance() ,X.columns)) , columns=['Value','Feature'])
 print(feature_imp)
 del tr_x, tr_y, vl_x, vl_y, tr_data, vl_data
gc.collect()
tt_df['prediction'] = predictions
return tt_df
sub = pd.DataFrame(list(zip(name_list, tmp_list)) , columns = ['id', 'rle_mask'] )<save_to_csv>
SEED = 42
seed_everything(SEED)
 LOCAL_TEST = False
TARGET = 'isFraud'
START_DATE = datetime.datetime.strptime('2017-11-30', '%Y-%m-%d' )
sub.to_csv('submission.csv', index=False )<save_to_csv>
lgb_params = {
'objective':'binary',
'boosting_type':'gbdt',
'metric':'auc',
'n_jobs':-1,
'learning_rate':0.01,
'num_leaves': 2**8,
'max_depth':-1,
'tree_learner':'serial',
'colsample_bytree': 0.7,
'subsample_freq':1,
'subsample':0.7,
'n_estimators':800,
'max_bin':255,
'verbose':-1,
'seed': SEED,
'early_stopping_rounds':100,
}
sub.to_csv('submission.csv', index=False )<normalization>
print('Load Data')
 train_df = pd.read_pickle('.. /input/ieee-fe-with-some-eda/train_df.pkl')
 if LOCAL_TEST:
test_df = train_df[train_df['DT_M']==train_df['DT_M'].max() ].reset_index(drop=True)
 train_df = train_df[train_df['DT_M']<(train_df['DT_M'].max() -1)].reset_index(drop=True)
 else:
test_df = pd.read_pickle('.. /input/ieee-fe-with-some-eda/test_df.pkl')
 remove_features = pd.read_pickle('.. /input/ieee-fe-with-some-eda/remove_features.pkl')
 remove_features = list(remove_features['features_to_remove'].values)
 print('Shape control:', train_df.shape, test_df.shape )
try:
except ImportError:
def lovasz_grad(gt_sorted):

 p = len(gt_sorted)
 gts = gt_sorted.sum()
intersection = gts - gt_sorted.float().cumsum(0)
 union = gts +(1 - gt_sorted ).float().cumsum(0)
 jaccard = 1.- intersection / union
if p > 1:
jaccard[1:p] = jaccard[1:p] - jaccard[0:-1]
return jaccard
def iou_binary(preds, labels, EMPTY=1., ignore=None, per_image=True):

 if not per_image:
preds, labels =(preds,),(labels,)
 ious = []
for pred, label in zip(preds, labels):
intersection =(( label == 1)&(pred == 1)).sum()
union =(( label == 1)|(( pred == 1)&(label != ignore)) ).sum()
if not union:
iou = EMPTY
else:
iou = float(intersection)/ union
ious.append(iou)
 iou = mean(ious)
 return 100 * iou
def iou(preds, labels, C, EMPTY=1., ignore=None, per_image=False):

 if not per_image:
preds, labels =(preds,),(labels,)
 ious = []
for pred, label in zip(preds, labels):
iou = []
for i in range(C):
if i != ignore:
intersection =(( label == i)&(pred == i)).sum()
union =(( label == i)|(( pred == i)&(label != ignore)) ).sum()
if not union:
iou.append(EMPTY)
 else:
iou.append(float(intersection)/ union)
 ious.append(iou)
 ious = map(mean, zip(*ious))
return 100 * np.array(ious)
 def lovasz_hinge(logits, labels, per_image=True, ignore=None):

 if per_image:
loss = mean(lovasz_hinge_flat(*flatten_binary_scores(log.unsqueeze(0), lab.unsqueeze(0), ignore))
for log, lab in zip(logits, labels))
else:
loss = lovasz_hinge_flat(*flatten_binary_scores(logits, labels, ignore))
return loss
def lovasz_hinge_flat(logits, labels):

 if len(labels)== 0:
return logits.sum() * 0.
 signs = 2.* labels.float() - 1.
 errors =(1.- logits * Variable(signs))
errors_sorted, perm = torch.sort(errors, dim=0, descending=True)
 perm = perm.data
gt_sorted = labels[perm]
grad = lovasz_grad(gt_sorted)
 loss = torch.dot(F.elu(errors_sorted)+1, Variable(grad))
return loss
def flatten_binary_scores(scores, labels, ignore=None):

 scores = scores.view(-1)
 labels = labels.view(-1)
 if ignore is None:
return scores, labels
valid =(labels != ignore)
 vscores = scores[valid]
vlabels = labels[valid]
return vscores, vlabels
class StableBCELoss(torch.nn.modules.Module):
def __init__(self):
super(StableBCELoss, self ).__init__()
def forward(self, input, target):
neg_abs = - input.abs()
loss = input.clamp(min=0)- input * target +(1 + neg_abs.exp() ).log()
return loss.mean()
def binary_xloss(logits, labels, ignore=None):

 logits, labels = flatten_binary_scores(logits, labels, ignore)
 loss = StableBCELoss()(logits, Variable(labels.float()))
return loss
def lovasz_softmax(probas, labels, only_present=False, per_image=False, ignore=None):

 if per_image:
loss = mean(lovasz_softmax_flat(*flatten_probas(prob.unsqueeze(0), lab.unsqueeze(0), ignore), only_present=only_present)
 for prob, lab in zip(probas, labels))
else:
loss = lovasz_softmax_flat(*flatten_probas(probas, labels, ignore), only_present=only_present)
 return loss
def lovasz_softmax_flat(probas, labels, only_present=False):

 C = probas.size(1)
 losses = []
for c in range(C):
fg =(labels == c ).float()
if only_present and fg.sum() == 0:
continue
errors =(Variable(fg)- probas[:, c] ).abs()
errors_sorted, perm = torch.sort(errors, 0, descending=True)
 perm = perm.data
fg_sorted = fg[perm]
losses.append(torch.dot(errors_sorted, Variable(lovasz_grad(fg_sorted))))
return mean(losses)
 def flatten_probas(probas, labels, ignore=None):

 B, C, H, W = probas.size()
probas = probas.permute(0, 2, 3, 1 ).contiguous().view(-1, C)
 labels = labels.view(-1)
 if ignore is None:
return probas, labels
valid =(labels != ignore)
 vprobas = probas[valid.nonzero().squeeze() ]
vlabels = labels[valid]
return vprobas, vlabels
def xloss(logits, labels, ignore=None):

 return F.cross_entropy(logits, Variable(labels), ignore_index=255)
 def mean(l, ignore_nan=False, empty=0):

 l = iter(l)
 if ignore_nan:
l = ifilterfalse(np.isnan, l)
 try:
n = 1
acc = next(l)
 except StopIteration:
if empty == 'raise':
raise ValueError('Empty mean')
 return empty
for n, v in enumerate(l, 2):
acc += v
if n == 1:
return acc
return acc / n<set_options>
features_columns = [col for col in list(train_df)if col not in remove_features]
train_df = reduce_mem_usage(train_df)
 test_df = reduce_mem_usage(test_df )
torch.cuda.set_device(0 )<load_from_csv>
if LOCAL_TEST:
lgb_params['learning_rate'] = 0.01
lgb_params['n_estimators'] = 20000
lgb_params['early_stopping_rounds'] = 100
test_predictions = make_predictions(train_df, test_df, features_columns, TARGET, lgb_params)
 print(metrics.roc_auc_score(test_predictions[TARGET], test_predictions['prediction']))
else:
lgb_params['learning_rate'] = 0.005
lgb_params['n_estimators'] = 1800
lgb_params['early_stopping_rounds'] = 100
test_predictions = make_predictions(train_df, test_df, features_columns, TARGET, lgb_params, NFOLDS=8 )
<define_variables><EOS> 
if not LOCAL_TEST:
test_predictions['isFraud'] = test_predictions['prediction']
test_predictions[['TransactionID','isFraud']].to_csv('submission.csv', index=False )