Kaggle competition: IEEE-CIS Fraud Detection
<SOS> metric: AUC Kaggle data source: ieee-cis-fraud-detection<categorify>
warnings.simplefilter('ignore')
 sns.set()
%matplotlib inline
def Age_group(age):
a = 0
if(age <= 10):
a = 1
elif(age <= 20):
a = 2
elif(age <=40):
a = 3
else:
a = 4
return a
data['Age Group'] = data.Age.map(Age_group)
 test['Age Group'] = test.Age.map(Age_group)
<feature_engineering>
%%time
warnings.simplefilter('ignore')
 files = ['.. /input/ieee-fraud-detection/test_identity.csv',
'.. /input/ieee-fraud-detection/test_transaction.csv',
'.. /input/ieee-fraud-detection/train_identity.csv',
'.. /input/ieee-fraud-detection/train_transaction.csv',
'.. /input/ieee-fraud-detection/sample_submission.csv']
def load_data(file):
return reduce_mem_usage(pd.read_csv(file))
with multiprocessing.Pool() as pool:
test_identity, test_transaction, train_identity, train_transaction, sample_submission = pool.map(load_data, files )
data['Sex'] = data.Sex.apply(lambda x:1 if x=='female' else 2)
 test['Sex'] = test.Sex.apply(lambda x:1 if x=='female' else 2 )<feature_engineering>
train_transaction.loc[train_transaction.card3.isin(train_transaction.card3.value_counts() [train_transaction.card3.value_counts() < 200].index), 'card3'] = "Others"
train_transaction.loc[train_transaction.card5.isin(train_transaction.card5.value_counts() [train_transaction.card5.value_counts() < 300].index), 'card5'] = "Others"
data['Embarked'] = data.Embarked.apply(lambda x:1 if x=='S' else(2 if x=='C' else 3))
test['Embarked'] = test.Embarked.apply(lambda x:1 if x=='S' else(2 if x=='C' else 3))<drop_column>
train_transaction.loc[train_transaction.addr1.isin(train_transaction.addr1.value_counts() [train_transaction.addr1.value_counts() <= 5000 ].index), 'addr1'] = "Others"
train_transaction.loc[train_transaction.addr2.isin(train_transaction.addr2.value_counts() [train_transaction.addr2.value_counts() <= 50 ].index), 'addr2'] = "Others"
data['No_fam_mem'] = data['SibSp'] + data['Parch']
data.drop(['SibSp','Parch', 'Ticket'], axis=1, inplace=True)
 test['No_fam_mem'] = test['SibSp'] + test['Parch']
test.drop(['SibSp','Parch', 'Ticket'], axis=1, inplace=True )<count_values>
train_transaction.loc[train_transaction['P_emaildomain'].isin(['gmail.com', 'gmail']),'P_emaildomain'] = 'Google'
train_transaction.loc[train_transaction['P_emaildomain'].isin(['yahoo.com', 'yahoo.com.mx', 'yahoo.co.uk',
'yahoo.co.jp', 'yahoo.de', 'yahoo.fr',
'yahoo.es']), 'P_emaildomain'] = 'Yahoo Mail'
train_transaction.loc[train_transaction['P_emaildomain'].isin(['hotmail.com','outlook.com','msn.com', 'live.com.mx',
'hotmail.es','hotmail.co.uk', 'hotmail.de',
'outlook.es', 'live.com', 'live.fr',
'hotmail.fr']), 'P_emaildomain'] = 'Microsoft'
train_transaction.loc[train_transaction.P_emaildomain.isin(train_transaction.P_emaildomain\ 
.value_counts() [train_transaction.P_emaildomain.value_counts() <= 500 ]\ 
.index), 'P_emaildomain'] = "Others"
train_transaction.P_emaildomain.fillna("NoInf", inplace=True )
data.No_fam_mem.value_counts()<categorify>
ploting_cnt_amt(train_transaction, 'R_emaildomain' )
def fam_type(fam_size):
a = 0
if(fam_size==0):
a = 1
elif(fam_size<= 5):
a = 2
else:
a = 3
return a
data['Fam size'] = data.No_fam_mem.map(fam_type)
 data.drop('No_fam_mem', axis=1, inplace=True)
 data = data[['PassengerId', 'Pclass', 'Title', 'Sex', 'Age', 'Age Group', 'Fam size', 'Fare', 'Fare Group', 'Embarked', 'Cabin', 'Survived']]
