Kaggle competition: IEEE-CIS Fraud Detection
<SOS> metric: AUC Kaggle data source: ieee-cis-fraud-detection<normalization>
pd.set_option('display.max_rows', 500)
 pd.set_option('display.max_columns', 500)
 pd.set_option('display.width', 1000)
 train_identity = pd.read_csv('/kaggle/input/ieee-fraud-detection/train_identity.csv')
 train_transaction = pd.read_csv('/kaggle/input/ieee-fraud-detection/train_transaction.csv')
 test_identity = pd.read_csv('/kaggle/input/ieee-fraud-detection/test_identity.csv')
 test_transaction = pd.read_csv('/kaggle/input/ieee-fraud-detection/test_transaction.csv' )
scaler = MinMaxScaler()
train_x_new = scaler.fit_transform(train_x_new)
 train_y_new = np.log1p(train_y_new )<normalization>
identity_cols_withNA = 100*train_identity.isnull().sum().sort_values(ascending=False)/train_identity.shape[0]
identity_cols_withNA = identity_cols_withNA[identity_cols_withNA > 0]
print('train_identity has {} columns with null entries.The 10 most empty columns are shown below'.format(identity_cols_withNA.shape[0]))
identity_cols_withNA[:10]
test_x_new_1 = scaler.transform(test_x_new)
 test_x_new_2 = scaler.fit_transform(test_x_new )<split>
transaction_cols_withNA = 100*train_transaction.isnull().sum().sort_values(ascending=False)/train_transaction.shape[0]
transaction_cols_withNA = transaction_cols_withNA[transaction_cols_withNA > 0]
print('train_transaction has {} columns with null entries.The 10 most empty columns are shown below'.format(transaction_cols_withNA.shape[0]))
transaction_cols_withNA[:10]
X_train , X_test , Y_train , Y_test = train_test_split(train_x_new , train_y_new , test_size =.15 , random_state = 65 )<define_variables>
identity_cat_cols = ['DeviceType','DeviceInfo']
for i in range(12,39):
identity_cat_cols.append('id_'+str(i))
transaction_cat_cols = ['ProductCD','addr1','addr2','P_emaildomain','R_emaildomain']
for i in range(1,7):
transaction_cat_cols.append('card'+str(i))
for i in range(1,10):
transaction_cat_cols.append('M'+str(i))
valid_0_error =0
valid_1_error =0<compute_test_metric>
print('Number of unique entries per categorical column(including nulls)in train_identity:
')
 for col in identity_cat_cols:
cat_number = len(train_identity[col].unique())
 print(col+': '+str(cat_number))
def score_diff(valid_0_error , valid_1_error , valid_0_error_new , valid_1_error_new):
if valid_0_error == 0:
print('First Observaton')
 print('Train Error is : ',valid_0_error_new)
 print('Test Error is : ',valid_1_error_new)
 print('Diffence Between Train and Test is : ',(( valid_1_error_new-valid_0_error_new)/valid_0_error_new)*100 ,' %')
 else:
if valid_0_error_new > valid_0_error:
print('Train Error is : ',valid_0_error_new)
 print('Test Error is : ',valid_1_error_new)
 print('Train Error have Gone up by : ',(( valid_0_error_new-valid_0_error)/valid_0_error)*100,'%')
 print('Test Error have Gone up by : ',(( valid_1_error_new-valid_1_error)/valid_1_error)*100,'%')
 print('Diffence Between Train and Test is : ',(( valid_1_error_new-valid_0_error_new)/valid_0_error_new)*100 ,' %')
 print('Earlier Diffence Between Train and Test is : ',(( valid_1_error-valid_0_error)/valid_0_error)*100 ,' %')
 if valid_0_error_new < valid_0_error:
print('Train Error is : ',valid_0_error_new)
 print('Test Error is : ',valid_1_error_new)
 print('Train Error have Down up by : ',(( valid_0_error-valid_0_error_new)/valid_0_error_new)*100,'%')
 print('Test Error have Down up by : ',(( valid_1_error-valid_1_error_new)/valid_1_error_new)*100,'%')
 print('Diffence Between Train and Test is : ',(( valid_1_error_new-valid_0_error_new)/valid_0_error_new)*100 ,' %')
 print('Earlier Diffence Between Train and Test is : ',(( valid_1_error-valid_0_error)/valid_0_error)*100 ,' %')
 if valid_0_error_new == valid_0_error:
print('No Differnce in new Obseravtion')
 print('Train Error is : ',valid_0_error_new)
 print('Test Error is : ',valid_1_error_new)
 print('Diffence Between Train and Test is : ',(( valid_1_error_new-valid_0_error_new)/valid_0_error_new)*100 ,' %')
<create_dataframe>
print('Number of unique entries per categorical column(including nulls)in train_transaction:
')
 for col in transaction_cat_cols:
cat_number = len(train_transaction[col].unique())
 print(col+': '+str(cat_number))
train_set = lgb.Dataset(X_train , label = Y_train)
 val_set = lgb.Dataset(X_test, label = Y_test )<train_model>
def reduce_mem_usage(df,cat_cols):
start_mem = df.memory_usage().sum() / 1024**2
print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))
for col in cat_cols:
df[col] = df[col].astype('category')
 num_cols = list(set(df.columns)- set(cat_cols))
