Kaggle competition: IEEE-CIS Fraud Detection
<SOS> metric: AUC Kaggle data source: ieee-cis-fraud-detection<drop_column>
import pandas as pd
import datetime
all_data.drop(['_days'], axis=1, inplace=True)
 all_data.drop(['TransactionDT'], axis=1, inplace=True)
 uid = all_data['_uid2']
drop_card_cols = ['_uid1','_uid2','card1','card2'] 
<categorify>
train_transaction = pd.read_csv('.. /input/ieee-fraud-detection/train_transaction.csv')
 test_transaction = pd.read_csv('.. /input/ieee-fraud-detection/test_transaction.csv' )
_=
cat_cols = ['ProductCD','card1','card2','card3','card4','card5','card6','addr1','addr2','P_emaildomain','R_emaildomain',
'M1','M2','M3','M4','M5','M6','M7','M8','M9','DeviceType','DeviceInfo'] + [f'id_{i}' for i in range(12,39)]
for i in cat_cols:
if i in all_data.columns:
all_data[i] = all_data[i].astype(str)
 enc_cols = []
for i, t in all_data.loc[:, all_data.columns != 'isFraud'].dtypes.iteritems() :
if t == object:
enc_cols.append(i)
 all_data[i] = pd.factorize(all_data[i])[0]
print(enc_cols )<prepare_x_and_y>
submit = pd.read_csv(".. /input/ieee-gb-2-make-amount-useful-again/submission.csv" )
X_train = all_data[all_data['isFraud'].notnull() ]
X_test = all_data[all_data['isFraud'].isnull() ].drop('isFraud', axis=1)
 Y_train = X_train.pop('isFraud')
 uid_train = uid[all_data['isFraud'].notnull() ]
uid_fraud = uid_train[Y_train == 1]
uid_test = uid[all_data['isFraud'].isnull() ]
train_group = trandate[:len(X_train)].dt.month
del uid
del all_data<prepare_x_and_y>
def make_day(df):
def fillna(x):
if "nan" in x:
return np.nan
else:
return x
START_DATE = datetime.datetime.strptime('2017-11-30', '%Y-%m-%d')
 df['Date'] = df['TransactionDT'].apply(lambda x:(START_DATE + datetime.timedelta(seconds=x)))
 df['Day'] =(df["Date"].dt.year - 2017)* 365 + df["Date"].dt.dayofyear
df["ID1"] = df["card1"].astype(str)+df["card2"].astype(str)+df["card3"].astype(str)+df["card4"].astype(str)+\
df["card5"].astype(str)+df["card6"].astype(str)
 df["ID_D1"] = fillna(df["ID1"].astype(str)+(df["Day"] - df["D1"] ).astype(str))
df["ID_D1D10"] = fillna(df["ID_D1"].astype(str)+(df["Day"] - df["D10"] ).astype(str))
df["ID_D1D12"] = fillna(df["ID_D1"].astype(str)+(df["Day"] - df["D12"] ).astype(str))
return df
_=<categorify>
train_transaction = make_day(train_transaction)
 test_transaction = make_day(test_transaction )
cat_cols = ['card1','card2','card3','card5','addr1','_uid1','_uid2','_P_emaildomain__ProductCD','_card3__card5']
for f in cat_cols:
if f in X_train.columns:
train_set = set(X_train[f])
 test_set = set(X_test[f])
 tt = train_set.intersection(test_set)
 print(f, '-', 'train:%.3f'%(len(tt)/len(train_set)) , ',test:%.3f'%(len(tt)/len(test_set)))
 X_train[f] = X_train[f].map(lambda x: -999 if x not in tt else x)
 X_test[f] = X_test[f].map(lambda x: -999 if x not in tt else x )<train_model>
q_id1 = train_transaction[["ID_D1D10", "isFraud"]].groupby("ID_D1D10" ).agg({"isFraud": ["count", "mean"]} ).reset_index()
q_id1.columns = ["ID_D1D10", "isFraud_countD1D10", "isFraud_meanD1D10"]
q_id2 = train_transaction[["ID_D1D10", "isFraud"]].groupby("ID_D1D10" ).agg({"isFraud": ["count", "mean"]} ).reset_index()
q_id2.columns = ["ID_D1D12", "isFraud_countD1D12", "isFraud_meanD1D12"]
def scale_minmax(preds):
return(preds - preds.min())/(preds.max() - preds.min())
 def pred_lgb(X_train, Y_train, X_test, params, num_iterations=1000):
feature_importances = pd.DataFrame()
feature_importances['feature'] = X_train.columns
oof_preds = np.zeros(X_train.shape[0])
 sub_preds = np.zeros(X_test.shape[0])
 dtrain = lgb.Dataset(X_train[:312574], label=Y_train.iloc[:312574])
 dvalid = lgb.Dataset(X_train[-175998:], label=Y_train[-175998:])
 clf = lgb.train(params, dtrain, 5000,
valid_sets=[dtrain, dvalid], valid_names=['train', 'valid'],
verbose_eval=1000, early_stopping_rounds=200)
 feature_importances['fold_0'] = clf.feature_importance()
oof_preds = clf.predict(X_train)
 sub_preds = clf.predict(X_test)
 return oof_preds, sub_preds, feature_importances
def pred_lgb_kfold(X_train, Y_train, X_test, params, nfolds=5):
feature_importances = pd.DataFrame()
feature_importances['feature'] = X_train.columns
oof_preds = np.zeros(X_train.shape[0])
 sub_preds = np.zeros(X_test.shape[0])
 kf = KFold(n_splits=nfolds, shuffle=False, random_state=42)
 for fold,(train_index, test_index)in enumerate(kf.split(X_train, Y_train)) :