test['Fam size'] = test.No_fam_mem.map(fam_type)
 test.drop('No_fam_mem', axis=1, inplace=True)
 test = test[['PassengerId', 'Pclass', 'Title', 'Sex', 'Age', 'Age Group', 'Fam size', 'Fare', 'Fare Group', 'Embarked', 'Cabin']]<count_values>
train_transaction.loc[train_transaction.C1.isin(train_transaction.C1\ 
.value_counts() [train_transaction.C1.value_counts() <= 400 ]\ 
.index), 'C1'] = "Others"
data.Cabin.value_counts()<count_values>
train_transaction.loc[train_transaction.C2.isin(train_transaction.C2\ 
.value_counts() [train_transaction.C2.value_counts() <= 350 ]\ 
.index), 'C2'] = "Others"
data.Cabin = data.Cabin.map(lambda x: x[0])
 test.Cabin = test.Cabin.map(lambda x: x[0])
 data.Cabin.value_counts()<categorify>
ploting_cnt_amt(train_transaction, 'C2' )
deck_map = {'U':1, 'A':2, 'B':3, 'C':4, 'D':5, 'E':6, 'F':7, 'G':8, 'T':9}
data['Deck'] = data['Cabin']
data.Deck = data.Deck.map(deck_map)
 data.drop('Cabin', axis=1, inplace=True)
 test['Deck'] = test['Cabin']
test.Deck = test.Deck.map(deck_map)
 test.drop('Cabin', axis=1, inplace=True )<count_missing_values>
START_DATE = '2017-12-01'
startdate = datetime.datetime.strptime(START_DATE, "%Y-%m-%d")
 train_transaction["Date"] = train_transaction['TransactionDT'].apply(lambda x:(startdate + datetime.timedelta(seconds=x)))
 train_transaction['_Weekdays'] = train_transaction['Date'].dt.dayofweek
train_transaction['_Hours'] = train_transaction['Date'].dt.hour
train_transaction['_Days'] = train_transaction['Date'].dt.day
test_transaction["Date"] = test_transaction['TransactionDT'].apply(lambda x:(startdate + datetime.timedelta(seconds=x)))
 test_transaction['_Weekdays'] = test_transaction['Date'].dt.dayofweek
