Kaggle competition: IEEE-CIS Fraud Detection
<SOS> metric: AUC Kaggle data source: ieee-cis-fraud-detection<create_dataframe>
sns.set_style('darkgrid')
 warnings.filterwarnings("ignore")
df_keep = df_trn[to_keep].copy()<train_model>
train_trains = pd.read_csv('/kaggle/input/ieee-fraud-detection/train_transaction.csv', index_col = 'TransactionID')
 train_id = pd.read_csv('/kaggle/input/ieee-fraud-detection/train_identity.csv', index_col = 'TransactionID')
 test_trains = pd.read_csv('/kaggle/input/ieee-fraud-detection/test_transaction.csv', index_col = 'TransactionID')
 test_id = pd.read_csv('/kaggle/input/ieee-fraud-detection/test_identity.csv', index_col = 'TransactionID' )
def get_oob(df):
m = RandomForestRegressor(n_estimators=30, min_samples_leaf=5, max_features=0.6, n_jobs=-1, oob_score=True)
 x, _ = split_vals(df, n_trn)
 m.fit(x, y_train)
 return m.oob_score_<feature_engineering>
train_trains = reduce_mem_usage(train_trains)
 train_id = reduce_mem_usage(train_id)
 test_trains = reduce_mem_usage(test_trains)
 test_id = reduce_mem_usage(test_id )
get_oob(df_keep )<drop_column>
train = pd.merge(train_trains, train_id, on ='TransactionID', how = 'left')
 test = pd.merge(test_trains, test_id, on = 'TransactionID', how = 'left')
 train = train.reset_index()
test = test.reset_index()
print(train.shape)
 print(test.shape )
for c in('totals.transactions', 'totals.totalTransactionRevenue'):
print(c, get_oob(df_keep.drop(c, axis=1)) )<drop_column>
del train_id, train_trains, test_id, test_trains
gc.collect()
to_drop = ['totals.transactions']
get_oob(df_keep.drop(to_drop, axis=1))<split>
print('This might be an imbalanced class problem from what we can see')
 train['isFraud'].value_counts(normalize = True)*100
df_keep.drop(to_drop, axis=1, inplace=True)
 X_train, X_valid = split_vals(df_keep, n_trn )<save_model>
print('THERE ARE {0} MISSING VALUES IN TRAIN'.format(train.isnull().any().sum()))
np.save('tmp/keep_cols.npy', np.array(df_keep.columns))<load_pretrained>
train['isFraud'].value_counts(normalize = True, dropna = False ).values
keep_cols = np.load('tmp/keep_cols.npy')
 df_keep = df_trn[keep_cols]<train_model>
for cols in train.columns[2:]:
if train[cols].value_counts(normalize = True, dropna = False ).values[0]> 0.9:
print(train[cols].value_counts(normalize = True, dropna = False)*100)
 print('-'*90)
 print('-'*90 )
m = RandomForestRegressor(n_estimators=40, min_samples_leaf=3, max_features=0.5, n_jobs=-1, oob_score=True)
 m.fit(X_train, y_train)
 print_score(m )<install_modules>
def label_collector(string):
label = string.split('.')[0]
return label
temp = train['P_emaildomain'].astype(str)
 train['label_encode'] = temp.apply(label_collector )
!pip install treeinterpreter<import_modules>
train = train.drop('label_encode', axis = 1 )
from treeinterpreter import treeinterpreter as ti<split>
cols_drop_train = [cols for cols in train.columns if train[cols].isnull().sum() / train.shape[0] > 0.9]
cols_drop_test = [cols for cols in test.columns if test[cols].isnull().sum() / test.shape[0]> 0.9]
big_top_value_cols = [col for col in train.columns if train[col].value_counts(dropna=False, normalize=True ).values[0] > 0.9]
big_top_value_cols_test = [col for col in test.columns if test[col].value_counts(dropna=False, normalize=True ).values[0] > 0.9]
drop_cols = list(set(cols_drop_train + cols_drop_test + big_top_value_cols + big_top_value_cols_test))
drop_cols.remove('isFraud' )
df_train, df_valid = split_vals(df_raw[df_keep.columns], n_trn )<predict_on_test>
train.drop(drop_cols, axis = 1, inplace = True)
 test.drop(drop_cols, axis = 1, inplace = True )