dtrain = lgb.Dataset(X_train.iloc[train_index], label=Y_train.iloc[train_index])
 dvalid = lgb.Dataset(X_train.iloc[test_index], label=Y_train.iloc[test_index])
 clf = lgb.train(params, dtrain, 5000,
valid_sets=[dtrain, dvalid], valid_names=['train', 'valid'],
verbose_eval=1000, early_stopping_rounds=200)
 feature_importances['fold_{}'.format(fold + 1)] = clf.feature_importance()
oof_preds[test_index] = clf.predict(X_train.iloc[test_index])
 sub_preds += clf.predict(X_test)/ nfolds
return oof_preds, sub_preds, feature_importances<find_best_model_class>
test_transaction = pd.merge(test_transaction, q_id1, how="left", on="ID_D1D10")
 test_transaction = pd.merge(test_transaction, q_id2, how="left", on="ID_D1D12" )
def pred_lgb_LOGO(X_train, Y_train, X_test, groups, params, undersampling=False):
feature_importances = pd.DataFrame()
feature_importances['feature'] = X_train.columns
oof_preds = np.zeros(X_train.shape[0])
 sub_preds = np.zeros(X_test.shape[0])
 nfolds = groups.nunique()
kf = LeaveOneGroupOut()
for fold,(train_index, test_index)in enumerate(kf.split(X_train, Y_train, groups)) :
if undersampling:
size1 = sum(Y_train.iloc[train_index]==1)
 X_train_us, Y_train_us = make_imbalance(
 X_train.iloc[train_index].fillna(-9999), Y_train.iloc[train_index],
sampling_strategy={0:size1*5, 1:size1}, random_state=42)
 X_train_us = pd.DataFrame(X_train_us, columns=X_train.columns)
 dtrain = lgb.Dataset(X_train_us, label=Y_train_us)
 else:
dtrain = lgb.Dataset(X_train.iloc[train_index], label=Y_train.iloc[train_index])
 dvalid = lgb.Dataset(X_train.iloc[test_index], label=Y_train.iloc[test_index])
 clf = lgb.train(params, dtrain, 5000,
valid_sets=[dtrain, dvalid], valid_names=['train', 'valid'],
verbose_eval=1000, early_stopping_rounds=200)
 feature_importances['fold_{}'.format(fold + 1)] = clf.feature_importance()
oof_preds[test_index] = clf.predict(X_train.iloc[test_index])
 sub_preds += clf.predict(X_test)/ nfolds
return oof_preds, sub_preds, feature_importances<find_best_model_class>
submit = pd.merge(submit, test_transaction, how="left", on="TransactionID" )
def pred_lgb_LOGO_2(X_train, Y_train, X_test, groups, params):
feature_importances = pd.DataFrame()
feature_importances['feature'] = X_train.columns
oof_preds = np.zeros(X_train.shape[0])
 sub_preds = np.zeros(X_test.shape[0])
 nfolds = groups.nunique()
print('groups:', nfolds)
 kf = LeaveOneGroupOut()
pred_count = 0
for fold,(tt_index, out_index)in enumerate(kf.split(X_train, Y_train, groups)) :
train_index = tt_index[tt_index < out_index[0]]
test_index = tt_index[tt_index > out_index[len(out_index)- 1]]
print(f'fold[{fold+1}] - {len(tt_index)}(train:{len(train_index)}, valid:{len(test_index)})')
 if len(train_index)== 0 or len(test_index)== 0:
print('- skip')
 continue
pred_count += 1
dtrain = lgb.Dataset(X_train.iloc[train_index], label=Y_train.iloc[train_index])
 dvalid = lgb.Dataset(X_train.iloc[test_index], label=Y_train.iloc[test_index])
 clf = lgb.train(params, dtrain, 5000,
valid_sets=[dtrain, dvalid], valid_names=['train', 'valid'],
verbose_eval=500, early_stopping_rounds=200)
 feature_importances['fold_{}'.format(pred_count)] = clf.feature_importance()
oof_preds += clf.predict(X_train)
 sub_preds += clf.predict(X_test)
 oof_preds = oof_preds / pred_count
sub_preds = sub_preds / pred_count
return oof_preds, sub_preds, feature_importances<init_hyperparams>
q = "isFraud_countD1D10 > 1 and isFraud_meanD1D10 > 0.7 and ProductCD != 'C'"
%%time
params={
'learning_rate': 0.01,
'objective': 'binary',
'boost_from_average': False,
'is_unbalance': False,
'metric': 'auc',
'num_threads': -1,
'num_leaves': 256,
'max_bin': 256,
'verbose': 1,
'random_state': 42,
'bagging_fraction': 0.85,
'bagging_freq': 1,
'feature_fraction': 0.60
}
oof_preds, sub_preds, feature_importances = pred_lgb_LOGO(X_train, Y_train, X_test, train_group, params)
<concatenate>
submit["isFraud"][submit.query(q ).index] = 1
_=<sort_values>
q = "isFraud_countD1D12 > 1 and isFraud_meanD1D12 == 1 and ProductCD == 'C'"
feature_importances.sort_values(by='average', ascending=False)['feature'].values<save_to_csv>
q = "isFraud_countD1D10 > 5 and isFraud_meanD1D10 == 0"
submission = pd.DataFrame()
submission['TransactionID'] = X_test_id
submission['isFraud'] = sub_preds
submission.loc[uid_test.isin(uid_fraud ).values, 'isFraud'] = 1
submission.to_csv('submission.csv', index=False )<prepare_output>
submit["isFraud"][submit.query(q ).index] = 0
np.mean(sub_preds )<set_options>
submit[["TransactionID", "isFraud"]].to_csv("postprocessed.csv", index=False )