test_transaction['_Hours'] = test_transaction['Date'].dt.hour
test_transaction['_Days'] = test_transaction['Date'].dt.day
data.isnull().sum()<count_missing_values>
color_op = ['
'
'
dates_temp = train_transaction.groupby(train_transaction.Date.dt.date)['TransactionAmt'].count().reset_index()
trace = go.Scatter(x=dates_temp['Date'], y=dates_temp.TransactionAmt,
opacity = 0.8, line = dict(color = color_op[7]), name= 'Total Transactions')
 dates_temp_sum = train_transaction.groupby(train_transaction.Date.dt.date)['TransactionAmt'].sum().reset_index()
trace1 = go.Scatter(x=dates_temp_sum.Date, line = dict(color = color_op[1]), name="Total Amount",
y=dates_temp_sum['TransactionAmt'], opacity = 0.8, yaxis='y2')
 layout = dict(
 title= "Total Transactions and Fraud Informations by Date",
xaxis=dict(
 rangeselector=dict(
 buttons=list([
dict(count=1, label='1m', step='month', stepmode='backward'),
dict(count=3, label='3m', step='month', stepmode='backward'),
dict(count=6, label='6m', step='month', stepmode='backward'),
dict(step='all')
 ])
),
rangeslider=dict(visible = True),
type='date'),
yaxis=dict(title='Total Transactions'),
yaxis2=dict(overlaying='y',
anchor='x', side='right',
zeroline=False, showgrid=False,
title='Total Transaction Amount')
)
 fig = dict(data= [trace, trace1,], layout=layout)
 iplot(fig )
data.isnull().sum()<count_missing_values>
color_op = ['
'
'
tmp_amt = train_transaction.groupby([train_transaction.Date.dt.date, 'isFraud'])['TransactionAmt'].sum().reset_index()
tmp_trans = train_transaction.groupby([train_transaction.Date.dt.date, 'isFraud'])['TransactionAmt'].count().reset_index()
tmp_trans_fraud = tmp_trans[tmp_trans['isFraud'] == 1]
tmp_amt_fraud = tmp_amt[tmp_amt['isFraud'] == 1]
dates_temp = train_transaction.groupby(train_transaction.Date.dt.date)['TransactionAmt'].count().reset_index()
trace = go.Scatter(x=tmp_trans_fraud['Date'], y=tmp_trans_fraud.TransactionAmt,
opacity = 0.8, line = dict(color = color_op[1]), name= 'Fraud Transactions')
 trace1 = go.Scatter(x=tmp_amt_fraud.Date, line = dict(color = color_op[7]), name="Fraud Amount",
y=tmp_amt_fraud['TransactionAmt'], opacity = 0.8, yaxis='y2')
 layout = dict(
 title= "FRAUD TRANSACTIONS - Total Transactions and Fraud Informations by Date",
xaxis=dict(
 rangeselector=dict(
 buttons=list([
dict(count=1, label='1m', step='month', stepmode='backward'),
dict(count=3, label='3m', step='month', stepmode='backward'),
dict(count=6, label='6m', step='month', stepmode='backward'),
dict(step='all')
 ])
),
rangeslider=dict(visible = True),
type='date'),
yaxis=dict(title='Total Transactions'),
yaxis2=dict(overlaying='y',
anchor='x', side='right',
zeroline=False, showgrid=False,
title='Total Transaction Amount')
)
 fig = dict(data= [trace, trace1], layout=layout)
 iplot(fig )
test.isnull().sum()<count_missing_values>
def seed_everything(seed=0):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed )
test.isnull().sum()<feature_engineering>
def make_predictions(tr_df, tt_df, features_columns, target, lgb_params, NFOLDS=6):
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
data['class_age'] = data['Pclass']*data['Age']
data['class_title'] = data['Pclass']*data['Title']
data['class_gen'] = data['Pclass']*data['Sex']
data['fam_fare'] = data['Fam size']*data['Fare']
data['em_fare'] = data['Embarked']*data['Fare']
data['title_age'] = data['Title']*data['Age']
test['class_age'] = test['Pclass']*test['Age']
test['class_title'] = test['Pclass']*test['Title']
test['class_gen'] = test['Pclass']*test['Sex']
test['fam_fare'] = test['Fam size']*test['Fare']
test['em_fare'] = test['Embarked']*test['Fare']
test['title_age'] = test['Title']*test['Age']<drop_column>
SEED = 42
seed_everything(SEED)
 LOCAL_TEST = False
TARGET = 'isFraud'
START_DATE = datetime.datetime.strptime('2017-11-30', '%Y-%m-%d' )
data.drop(['Age', 'Fare'], axis=1, inplace=True)
 test.drop(['Age', 'Fare'], axis=1, inplace=True )<normalization>
print('Load Data')
 train_df = pd.read_pickle('.. /input/ieee-data-minification/train_transaction.pkl')
 if LOCAL_TEST:
train_df['DT_M'] = train_df['TransactionDT'].apply(lambda x:(START_DATE + datetime.timedelta(seconds = x)))
 train_df['DT_M'] =(train_df['DT_M'].dt.year-2017)*12 + train_df['DT_M'].dt.month
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)
 train_identity = pd.read_pickle('.. /input/ieee-data-minification/train_identity.pkl')
 test_identity = train_identity[train_identity['TransactionID'].isin(
 test_df['TransactionID'])].reset_index(drop=True)
 train_identity = train_identity[train_identity['TransactionID'].isin(
 train_df['TransactionID'])].reset_index(drop=True)
 del train_df['DT_M'], test_df['DT_M']
else:
test_df = pd.read_pickle('.. /input/ieee-data-minification/test_transaction.pkl')
 train_identity = pd.read_pickle('.. /input/ieee-data-minification/train_identity.pkl')
 test_identity = pd.read_pickle('.. /input/ieee-data-minification/test_identity.pkl')
 base_columns = list(train_df)+ list(train_identity)
 print('Shape control:', train_df.shape, test_df.shape )
scaler = MinMaxScaler()
col_lst = [ 'Pclass', 'Title', 'Age Group', 'Fam size', 'Fare Group', 'Embarked',
'Deck', 'class_age', 'class_title', 'class_gen', 'fam_fare', 'em_fare', 'title_age']
data[col_lst] = scaler.fit_transform(data[col_lst])
 test[col_lst] = scaler.fit_transform(test[col_lst] )<drop_column>