prediction, bias, contributions = ti.predict(m, row )<sort_values>
del cols_drop_test, cols_drop_train, big_top_value_cols, big_top_value_cols_test, drop_cols
idxs = np.argsort(contributions[0] )<define_variables>
train[['P_emaildomain_1', 'P_emaildomain_2', 'P_emaildomain_3']] = train['P_emaildomain'].str.split('.', expand=True)
 train[['R_emaildomain_1', 'R_emaildomain_2', 'R_emaildomain_3']] = train['R_emaildomain'].str.split('.', expand=True)
 test[['P_emaildomain_1', 'P_emaildomain_2', 'P_emaildomain_3']] = test['P_emaildomain'].str.split('.', expand=True)
 test[['R_emaildomain_1', 'R_emaildomain_2', 'R_emaildomain_3']] = test['R_emaildomain'].str.split('.', expand=True )
<prepare_x_and_y>
def labelencode(train,test):
for col in train.drop(['TransactionID','isFraud','TransactionDT'],axis = 1 ).columns:
if train[col].dtype == 'O' or test[col].dtype == 'O':
le = LabelEncoder()
le.fit(list(train[col].astype(str ).values)+ list(test[col].astype(str ).values))
train[col] = le.transform(list(train[col].astype(str ).values))
test[col] = le.transform(list(test[col].astype(str ).values))
return train,test
df_ext = df_keep.copy()
df_ext['is_valid'] = 1
df_ext.is_valid[:n_trn] = 0
x, y, nas = proc_df(df_ext, 'is_valid' )<train_model>
train, test = labelencode(train, test )
m = RandomForestClassifier(n_estimators=40, min_samples_leaf=3, max_features=0.5, n_jobs=-1, oob_score=True)
 m.fit(x, y);
m.oob_score_<compute_test_metric>
y_test = train['isFraud']
fi = rf_feat_importance(m, x); fi[:10]<define_variables>
cols_drops = ['TransactionID','isFraud','TransactionDT']
train = train.drop(cols_drops, axis = 1 )
feats=['totals.timeOnSite']<drop_column>
test = test.drop(['TransactionID','TransactionDT'], axis = 1 )
x.drop(feats, axis=1, inplace=True )<train_model>
train = train.fillna(-999)
 test = test.fillna(-999 )
m = RandomForestClassifier(n_estimators=40, min_samples_leaf=3, max_features=0.5, n_jobs=-1, oob_score=True)
 m.fit(x, y);
m.oob_score_<compute_test_metric>
train_m, val_m_train, val1, val2 = train_test_split(train,y_test, test_size = 0.3, random_state = 10, stratify = y_test)
 train_m_index = train_m.index
val_m_index = val_m_train.index
val1_index = val1.index
val2_index = val2.index
fi = rf_feat_importance(m, x); fi[:10]<set_options>
def objective(num_leaves,min_child_weight,feature_fraction,bagging_fraction,
max_depth,learning_rate,reg_alpha,reg_lambda,min_data_in_leaf):
global train_m
global val_m
global y_test
global train_m_index
global val_m_index
global val1,val2, val1_index, val2_index
num_leaves = int(num_leaves)
 max_depth = int(max_depth)
 min_data_in_leaf = int(min_data_in_leaf)
 assert type(num_leaves)== int
assert type(min_data_in_leaf)== int
assert type(max_depth)== int
params = {'num_leaves': num_leaves,
'min_child_weight': min_child_weight,
'feature_fraction': feature_fraction,
'bagging_fraction': bagging_fraction,
'min_data_in_leaf': min_data_in_leaf,
'objective': 'binary',
'max_depth': max_depth,
'learning_rate': learning_rate,
"boosting_type": "gbdt",
"bagging_seed": 11,
"metric": 'auc',
"verbosity": -1,
'reg_alpha': reg_alpha,
'reg_lambda': reg_lambda,
'random_state':42,
}
oof = np.zeros(len(train_m))
early_stopping_rounds = 50
xgtrain = lgb.Dataset(train_m, label=val1[val1_index])
 xgvalid = lgb.Dataset(val_m_train, label=val2[val2_index])
 num_boost_round = 200