for col in num_cols:
col_type = df[col].dtype
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.uint8 ).min and c_max < np.iinfo(np.uint8 ).max:
df[col] = df[col].astype(np.uint8)
 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.uint16 ).min and c_max < np.iinfo(np.uint16 ).max:
df[col] = df[col].astype(np.uint16)
 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.uint32 ).min and c_max < np.iinfo(np.uint32 ).max:
df[col] = df[col].astype(np.uint32)
 elif c_min > np.iinfo(np.int64 ).min and c_max < np.iinfo(np.int64 ).max:
df[col] = df[col].astype(np.int64)
 elif c_min > np.iinfo(np.uint64 ).min and c_max < np.iinfo(np.uint64 ).max:
df[col] = df[col].astype(np.uint64)
 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
print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))
print('Decreased by {:.1f}%'.format(100 *(start_mem - end_mem)/ start_mem))
return df
%%time
params = {
"objective" : "regression",
"metric" : "mae",
"num_leaves" : 60,
"learning_rate" : 0.01,
"bagging_fraction" : 0.9,
"bagging_seed" : 0,
"num_threads" : 4,
"colsample_bytree" : 0.5,
'lambda_l2':9
}
model = lgb.train(params,
train_set = train_set,
num_boost_round=10000,
early_stopping_rounds=200,
verbose_eval=100,
valid_sets=[train_set,val_set] 
)
<predict_on_test>
print('Reducing memory usage of train_identity
')
 train_identity = reduce_mem_usage(train_identity,identity_cat_cols)
 print('
Reducing memory usage of train_transaction
')
 train_transaction = reduce_mem_usage(train_transaction,transaction_cat_cols)
 train = pd.merge(train_transaction, train_identity, on='TransactionID', how='left')
 train_mem_usage = train.memory_usage(index = True ).sum() /(1024**2)
 print('
Memory usage of train is {:.2f} MB'.format(train_mem_usage))
train.head()
%%time
lgb_pred_test = model.predict(X_test, num_iteration=model.best_iteration)
 lgb_pred_train = model.predict(X_train, num_iteration=model.best_iteration)
 lgb_pred_normal = model.predict(test_x_new_1, num_iteration=model.best_iteration)
 lgb_pred_fit = model.predict(test_x_new_2, num_iteration=model.best_iteration )<compute_test_metric>
cols_to_rename = {}
for i in range(10):
cols_to_rename['id-0'+str(i)] = 'id_0'+str(i)
 for i in range(10,39):
cols_to_rename['id-'+str(i)] = 'id_'+str(i)
 test_identity = test_identity.rename(columns=cols_to_rename)
 print('Reducing memory usage of test_identity
')
 test_identity = reduce_mem_usage(test_identity,identity_cat_cols)
 print('
Reducing memory usage of test_transaction
')
 test_transaction = reduce_mem_usage(test_transaction,transaction_cat_cols)
 test = pd.merge(test_transaction, test_identity, on='TransactionID', how='left')
 test_mem_usage = test.memory_usage(index = True ).sum() /(1024**2)
 print('
Memory usage of test is {:.2f} MB'.format(test_mem_usage))
del train_transaction
del train_identity
del test_identity
del test_transaction
gc.collect()
test.head()
valid_0_error_new = sqrt(mean_squared_error(np.array(Y_train),lgb_pred_train))
valid_1_error_new = sqrt(mean_squared_error(np.array(Y_test),lgb_pred_test))
score_diff(valid_0_error , valid_1_error , valid_0_error_new , valid_1_error_new)
 valid_0_error = valid_0_error_new
valid_1_error = valid_1_error_new<train_model>
cat_cols = transaction_cat_cols + identity_cat_cols
num_cols = list(set(train.drop(['TransactionID','isFraud','TransactionDT'],axis=1 ).columns)- set(cat_cols))
train_copy = train.copy()
test_copy = test.copy()
for col in cat_cols:
le = LabelEncoder()
le.fit(list(train_copy[col].astype(str ).values)+ list(test_copy[col].astype(str ).values))
train_copy[col] = le.transform(list(train_copy[col].astype(str ).values))
test_copy[col] = le.transform(list(test_copy[col].astype(str ).values))
X = train_copy[cat_cols + num_cols]
y = train_copy['isFraud']
X_test = test_copy[cat_cols + num_cols]
del train_copy
del test_copy
gc.collect()
%%time
n_estimators=100
xgb = XGBRegressor(n_estimators=n_estimators,max_depth=4,learning_rate =0.01 , booster = 'gbtree')
 xgb.fit(X_train ,Y_train ,eval_set=[(X_train, Y_train),(X_test, Y_test)] , verbose = False)
 score = xgb.evals_result()
valid_0_error_new = np.amin(score['validation_0']['rmse'])
 valid_1_error_new = np.amin(score['validation_1']['rmse'])
 score_diff(valid_0_error , valid_1_error , valid_0_error_new , valid_1_error_new)
 valid_0_error = valid_0_error_new
valid_1_error = valid_1_error_new<train_model>
params = {'num_leaves': 256,
'min_child_samples': 79,
'objective': 'binary',
'max_depth': 8,
'learning_rate': 0.02,
"boosting_type": "gbdt",
"subsample_freq": 3,
"subsample": 0.9,
"bagging_seed": 11,
"metric": 'auc',
"verbosity": -1,
'reg_alpha': 0.3,
'reg_lambda': 0.3,
'colsample_bytree': 0.9,
}
n_splits = 4
skf = StratifiedKFold(n_splits=n_splits,shuffle=True)
 skf.get_n_splits(X, y)
 scores = []
i = 1
y_pred = np.zeros(len(X_test.iloc[:,0]))
feat_imp = pd.Series(0, index=X.columns)
 for train_index, valid_index in skf.split(X, y):
print('Training the model using fold number {}...'.format(i))
X_train, X_valid = X.iloc[train_index,:], X.iloc[valid_index,:]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
model = lgb.LGBMClassifier(**params, n_estimators=500, n_jobs = -1)
 model.fit(X_train, y_train,eval_set=[(X_train, y_train),(X_valid, y_valid)], eval_metric='auc', early_stopping_rounds=200,verbose=False)
 y_pred_valid = model.predict_proba(X_valid)[:, 1]
y_pred += model.predict_proba(X_test)[:, 1]/n_splits
score = metrics.roc_auc_score(y_valid, y_pred_valid)
 scores.append(score)
 feat_imp += pd.Series(model.feature_importances_, index=X.columns)/n_splits
print('AUC score: {:.3f}'.format(score))
i += 1
print('mean AUC score over the validation sets: {:.3f}
'.format(np.mean(scores)))
 fig,ax = plt.subplots(figsize=(10,10))
_ = feat_imp.nlargest(20 ).sort_values().plot(kind='barh',ax=ax)
 _ = ax.set_xlabel('Average importance(a.u.) ')
 _ = ax.set_ylabel('Features' )
%%time
model = RandomForestRegressor(random_state=65, n_estimators=200, min_samples_split=4)
 result = model.fit(X_train, Y_train )<compute_test_metric>
output = pd.DataFrame({'TransactionID': test.TransactionID, 'isFraud': y_pred})
 output.to_csv('simple_lightgbm_submission.csv', index=False)
 print("Submission successfully saved!" )
model.score(X_test, Y_test )<train_model>
del X
del X_test
del y
del X_train
del X_valid
del y_train
del y_valid
del y_pred
del y_pred_valid
gc.collect()
%%time
n_estimators=3000
xgb = XGBRegressor(n_estimators=n_estimators,max_depth=4,learning_rate =0.01 , booster = 'gbtree')
 xgb.fit(train_x_new , train_y_new ,eval_set=[(X_train, Y_train),(X_test, Y_test)] , verbose = False)
 pred_normal = xgb.predict(test_x_new_1)
 pred_fit = xgb.predict(test_x_new_2 )<predict_on_test>
to_drop_notV = to_drop[:14]
to_drop_V = to_drop[14:]
for col in to_drop_notV:
na_proportion = train[col].isna().sum() /train.shape[0]
if na_proportion >= 0.4:
train = train.drop(col,axis=1)
 test = test.drop(col,axis=1)
 print('Column {} was dropped'.format(col))
train = train.drop(columns = to_drop_V)
 test = test.drop(columns = to_drop_V)
 print('
{} V columns were dropped'.format(len(to_drop_V)))
%%time
model = RandomForestRegressor(random_state=65, n_estimators=n_estimators-2000)
 model.fit(train_x_new , train_y_new)
 rfr_pred_normal = model.predict(test_x_new_1)
 rfr_pred_fit = model.predict(test_x_new_2 )<train_model>
num_cols = list(set(num_cols)- set(['D2','D6','D7'])- set(to_drop_V))
train[cat_cols] = train[cat_cols].astype('str' ).replace('nan','unknown' ).astype('category')
 test[cat_cols] = test[cat_cols].astype('str' ).replace('nan','unknown' ).astype('category')
 train[num_cols] = train[num_cols].fillna(-999)
 test[num_cols] = test[num_cols].fillna(-999 )
%%time
pred_normal = np.expm1(pred_normal)
 pred_fit = np.expm1(pred_fit)
 rfr_pred_normal = np.expm1(rfr_pred_normal)
 rfr_pred_fit = np.expm1(rfr_pred_fit )<choose_model_class>
print('Showing the first 10 unique elements of DeviceInfo(out of {}):
{}'\ 
.format(len(list(train['DeviceInfo'].unique())) ,\
list(train['DeviceInfo'].unique())[:10]))
%%time
NN_model = Sequential()
NN_model.add(Dense(128, kernel_initializer='normal',input_dim = train_x_new.shape[1], activation='relu'))
NN_model.add(Dense(256, kernel_initializer='normal',activation='relu'))
NN_model.add(Dropout(0.3))
NN_model.add(Dense(256, kernel_initializer='normal',activation='relu'))
NN_model.add(Dropout(0.3))
NN_model.add(Dense(256, kernel_initializer='normal',activation='relu'))
NN_model.add(Dropout(0.3))
NN_model.add(Dense(1, kernel_initializer='normal',activation='linear'))
NN_model.compile(loss='mean_absolute_error', optimizer='adam', metrics=['mean_absolute_error'])
 NN_model.summary()
history = NN_model.fit(train_x_new,train_y_new, epochs=50, batch_size=64, verbose=1, validation_split=0.2)
 start_point = 150-100
r = range(start_point,150)
 plt.figure(figsize=(16,8))
plt.plot(history.history['loss'] ,'r' ,label ='Train')
 plt.plot(history.history['val_loss'] , 'g' , label = 'Test')
 plt.legend(fontsize='x-large')
 valid_0_error_new = history.history['loss'][-1]
valid_1_error_new = history.history['val_loss'][-1]
score_diff(valid_0_error , valid_1_error , valid_0_error_new , valid_1_error_new)
 valid_0_error = valid_0_error_new
valid_1_error = valid_1_error_new 
<predict_on_test>
def setDevice(df):
df['DeviceInfo'] = df['DeviceInfo'].str.lower()
df['device_name'] = df['DeviceInfo'].str.split('/', expand=True)[0]
df.loc[df['device_name'].str.contains('sm', na=False), 'device_name'] = 'samsung'
df.loc[df['device_name'].str.contains('samsung', na=False), 'device_name'] = 'samsung'
df.loc[df['device_name'].str.contains('gt-', na=False), 'device_name'] = 'samsung'
df.loc[df['device_name'].str.contains('moto', na=False), 'device_name'] = 'motorola'
df.loc[df['device_name'].str.contains('lg', na=False), 'device_name'] = 'lg'
df.loc[df['device_name'].str.contains('rv:', na=False), 'device_name'] = 'rv'
df.loc[df['device_name'].str.contains('huawei', na=False), 'device_name'] = 'huawei'
df.loc[df['device_name'].str.contains('ale-', na=False), 'device_name'] = 'huawei'
df.loc[df['device_name'].str.contains('-l', na=False), 'device_name'] = 'huawei'
df.loc[df['device_name'].str.contains('blade', na=False), 'device_name'] = 'zte'
df.loc[df['device_name'].str.contains('linux', na=False), 'device_name'] = 'linux'
df.loc[df['device_name'].str.contains('xt', na=False), 'device_name'] = 'sony'
df.loc[df['device_name'].str.contains('htc', na=False), 'device_name'] = 'htc'
df.loc[df['device_name'].str.contains('asus', na=False), 'device_name'] = 'asus'
df.loc[df['device_name'].str.contains('lenovo', na=False), 'device_name'] = 'lenovo'
df.loc[df['device_name'].str.contains('pixel', na=False), 'device_name'] = 'google'
df.loc[df['device_name'].str.contains('redmi', na=False), 'device_name'] = 'xiaomi'
df.loc[df['device_name'].str.contains('windows', na=False), 'device_name'] = 'microsoft'
df.loc[df['device_name'].str.contains('microsoft', na=False), 'device_name'] = 'microsoft'
df.loc[df['device_name'].str.contains('nexus', na=False), 'device_name'] = 'google'
df.loc[df['device_name'].str.contains('ilium', na=False), 'device_name'] = 'lanix'
df.loc[df['device_name'].str.contains('lumia', na=False), 'device_name'] = 'nokia'
df['DeviceInfo'] = df['DeviceInfo'].astype('category')
 df['device_name'] = df['device_name'].astype('category')
 return df
ANN_pred_normal = NN_model.predict(test_x_new_1)
 ANN_pred_fit = NN_model.predict(test_x_new_2)
 ANN_pred_normal = np.expm1(ANN_pred_normal)
 ANN_pred_fit = np.expm1(ANN_pred_fit)
 ANN_pred_fit = ANN_pred_fit.reshape(6493)
 ANN_pred_normal = ANN_pred_normal.reshape(6493 )<save_to_csv>
train['id_31'] = train['id_31'].str.lower().astype('category')
 test['id_31'] = test['id_31'].str.lower().astype('category' )
output = pd.DataFrame({'datetime': test.datetime,'count': pred_normal})
 output.to_csv('xgb_pred_normal.csv', index=False)
 output = pd.DataFrame({'datetime': test.datetime,'count': pred_fit})
 output.to_csv('xgb_pred_fit.csv', index=False)
 output = pd.DataFrame({'datetime': test.datetime,'count': rfr_pred_normal})
 output.to_csv('rfr_pred_normal.csv', index=False)
 output = pd.DataFrame({'datetime': test.datetime,'count': rfr_pred_fit})
 output.to_csv('rfr_pred_fit.csv', index=False)
 output = pd.DataFrame({'datetime': test.datetime,'count': ANN_pred_normal})
 output.to_csv('ANN_pred_normal.csv', index=False)
 output = pd.DataFrame({'datetime': test.datetime,'count': ANN_pred_fit})
 output.to_csv('ANN_pred_fit.csv', index=False)
 output = pd.DataFrame({'datetime': test.datetime,'count': lgb_pred_normal})
 output.to_csv('lgb_pred_normal.csv', index=False)
 output = pd.DataFrame({'datetime': test.datetime,'count': lgb_pred_fit})
 output.to_csv('lgb_pred_fit.csv', index=False )<set_options>
train['id_30'] = train['id_30'].str.lower()
test['id_30'] = test['id_30'].str.lower()
train['id_30_word1'] = train['id_30'].str.split(' ', expand=True)[0].astype('category')
 test['id_30_word1'] = test['id_30'].str.split(' ', expand=True)[0].astype('category')
 train['id_30'] = train['id_30'].astype('category')
 test['id_30'] = test['id_30'].astype('category')
 print('The new column, id_30_word1, has {} unique values'\ 
.format(len(list(train['id_30_word1'].unique()))))
%matplotlib inline<load_from_csv>
train['id_15'] = train['id_15'].str.lower().astype('category')
 test['id_15'] = test['id_15'].str.lower().astype('category' )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<load_from_csv>
def SplitEmailDomain(df):
df['P_emaildomain_prefix'] = df['P_emaildomain'].str.split('.',1, expand=True)[0].astype('category')
 df['P_emaildomain_sufix'] = df['P_emaildomain'].str.split('.',1, expand=True)[1].fillna('unknown' ).astype('category')
 df['R_emaildomain_prefix'] = df['R_emaildomain'].str.split('.',1, expand=True)[0].astype('category')
 df['R_emaildomain_sufix'] = df['R_emaildomain'].str.split('.',1, expand=True)[1].fillna('unknown' ).astype('category')
 return df
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<feature_engineering>
train = SplitEmailDomain(train)
 test = SplitEmailDomain(test)
 train.head()
train['datetime'] = pd.to_datetime(train['datetime'],errors='coerce')
 train['date'] = train['datetime'].apply(lambda x: x.date())
 train['year'] = train['datetime'].apply(lambda x: x.year)
 train['month'] = train['datetime'].apply(lambda x: x.month)
 train['weekday'] = train['datetime'].apply(lambda x: x.weekday())
 train['hour'] = train['datetime'].apply(lambda x: x.hour ).astype('int' )<categorify>
for i in range(1,16):
if i not in [2,6,7]:
train['D'+str(i)] = train['D'+str(i)] - train.TransactionDT/np.float32(24*60*60)
 test['D'+str(i)] = test['D'+str(i)] - test.TransactionDT/np.float32(24*60*60 )
dummies_month = pd.get_dummies(train['month'], prefix= 'month')
 dummies_season = pd.get_dummies(train['season'], prefix= 'season')
 dummies_weather = pd.get_dummies(train['weather'], prefix= 'weather')
 dummies_year = pd.get_dummies(train['year'], prefix= 'year')
 data=pd.concat([train,dummies_month,dummies_season,dummies_weather,dummies_year],axis=1)
 yLabels=data['count']
dropFeatures = ['casual' , 'count' , 'datetime' ,
'registered' , 'date' ,'season',
'weather','month','year']
dataTrain = data.drop(dropFeatures,axis=1 )<import_modules>
cat_cols = cat_cols + ['device_name','id_30_word1','P_emaildomain_prefix','P_emaildomain_sufix',\
'R_emaildomain_prefix','R_emaildomain_sufix']
train_copy = train.copy()
test_copy = test.copy()
for col in cat_cols:
le = LabelEncoder()
le.fit(list(train_copy[col].astype(str ).values)+ list(test_copy[col].astype(str ).values))
train_copy[col] = le.transform(list(train_copy[col].astype(str ).values))
test_copy[col] = le.transform(list(test_copy[col].astype(str ).values))
first_day = train_copy['TransactionDT'].min()
five_months = first_day + 5*30*24*3600
X_train = train_copy.loc[train_copy['TransactionDT'] <= five_months,cat_cols + num_cols]
y_train = train_copy.loc[train_copy['TransactionDT'] <= five_months,'isFraud']
X_valid = train_copy.loc[train_copy['TransactionDT'] > five_months,cat_cols + num_cols]
y_valid = train_copy.loc[train_copy['TransactionDT'] > five_months,'isFraud']
X_test = test_copy[cat_cols + num_cols]
del train_copy
del test_copy
gc.collect()
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_log_error
from math import sqrt
import statsmodels.api as sm<split>
params = {'num_leaves': 256,
'min_child_samples': 79,
'objective': 'binary',
'max_depth': 8,
'learning_rate': 0.02,
"boosting_type": "gbdt",
"subsample_freq": 3,
"subsample": 0.9,
"bagging_seed": 11,
"metric": 'auc',
'reg_alpha': 0.3,
'reg_lambda': 0.3,
'colsample_bytree': 0.9,
}
model = lgb.LGBMClassifier(**params, n_estimators=2000, n_jobs = -1)
 model.fit(X_train, y_train,eval_set=[(X_train, y_train),(X_valid, y_valid)], eval_metric='auc', early_stopping_rounds=100,verbose=50)
 y_pred_valid = model.predict_proba(X_valid)[:, 1]
y_pred = model.predict_proba(X_test)[:, 1]
score = metrics.roc_auc_score(y_valid, y_pred_valid)
 feat_imp = pd.Series(model.feature_importances_, index=X_train.columns)
 print('AUC score: {:.3f}'.format(score))
fig,ax = plt.subplots(figsize=(10,10))
_ = feat_imp.nlargest(20 ).sort_values().plot(kind='barh',ax=ax)
 _ = ax.set_xlabel('Average importance(a.u.) ')
 _ = ax.set_ylabel('Features' )
X_train_ca, X_valid_ca, y_train_ca, y_valid_ca = train_test_split(dataTrain,
train['casual'], test_size=0.3, random_state=42)
 X_train_re, X_valid_re, y_train_re, y_valid_re = train_test_split(dataTrain,
train['registered'], test_size=0.3, random_state=42)
 X_train, X_valid, y_train, y_valid = train_test_split(dataTrain,
train['count'], test_size=0.3, random_state=42 )<train_model>
output = pd.DataFrame({'TransactionID': test.TransactionID, 'isFraud': y_pred})
 output.to_csv('lightgbm_submission.csv', index=False)
 print("Submission successfully saved!" )