for df in [train_df, test_df]:
df['DT'] = df['TransactionDT'].apply(lambda x:(START_DATE + datetime.timedelta(seconds = x)))
 df['DT_M'] =(df['DT'].dt.year-2017)*12 + df['DT'].dt.month
df['DT_W'] =(df['DT'].dt.year-2017)*52 + df['DT'].dt.weekofyear
df['DT_D'] =(df['DT'].dt.year-2017)*365 + df['DT'].dt.dayofyear
df['DT_hour'] = df['DT'].dt.hour
df['DT_day_week'] = df['DT'].dt.dayofweek
df['DT_day'] = df['DT'].dt.day
df['D9'] = np.where(df['D9'].isna() ,0,1 )
data = data[['Pclass', 'Title', 'Sex', 'Age Group', 'Fam size', 'Fare Group', 'Embarked', 'Deck', 'class_age',
'class_title', 'class_gen', 'fam_fare', 'em_fare', 'title_age', 'Survived']]
test = test[['PassengerId', 'Pclass', 'Title', 'Sex', 'Age Group', 'Fam size', 'Fare Group', 'Embarked', 'Deck', 'class_age',
'class_title', 'class_gen', 'fam_fare', 'em_fare', 'title_age']]<prepare_x_and_y>
i_cols = ['card1']
for col in i_cols:
valid_card = pd.concat([train_df[[col]], test_df[[col]]])
 valid_card = valid_card[col].value_counts()
valid_card = valid_card[valid_card>2]
valid_card = list(valid_card.index)
 train_df[col] = np.where(train_df[col].isin(test_df[col]), train_df[col], np.nan)
 test_df[col] = np.where(test_df[col].isin(train_df[col]), test_df[col], np.nan)
 train_df[col] = np.where(train_df[col].isin(valid_card), train_df[col], np.nan)
 test_df[col] = np.where(test_df[col].isin(valid_card), test_df[col], np.nan )
kf = StratifiedKFold(n_splits=10, shuffle=True, random_state=1)
 test_2 = test.drop('PassengerId', axis=1 ).copy()
target = data['Survived']
train = data.drop('Survived', axis=1 )<split>
i_cols = ['M1','M2','M3','M5','M6','M7','M8','M9']
for df in [train_df, test_df]:
df['M_sum'] = df[i_cols].sum(axis=1 ).astype(np.int8)
 df['M_na'] = df[i_cols].isna().sum(axis=1 ).astype(np.int8 )
X_train, X_test, y_train, y_test = train_test_split(train, target, test_size=0.3, random_state=1)
 X_train.shape, X_test.shape, y_train.shape, y_test.shape<import_modules>
train_df['uid'] = train_df['card1'].astype(str)+'_'+train_df['card2'].astype(str)
 test_df['uid'] = test_df['card1'].astype(str)+'_'+test_df['card2'].astype(str)
 train_df['uid2'] = train_df['uid'].astype(str)+'_'+train_df['card3'].astype(str)+'_'+train_df['card5'].astype(str)
 test_df['uid2'] = test_df['uid'].astype(str)+'_'+test_df['card3'].astype(str)+'_'+test_df['card5'].astype(str)
 train_df['uid3'] = train_df['uid2'].astype(str)+'_'+train_df['addr1'].astype(str)+'_'+train_df['addr2'].astype(str)
 test_df['uid3'] = test_df['uid2'].astype(str)+'_'+test_df['addr1'].astype(str)+'_'+test_df['addr2'].astype(str)
 train_df['TransactionAmt_check'] = np.where(train_df['TransactionAmt'].isin(test_df['TransactionAmt']), 1, 0)
 test_df['TransactionAmt_check'] = np.where(test_df['TransactionAmt'].isin(train_df['TransactionAmt']), 1, 0)
 i_cols = ['card1','card2','card3','card5','uid','uid2','uid3']
for col in i_cols:
for agg_type in ['mean','std']:
new_col_name = col+'_TransactionAmt_'+agg_type
temp_df = pd.concat([train_df[[col, 'TransactionAmt']], test_df[[col,'TransactionAmt']]])
 temp_df = temp_df.groupby([col])['TransactionAmt'].agg([agg_type] ).reset_index().rename(
 columns={agg_type: new_col_name})
 temp_df.index = list(temp_df[col])
 temp_df = temp_df[new_col_name].to_dict()
