Kaggle competition: IEEE-CIS Fraud Detection
<SOS> metric: AUC Kaggle data source: ieee-cis-fraud-detection<count_values>
warnings.filterwarnings('ignore' )
test_df.Name.str.split('.' ).str[0].str.split(',' ).str[1].value_counts()<feature_engineering>
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
title_dict = {
'Mr': 'Mr',
'Miss': 'Miss',
'Mrs': 'Mrs',
'Master': 'Master',
'Dr': 'Officer',
'Rev': 'Officer',
'Major': 'Officer',
'Col': 'Officer',
'Mlle': 'Miss',
'the Countess': 'Royalty',
'Don': 'Royalty',
'Mme': 'Mrs',
'Jonkheer': 'Royalty',
'Sir': 'Royalty',
'Ms': 'Mrs',
'Lady': 'Royalty',
'Capt': 'Officer',
'Dona': 'Royalty'
}
for df in dataset:
df['Title'] = df.Name.str.split('.' ).str[0].str.split(',' ).str[1].str.strip().map(title_dict )<count_values>
def make_predictions(tr_df, tt_df, features_columns, target, lgb_params, NFOLDS=2):
folds = GroupKFold(n_splits=NFOLDS)
 X,y = tr_df[features_columns], tr_df[target]
P,P_y = tt_df[features_columns], tt_df[target]
split_groups = tr_df['DT_M']
tt_df = tt_df[['TransactionID',target]]
predictions = np.zeros(len(tt_df))
oof = np.zeros(len(tr_df))
for fold_,(trn_idx, val_idx)in enumerate(folds.split(X, y, groups=split_groups)) :
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)
 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
oof_preds = estimator.predict(vl_x)
 oof[val_idx] =(oof_preds - oof_preds.min())/(oof_preds.max() - oof_preds.min())
 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
print('OOF AUC:', metrics.roc_auc_score(y, oof))
if LOCAL_TEST:
print('Holdout AUC:', metrics.roc_auc_score(tt_df[TARGET], tt_df['prediction']))
return tt_df
train_df.Title.value_counts()<feature_engineering>
SEED = 42
seed_everything(SEED)
 LOCAL_TEST = False
TARGET = 'isFraud'
START_DATE = datetime.datetime.strptime('2017-11-30', '%Y-%m-%d' )
train_df['NameLen'] = train_df.Name.str.split().str.len()
test_df['NameLen'] = test_df.Name.str.split().str.len()<count_missing_values>
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.5,
'subsample_freq':1,
'subsample':0.7,
'n_estimators':800,
'max_bin':255,
'verbose':-1,
'seed': SEED,
'early_stopping_rounds':100,
}
train_df.Age.isnull().sum()<groupby>
print('Load Data')
 if LOCAL_TEST:
train_df = pd.read_pickle('.. /input/ieee-fe-for-local-test/train_df.pkl')
 test_df = pd.read_pickle('.. /input/ieee-fe-for-local-test/test_df.pkl')
 else:
train_df = pd.read_pickle('.. /input/ieee-fe-with-some-eda/train_df.pkl')
 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 )
age_pclass_title_map = train_df.groupby(['Pclass', 'Title', 'Sex'] ).median().reset_index() [['Pclass', 'Title', 'Sex', 'Age']]
age_pclass_title_map<feature_engineering>
features_columns = [col for col in list(train_df)if col not in remove_features]
if not LOCAL_TEST:
train_df = reduce_mem_usage(train_df)
 test_df = reduce_mem_usage(test_df )
def get_age(row):
return age_pclass_title_map[(age_pclass_title_map['Pclass']==row['Pclass'])& 
(age_pclass_title_map['Title']==row['Title'])& 
(age_pclass_title_map['Sex']==row['Sex'])]['Age'].values[0]
for df in dataset:
df.Age = df.apply(lambda row: get_age(row)if np.isnan(row['Age'])else row['Age'], axis=1 )<count_missing_values>
if LOCAL_TEST:
lgb_params['learning_rate'] = 0.01
lgb_params['n_estimators'] = 10000
lgb_params['early_stopping_rounds'] = 100
test_predictions = make_predictions(train_df, test_df, features_columns, TARGET, lgb_params, NFOLDS=4)
 else:
lgb_params['learning_rate'] = 0.007
lgb_params['n_estimators'] = 10000
lgb_params['early_stopping_rounds'] = 100
test_predictions = make_predictions(train_df, test_df, features_columns, TARGET, lgb_params, NFOLDS=6 )
<feature_engineering><EOS> 
if not LOCAL_TEST:
test_predictions['isFraud'] = test_predictions['prediction']
test_predictions[['TransactionID','isFraud']].to_csv('submission.csv', index=False )