model_lgb = lgb.train(params, xgtrain , valid_sets = [xgtrain, xgvalid], num_boost_round = num_boost_round,
early_stopping_rounds = early_stopping_rounds, verbose_eval = 0)
 score = roc_auc_score(val2, model_lgb.predict(val_m_train))
return score
set_rf_samples(50000 )<define_variables>
bound_lgb = {'num_leaves':(70,600),
'min_child_weight':(0.001, 0.07),
'feature_fraction':(0.1,0.9),
'bagging_fraction':(0.1,0.9),
'max_depth':(-1,50),
'learning_rate':(0.2,0.9),
'reg_alpha':(0.3,0.9),
'reg_lambda':(0.3,0.9),
'min_data_in_leaf':(50,300)
 }
feats=['totals.timeOnSite', 'geoNetwork.country', 'totals.hits', 'totals.pageviews']<train_model>
LGB_BO = BayesianOptimization(objective, bound_lgb, random_state=42 )
X_train, X_valid = split_vals(df_keep, n_trn)
 m = RandomForestRegressor(n_estimators=40, min_samples_leaf=3, max_features=0.5, n_jobs=-1, oob_score=True)
 m.fit(X_train, y_train)
 print_score(m )<train_model>
init_points = 10
n_iter = 15
print('-' * 130)
 with warnings.catch_warnings() :
warnings.filterwarnings('ignore')
 LGB_BO.maximize(init_points=init_points, n_iter=n_iter,acq='ucb', xi=0.0, alpha=1e-5 )
for f in feats:
df_subs = df_keep.drop(f, axis=1)
 X_train, X_valid = split_vals(df_subs, n_trn)
 m = RandomForestRegressor(n_estimators=40, min_samples_leaf=3, max_features=0.5, n_jobs=-1, oob_score=True)
 m.fit(X_train, y_train)
 print(f)
 print_score(m )<train_model>
params = {'num_leaves': 491,
'min_child_weight': 0.03454472573214212,
'feature_fraction': 0.3797454081646243,
'bagging_fraction': 0.4181193142567742,
'min_data_in_leaf': 106,
'objective': 'binary',
'max_depth': -1,
'learning_rate': 0.006883242363721497,
"boosting_type": "gbdt",
"bagging_seed": 11,
"metric": 'auc',
"verbosity": -1,
'reg_alpha': 0.3899927210061127,
'reg_lambda': 0.6485237330340494,
'random_state': 47,
}
df_subs = df_keep.drop(['geoNetwork.country'], axis=1)
 X_train, X_valid = split_vals(df_subs, n_trn)
 m = RandomForestRegressor(n_estimators=40, min_samples_leaf=3, max_features=0.5, n_jobs=-1, oob_score=True)
 m.fit(X_train, y_train)
 print_score(m )<save_model>
d_train = lgb.Dataset(train, label=y_test )
np.save('tmp/subs_cols.npy', np.array(df_subs.columns))<train_model>
clf = lgb.train(params, d_train, verbose_eval=False, num_boost_round = 1000 )
m = RandomForestRegressor(n_estimators=160, max_features=0.5, n_jobs=-1, oob_score=True)
 %time m.fit(X_train, y_train)
 print_score(m )<drop_column>
predict = clf.predict(test )
X_test = X_test.drop(set(df_raw.columns ).difference(set(df_subs.columns)) - {'totals.transactionRevenue'}, axis=1);<predict_on_test>
submitss = pd.read_csv('/kaggle/input/ieee-fraud-detection/sample_submission.csv' )
predictions = m.predict(X_test )<create_dataframe>
submission = pd.DataFrame({
"TransactionID": submitss['TransactionID'],
"isFraud": predict
})
 submission.TransactionID = submission.TransactionID.astype(int)
 submission.to_csv("submit.csv", index=False )