train_df[new_col_name] = train_df[col].map(temp_df)
 test_df[new_col_name] = test_df[col].map(temp_df)
 train_df['TransactionAmt'] = np.log1p(train_df['TransactionAmt'])
 test_df['TransactionAmt'] = np.log1p(test_df['TransactionAmt'] )
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier<train_model>
p = 'P_emaildomain'
r = 'R_emaildomain'
uknown = 'email_not_provided'
for df in [train_df, test_df]:
df[p] = df[p].fillna(uknown)
 df[r] = df[r].fillna(uknown)
 df['email_check'] = np.where(( df[p]==df[r])&(df[p]!=uknown),1,0)
 df[p+'_prefix'] = df[p].apply(lambda x: x.split('.')[0])
 df[r+'_prefix'] = df[r].apply(lambda x: x.split('.')[0] )
def model_score(model, X_train=X_train, X_test=X_test, y_train=y_train, y_test=y_test):
model.fit(X_train, y_train)
 model_score = model.score(X_test, y_test)*100
return model_score<compute_train_metric>
for df in [train_identity, test_identity]:
df['DeviceInfo'] = df['DeviceInfo'].fillna('unknown_device' ).str.lower()
df['DeviceInfo_device'] = df['DeviceInfo'].apply(lambda x: ''.join([i for i in x if i.isalpha() ]))
df['DeviceInfo_version'] = df['DeviceInfo'].apply(lambda x: ''.join([i for i in x if i.isnumeric() ]))
df['id_30'] = df['id_30'].fillna('unknown_device' ).str.lower()
df['id_30_device'] = df['id_30'].apply(lambda x: ''.join([i for i in x if i.isalpha() ]))
df['id_30_version'] = df['id_30'].apply(lambda x: ''.join([i for i in x if i.isnumeric() ]))
df['id_31'] = df['id_31'].fillna('unknown_device' ).str.lower()
df['id_31_device'] = df['id_31'].apply(lambda x: ''.join([i for i in x if i.isalpha() ]))
def cv_score(model):
cv_score = cross_val_score(model, train, target, cv=kf, scoring='accuracy')
 return cv_score.mean() *100<compute_train_metric>
temp_df = train_df[['TransactionID']]
temp_df = temp_df.merge(train_identity, on=['TransactionID'], how='left')
 del temp_df['TransactionID']
train_df = pd.concat([train_df,temp_df], axis=1)
 temp_df = test_df[['TransactionID']]
temp_df = temp_df.merge(test_identity, on=['TransactionID'], how='left')
 del temp_df['TransactionID']
