Kaggle competition: IEEE-CIS Fraud Detection
<SOS> metric: AUC Kaggle data source: ieee-cis-fraud-detection<save_to_csv>
pd.set_option('display.max_rows', 500)
 pd.set_option('display.max_columns', 500)
 pd.set_option('display.width', 1000)
 warnings.filterwarnings('ignore')
 SEED = 10
y_pred =(testpredictions/folds > 0.4 ).astype(int)
 submission = pd.DataFrame({"Id": testdata.Id.values,
"Response": y_pred})
 submission[['Id', 'Response']].to_csv('xgbsubmission'+str(folds)+'.csv',
index=False)
<import_modules>
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(deep=True ).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:
c_prec = df[col].apply(lambda x: np.finfo(x ).precision ).max()
if c_min > np.finfo(np.float32 ).min and c_max < np.finfo(np.float32 ).max and c_prec == np.finfo(np.float32 ).precision:
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
from catboost import CatBoostClassifier<load_from_csv>
seed_everything(SEED)
 print('Load Data')
 train_df = pd.read_pickle('.. /input/ieee-data-minification/train_transaction.pkl')
 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 )
file_path = "/kaggle/input/avazu-ctr-prediction/train.gz"
data_df = pd.read_csv(file_path, header=0, nrows=10000000 ).sample(frac=1 )<define_variables>
train_df = pd.merge(train_df,train_identity, how = 'left', on = 'TransactionID',validate = "many_to_one")
 test_df = pd.merge(test_df,test_identity, how = 'left', on = 'TransactionID',validate = "many_to_one" )
train_count = int(len(data_df)* 0.9 )<prepare_x_and_y>
train_df.drop(["TransactionID", "TransactionDT"],axis=1, inplace=True)
 test_df.drop(["TransactionDT"],axis=1, inplace=True )
X_train = data_df.iloc[:train_count,2:]
y_train = data_df.iloc[:train_count,1]<prepare_x_and_y>
X = train_df.drop(["isFraud"],axis=1)
 y= train_df["isFraud"]
X_Test = test_df.copy()
X_Test.drop(['TransactionID', 'isFraud'],axis=1,inplace=True)
 del train_df, test_df, train_identity, test_identity
gc.collect()
X_val = data_df.iloc[train_count:,2:]
y_val = data_df.iloc[train_count:,1]<define_variables>
print(f"Before dropna, top missing columns:
{X.isna().sum().sort_values(ascending = False ).head(5)}
")
 thresh = 0.80
X_less_nas = X.dropna(thresh=X.shape[0]*(1-thresh), axis='columns')
 cols_dropped = list(set(X.columns)-set(X_less_nas.columns))
X_Test.drop(cols_dropped, axis=1, inplace=True)
 print(f"After dropna, top missing columns:
{X_less_nas.isna().sum().sort_values(ascending = False ).head(5)}")
 print(f"
No.of cols dropped = {len(set(X.columns)-set(X_less_nas.columns)) }, or {len(set(X.columns)-set(X_less_nas.columns)) /len(X.columns)*100:.2f}% of columns")
 del X ; gc.collect()
cat_features = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]<choose_model_class>
Catfeats = ['ProductCD'] + \
["card"+f"{i+1}" for i in range(6)] + \
["addr"+f"{i+1}" for i in range(2)] + \
["P_emaildomain", "R_emaildomain"] + \
["M"+f"{i+1}" for i in range(9)] + \
["DeviceType", "DeviceInfo"] + \
["id_"+f"{i}" for i in range(12, 39)]
Catfeats = list(set(Catfeats)- set(cols_dropped))
model = CatBoostClassifier(
 iterations=50,
learning_rate=0.5,
task_type='GPU',
loss_function='Logloss',
)<train_model>
X_less_nas.fillna(-10000, inplace=True)
 X_Test.fillna(-10000, inplace=True )
model.fit(
 X_train, y_train,
eval_set=(X_val, y_val),
cat_features=cat_features,
verbose=10,
)<feature_engineering>
X_tr, X_val, y_tr, y_val = train_test_split(X_less_nas, y, test_size=0.2, random_state=SEED,stratify = y)
 cat_params = {
'loss_function': 'Logloss',
'custom_loss':['AUC'],
'logging_level':'Silent',
'task_type' : 'GPU',
'early_stopping_rounds' : 100
}
simple_model = CatBoostClassifier(**cat_params)
 simple_model.fit(
 X_tr, y_tr,
cat_features=Catfeats,
eval_set=(X_val, y_val),plot=True 
);
model.get_feature_importance(prettified=True )<load_from_csv>
simple_model.fit(
 X_less_nas, y,
cat_features=Catfeats,logging_level = 'Silent' 
);
test_file_path = "/kaggle/input/avazu-ctr-prediction/test.gz"
test_df = pd.read_csv(test_file_path, header=0, dtype=str )<predict_on_test>
submission = pd.read_csv('.. /input/ieee-fraud-detection/sample_submission.csv')
 submission['isFraud'] = simple_model.predict_proba(X_Test)[:,1]
submission.to_csv('simple_model_Catboost.csv', index=False )