test_df = pd.concat([test_df,temp_df], axis=1 )
print('Cross val score for LR : ', cv_score(LogisticRegression()))
print('LR Score : ', model_score(LogisticRegression()),'
')
 print('Cross val score for RF : ', cv_score(RandomForestClassifier(n_estimators=100, max_depth=7, min_samples_split=2,
min_samples_leaf=6, max_features='auto', random_state=1)))
 print('RF Score : ', model_score(RandomForestClassifier(n_estimators=100, max_depth=15, min_samples_split=6, min_samples_leaf=6,
max_features='auto', random_state=1)) , '
')
 print('Cross val score for SVC : ', cv_score(SVC(C=30)))
 print('SVC Score : ', model_score(SVC(C=30)) , '
')
 print('Cross val score for KNN : ', cv_score(KNeighborsClassifier(n_neighbors=50)))
 print('KNN Score : ', model_score(KNeighborsClassifier(n_neighbors=50)) , '
')
 print('Cross val score for DT : ', cv_score(DecisionTreeClassifier(max_depth=12, min_samples_split=2, random_state=1)))
 print('DT Score : ', model_score(DecisionTreeClassifier(max_depth=12, min_samples_split=2, random_state=1)) , '
')
<train_model>
i_cols = ['card1','card2','card3','card5',
'C1','C2','C3','C4','C5','C6','C7','C8','C9','C10','C11','C12','C13','C14',
'D1','D2','D3','D4','D5','D6','D7','D8',
'addr1','addr2',
'dist1','dist2',
'P_emaildomain', 'R_emaildomain',
'DeviceInfo','DeviceInfo_device','DeviceInfo_version',
'id_30','id_30_device','id_30_version',
'id_31_device',
'id_33',
'uid','uid2','uid3',
]
for col in i_cols:
temp_df = pd.concat([train_df[[col]], test_df[[col]]])
 fq_encode = temp_df[col].value_counts(dropna=False ).to_dict()
train_df[col+'_fq_enc'] = train_df[col].map(fq_encode)
 test_df[col+'_fq_enc'] = test_df[col].map(fq_encode)
 for col in ['DT_M','DT_W','DT_D']:
temp_df = pd.concat([train_df[[col]], test_df[[col]]])
 fq_encode = temp_df[col].value_counts().to_dict()
train_df[col+'_total'] = train_df[col].map(fq_encode)
 test_df[col+'_total'] = test_df[col].map(fq_encode)
 periods = ['DT_M','DT_W','DT_D']
i_cols = ['uid']
for period in periods:
for col in i_cols:
new_column = col + '_' + period
temp_df = pd.concat([train_df[[col,period]], test_df[[col,period]]])
 temp_df[new_column] = temp_df[col].astype(str)+ '_' +(temp_df[period] ).astype(str)
 fq_encode = temp_df[new_column].value_counts().to_dict()
train_df[new_column] =(train_df[col].astype(str)+ '_' + train_df[period].astype(str)).map(fq_encode)
 test_df[new_column] =(test_df[col].astype(str)+ '_' + test_df[period].astype(str)).map(fq_encode)
 train_df[new_column] /= train_df[period+'_total']
test_df[new_column] /= test_df[period+'_total']
xgbc = XGBClassifier(max_depth=15, min_child_weight=1, n_estimators=500, random_state=42, learning_rate=0.01,
eval_metric=["error", "logloss"])
 xgbc.fit(X_train,y_train, early_stopping_rounds=15, eval_set=[(X_train, y_train),(X_test, y_test)], verbose=True)
<predict_on_test>
for col in ['ProductCD','M4']:
temp_dict = train_df.groupby([col])[TARGET].agg(['mean'] ).reset_index().rename(
 columns={'mean': col+'_target_mean'})
 temp_dict.index = temp_dict[col].values
temp_dict = temp_dict[col+'_target_mean'].to_dict()
train_df[col] = train_df[col].map(temp_dict)
 test_df[col] = test_df[col].map(temp_dict )
y_pred_xgbc = xgbc.predict(X_test )<compute_test_metric>
for col in list(train_df):
if train_df[col].dtype=='O':
print(col)
 train_df[col] = train_df[col].fillna('unseen_before_label')
 test_df[col] = test_df[col].fillna('unseen_before_label')
 train_df[col] = train_df[col].astype(str)
 test_df[col] = test_df[col].astype(str)
 le = LabelEncoder()
le.fit(list(train_df[col])+list(test_df[col]))
train_df[col] = le.transform(train_df[col])
 test_df[col] = le.transform(test_df[col])
 train_df[col] = train_df[col].astype('category')
 test_df[col] = test_df[col].astype('category' )
xgbc_score_train = xgbc.score(X_train, y_train)
 print("Train Prediction Score",xgbc_score_train*100)
 xgbc_score_test = accuracy_score(y_test,y_pred_xgbc)
 print("Test Prediction Score",xgbc_score_test*100 )<predict_on_test>
rm_cols = [
'TransactionID','TransactionDT',
TARGET,
'uid','uid2','uid3',
'bank_type',
'DT','DT_M','DT_W','DT_D',
'DT_hour','DT_day_week','DT_day',
'DT_D_total','DT_W_total','DT_M_total',
'id_30','id_31','id_33',
]
xgbc.fit(train, target)
 prediction_xgbc = xgbc.predict(test_2 )<train_model>
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.85,
'subsample_freq':1,
'subsample':0.85,
'n_estimators':2**9,
'max_bin':255,
'verbose':-1,
'seed': SEED,
'early_stopping_rounds':100,
'reg_alpha':0.3,
'reg_lamdba':0.243
}
model=RandomForestClassifier(n_estimators=100, max_depth=7, min_samples_split=2,min_samples_leaf=6, max_features='auto', random_state=1)
 model.fit(train, target)
 pred_dt = model.predict(test_2 )<train_on_grid>
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.007
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=6 )
<save_to_csv><EOS> 
if not LOCAL_TEST:
test_predictions['isFraud'] = test_predictions['prediction']
test_predictions[['TransactionID','isFraud']].to_csv('submission.csv', index=False )