Kaggle competition: IEEE-CIS Fraud Detection
<SOS> metric: AUC Kaggle data source: ieee-cis-fraud-detection<define_search_space>
%matplotlib inline
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
pretrained_settings = {
'se_resnext101_32x4d': {
'imagenet': {
'url': 'http://data.lip6.fr/cadene/pretrainedmodels/se_resnext101_32x4d-3b2fe3d8.pth',
'input_space': 'RGB',
'input_size': [3, 224, 224],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
}
class SEModule(nn.Module):
def __init__(self, channels, reduction):
super(SEModule, self ).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
 self.fc1 = nn.Conv2d(channels, channels // reduction, kernel_size=1,
padding=0)
 self.relu = nn.ReLU(inplace=True)
 self.fc2 = nn.Conv2d(channels // reduction, channels, kernel_size=1,
padding=0)
 self.sigmoid = nn.Sigmoid()
def forward(self, x):
module_input = x
x = self.avg_pool(x)
 x = self.fc1(x)
 x = self.relu(x)
 x = self.fc2(x)
 x = self.sigmoid(x)
 return module_input * x
class Bottleneck(nn.Module):

 def forward(self, x):
residual = x
out = self.conv1(x)
 out = self.bn1(out)
 out = self.relu(out)
 out = self.conv2(out)
 out = self.bn2(out)
 out = self.relu(out)
 out = self.conv3(out)
 out = self.bn3(out)
 if self.downsample is not None:
residual = self.downsample(x)
 out = self.se_module(out)+ residual
out = self.relu(out)
 return out
class SEBottleneck(Bottleneck):

 expansion = 4
def __init__(self, inplanes, planes, groups, reduction, stride=1,
downsample=None):
super(SEBottleneck, self ).__init__()
self.conv1 = nn.Conv2d(inplanes, planes * 2, kernel_size=1, bias=False)
 self.bn1 = nn.BatchNorm2d(planes * 2)
 self.conv2 = nn.Conv2d(planes * 2, planes * 4, kernel_size=3,
stride=stride, padding=1, groups=groups,
bias=False)
 self.bn2 = nn.BatchNorm2d(planes * 4)
 self.conv3 = nn.Conv2d(planes * 4, planes * 4, kernel_size=1,
bias=False)
 self.bn3 = nn.BatchNorm2d(planes * 4)
 self.relu = nn.ReLU(inplace=True)
 self.se_module = SEModule(planes * 4, reduction=reduction)
 self.downsample = downsample
self.stride = stride
class SEResNetBottleneck(Bottleneck):

 expansion = 4
def __init__(self, inplanes, planes, groups, reduction, stride=1,
downsample=None):
super(SEResNetBottleneck, self ).__init__()
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False,
stride=stride)
 self.bn1 = nn.BatchNorm2d(planes)
 self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, padding=1,
groups=groups, bias=False)
 self.bn2 = nn.BatchNorm2d(planes)
 self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
 self.bn3 = nn.BatchNorm2d(planes * 4)
 self.relu = nn.ReLU(inplace=True)
 self.se_module = SEModule(planes * 4, reduction=reduction)
 self.downsample = downsample
self.stride = stride
class SEResNeXtBottleneck(Bottleneck):

 expansion = 4
def __init__(self, inplanes, planes, groups, reduction, stride=1,
downsample=None, base_width=4):
super(SEResNeXtBottleneck, self ).__init__()
width = math.floor(planes *(base_width / 64)) * groups
self.conv1 = nn.Conv2d(inplanes, width, kernel_size=1, bias=False,
stride=1)
 self.bn1 = nn.BatchNorm2d(width)
 self.conv2 = nn.Conv2d(width, width, kernel_size=3, stride=stride,
padding=1, groups=groups, bias=False)
 self.bn2 = nn.BatchNorm2d(width)
 self.conv3 = nn.Conv2d(width, planes * 4, kernel_size=1, bias=False)
 self.bn3 = nn.BatchNorm2d(planes * 4)
 self.relu = nn.ReLU(inplace=True)
 self.se_module = SEModule(planes * 4, reduction=reduction)
 self.downsample = downsample
self.stride = stride
class SENet(nn.Module):
def __init__(self, block, layers, groups, reduction, dropout_p=0.2,
inplanes=128, input_3x3=True, downsample_kernel_size=3,
downsample_padding=1, num_classes=1000):

 super(SENet, self ).__init__()
self.inplanes = inplanes
if input_3x3:
layer0_modules = [ 
('conv1', nn.Conv2d(3, 64, 3, stride=2, padding=1,
bias=False)) , 
('bn1', nn.BatchNorm2d(64)) , 
('relu1', nn.ReLU(inplace=True)) , 
('conv2', nn.Conv2d(64, 64, 3, stride=1, padding=1,
bias=False)) , 
('bn2', nn.BatchNorm2d(64)) , 
('relu2', nn.ReLU(inplace=True)) , 
('conv3', nn.Conv2d(64, inplanes, 3, stride=1, padding=1,
bias=False)) , 
('bn3', nn.BatchNorm2d(inplanes)) , 
('relu3', nn.ReLU(inplace=True)) ,
]
else:
layer0_modules = [ 
('conv1', nn.Conv2d(3, inplanes, kernel_size=7, stride=2,
padding=3, bias=False)) , 
('bn1', nn.BatchNorm2d(inplanes)) , 
('relu1', nn.ReLU(inplace=True)) ,
]
layer0_modules.append(( 'pool', nn.MaxPool2d(3, stride=2,
ceil_mode=True)))
 self.layer0 = nn.Sequential(OrderedDict(layer0_modules))
self.layer1 = self._make_layer(
 block,
planes=64,
blocks=layers[0],
groups=groups,
reduction=reduction,
downsample_kernel_size=1,
downsample_padding=0 
)
 self.layer2 = self._make_layer(
 block,
planes=128,
blocks=layers[1],
stride=2,
groups=groups,
reduction=reduction,
downsample_kernel_size=downsample_kernel_size,
downsample_padding=downsample_padding 
)
 self.layer3 = self._make_layer(
 block,
planes=256,
blocks=layers[2],
stride=2,
groups=groups,
reduction=reduction,
downsample_kernel_size=downsample_kernel_size,
downsample_padding=downsample_padding 
)
 self.layer4 = self._make_layer(
 block,
planes=512,
blocks=layers[3],
stride=2,
groups=groups,
reduction=reduction,
downsample_kernel_size=downsample_kernel_size,
downsample_padding=downsample_padding 
)
 self.avg_pool = nn.AvgPool2d(7, stride=1)
 self.dropout = nn.Dropout(dropout_p)if dropout_p is not None else None
self.last_linear = nn.Linear(512 * block.expansion, num_classes)
 def _make_layer(self, block, planes, blocks, groups, reduction, stride=1,
downsample_kernel_size=1, downsample_padding=0):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
 nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=downsample_kernel_size, stride=stride,
padding=downsample_padding, bias=False),
nn.BatchNorm2d(planes * block.expansion), 
)
 layers = []
layers.append(block(self.inplanes, planes, groups, reduction, stride,
downsample))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.inplanes, planes, groups, reduction))
return nn.Sequential(*layers)
 def features(self, x):
x = self.layer0(x)
 x = self.layer1(x)
 x = self.layer2(x)
 x = self.layer3(x)
 x = self.layer4(x)
 return x
def logits(self, x):
x = self.avg_pool(x)
 if self.dropout is not None:
x = self.dropout(x)
 x = x.view(x.size(0), -1)
 x = self.last_linear(x)
 return x
def forward(self, x):
x = self.features(x)
 x = self.logits(x)
 return x
def initialize_pretrained_model(model, num_classes, settings):
assert num_classes == settings['num_classes'], \
'num_classes should be {}, but is {}'.format(
 settings['num_classes'], num_classes)
 model.load_state_dict(model_zoo.load_url(settings['url']))
model.input_space = settings['input_space']
model.input_size = settings['input_size']
model.input_range = settings['input_range']
model.mean = settings['mean']
model.std = settings['std']
def se_resnext101(num_classes=1000, pretrained='imagenet'):
model = SENet(SEResNeXtBottleneck, [3, 4, 23, 3], groups=32, reduction=16,
dropout_p=None, inplanes=64, input_3x3=False,
downsample_kernel_size=1, downsample_padding=0,
num_classes=num_classes)
 if pretrained is not None:
settings = pretrained_settings['se_resnext101_32x4d'][pretrained]
initialize_pretrained_model(model, num_classes, settings)
 return model<find_best_params>
train_transaction = pd.read_csv('.. /input/ieee-fraud-detection/train_transaction.csv', index_col='TransactionID')
 train_identity = pd.read_csv('.. /input/ieee-fraud-detection/train_identity.csv', index_col='TransactionID')
 train = train_transaction.merge(train_identity, how='left', left_index=True, right_index=True)
 del train_transaction, train_identity
test_transaction = pd.read_csv('.. /input/ieee-fraud-detection/test_transaction.csv', index_col='TransactionID')
 test_identity = pd.read_csv('.. /input/ieee-fraud-detection/test_identity.csv', index_col='TransactionID')
 test = test_transaction.merge(test_identity, how='left', left_index=True, right_index=True)
 del test_transaction, test_identity
model = se_resnext101()<choose_model_class>
na_columns = train.isna().sum()
print(na_columns[na_columns>0]/train.shape[0] )
model.last_linear = nn.Linear(model.last_linear.in_features, NCLASSES )<choose_model_class>
one_value_cols = [col for col in train.columns if train[col].nunique() <= 1]
one_value_cols_test = [col for col in test.columns if test[col].nunique() <= 1]
one_value_cols == one_value_cols_test
criterion = nn.BCEWithLogitsLoss()
optimizer = optim.Adam(model.parameters() , lr=0.0005)
 scheduler = lr_scheduler.StepLR(optimizer, step_size=3, gamma=0.1 )<find_best_params>
def datetime_trans(data,start_date='2017-11-30'):
startdate=datetime.datetime.strptime(start_date,"%Y-%m-%d")
 data['TransactionDT']=data['TransactionDT'].fillna(data['TransactionDT'].mean())
 data['date']=data['TransactionDT'].apply(lambda x : datetime.timedelta(seconds=x)+startdate)
 data['weekday']=data['date'].apply(lambda x :x.weekday())
 data['month']=(data['date'].dt.year-2017)*12+train['date'].dt.month
data['hour']=data['date'].apply(lambda x :x.hour)
 data['day']=(data['date'].dt.year-2017)*365+train['date'].dt.dayofyear
data['year_weekday']=data['date'].apply(lambda x : str(x.year)+'_'+str(x.weekday()))
data['weekday_hour']=data['date'].apply(lambda x :str(x.weekday())+'_'+str(x.hour))
date_col=['weekday','month','day','hour','year_weekday','weekday_hour']
datetime_trans(train)
 datetime_trans(test )
n_epochs = 13
for epoch in range(1, n_epochs+1):
train_loss = []
for batch_i,(data, target)in tqdm(enumerate(train_loader), total = len(train_loader)) :
data, target = data.cuda() , target.cuda()
optimizer.zero_grad()
output = model(data)
 loss = criterion(output, target.float())
 train_loss.append(loss.item())
 loss.backward()
optimizer.step()
scheduler.step()
print(f'Epoch {epoch}, train loss: {np.mean(train_loss):.4f}' )<save_to_csv>
def transaction_amount_details(data):
data['TransactionAmt_to_mean_card1'] = data['TransactionAmt'] / data.groupby(['card1'])['TransactionAmt'].transform('mean')
 data['TransactionAmt_to_mean_card4'] = data['TransactionAmt'] / data.groupby(['card4'])['TransactionAmt'].transform('mean')
 data['TransactionAmt_to_std_card1'] = data['TransactionAmt'] / data.groupby(['card1'])['TransactionAmt'].transform('std')
 data['TransactionAmt_to_std_card4'] = data['TransactionAmt'] / data.groupby(['card4'])['TransactionAmt'].transform('std')
 transaction_amount_details(train)
 transaction_amount_details(test )
sub = pd.read_csv('.. /input/sample_submission.csv')
 model.eval()
for(data, target, name)in tqdm(test_loader):
data = data.cuda()
output = model(data)
 output = output.cpu().detach().numpy()
for i,(e, n)in enumerate(list(zip(output, name))):
sub.loc[sub['Image'] == n, 'Id'] = ' '.join(label_encoder.inverse_transform(e.argsort() [-5:][::-1]))
sub.to_csv('submission.csv', index=False)
<set_options>
def email_details(data):
data[['P_emaildomain_1', 'P_emaildomain_2', 'P_emaildomain_3']] = data['P_emaildomain'].str.split('.', expand=True)
 data[['R_emaildomain_1', 'R_emaildomain_2', 'R_emaildomain_3']] = data['R_emaildomain'].str.split('.', expand=True)
 email_details(train)
 email_details(test)
warnings.filterwarnings('ignore')
 sns.set_style('darkgrid')
 pd.options.display.float_format = '{:,.3f}'.format
print(os.listdir(".. /input"))<define_variables>
def id_and_address(data):
data['uid'] = data['card1'].astype(str)+'_'+data['card2'].astype(str)
 data['uid2'] = data['uid'].astype(str)+'_'+data['card3'].astype(str)+'_'+data['card5'].astype(str)
 data['uid3'] = data['uid2'].astype(str)+'_'+data['addr1'].astype(str)+'_'+data['addr2'].astype(str)
 data['uid4'] = data['addr1'].astype(str)+'_'+data['addr2'].astype(str)
 data['D9'] = np.where(data['D9'].isna() ,0,1)
 id_and_address(train)
 id_and_address(test )
def seed_everything(seed=0):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 seed_everything(42 )<load_from_csv>
agg_cols = ['card1','card2','card3','card5','uid','uid2','uid3','uid4']
def add_agg_col(col_prefix,agg_col,col_suffix='TransactionAmt'):
if isinstance(agg_col,list):
temp_df=pd.concat([train[[col_prefix,col_suffix]],test[[col_prefix,col_suffix]]])
 temp_df=temp_df.groupby(col_prefix)[col_suffix].agg(agg_col)
 for c in agg_col:
new_col=col_prefix+'_'+c+'_'+col_suffix
train[new_col]=train[col_prefix].map(temp_df[c])
 test[new_col]=test[col_prefix].map(temp_df[c])
 else:
raise TypeError('agg_col must be List')
 for i in agg_cols:
add_agg_col(i,['mean','std'])
 print(f'{i} for ['mean','std'] aggregate is done!' )
%%time
train_id = pd.read_csv('.. /input/train_identity.csv')
 train_trn = pd.read_csv('.. /input/train_transaction.csv')
 test_id = pd.read_csv('.. /input/test_identity.csv')
 test_trn = pd.read_csv('.. /input/test_transaction.csv')
 print(train_id.shape, test_id.shape)
 print(train_trn.shape, test_trn.shape )<merge>
def id02(data):
data['id_02_to_mean_card1'] = data['id_02'] / data.groupby(['card1'])['id_02'].transform('mean')
 data['id_02_to_mean_card4'] = data['id_02'] / data.groupby(['card4'])['id_02'].transform('mean')
 data['id_02_to_std_card1'] = data['id_02'] / data.groupby(['card1'])['id_02'].transform('std')
 data['id_02_to_std_card4'] = data['id_02'] / data.groupby(['card4'])['id_02'].transform('std')
 id02(train)
 id02(test )
id_cols = list(train_id.columns.values)
 trn_cols = list(train_trn.drop('isFraud', axis=1 ).columns.values)
 X_train = pd.merge(train_trn[trn_cols + ['isFraud']], train_id[id_cols], how='left')
 X_train = reduce_mem_usage(X_train)
 X_test = pd.merge(test_trn[trn_cols], test_id[id_cols], how='left')
 X_test = reduce_mem_usage(X_test)
 X_train_id = X_train.pop('TransactionID')
 X_test_id = X_test.pop('TransactionID')
 del train_id,train_trn,test_id,test_trn
all_data = X_train.append(X_test, sort=False ).reset_index(drop=True )<feature_engineering>
def D15(data):
data['D15_to_mean_card1'] = data['D15'] / data.groupby(['card1'])['D15'].transform('mean')
 data['D15_to_mean_card4'] = data['D15'] / data.groupby(['card4'])['D15'].transform('mean')
 data['D15_to_std_card1'] = data['D15'] / data.groupby(['card1'])['D15'].transform('std')
 data['D15_to_std_card4'] = data['D15'] / data.groupby(['card4'])['D15'].transform('std')
 data['D15_to_mean_addr1'] = data['D15'] / data.groupby(['addr1'])['D15'].transform('mean')
 data['D15_to_mean_addr2'] = data['D15'] / data.groupby(['addr2'])['D15'].transform('mean')
 data['D15_to_std_addr1'] = data['D15'] / data.groupby(['addr1'])['D15'].transform('std')
 data['D15_to_std_addr2'] = data['D15'] / data.groupby(['addr2'])['D15'].transform('std')
 D15(train)
 D15(test )
def encode_loop(df, col, drop=True):
df[col + '_cos'] = np.cos(2 * np.pi * df[col] / df[col].max())
 df[col + '_sin'] = np.sin(2 * np.pi * df[col] / df[col].max())
 if drop:
df.drop(col, axis=1, inplace=True)
 return df<define_variables>
def device(data):
data['device_name'] = data['DeviceInfo'].str.split('/', expand=True)[0]
data['device_version'] = data['DeviceInfo'].str.split('/', expand=True)[1]
device(train)
 device(test )
ccols = [f'C{i}' for i in range(1,15)]
dcols = [f'D{i}' for i in range(1,16)]
mcols = ['M1','M2','M3','M5','M6','M7','M8','M9']<feature_engineering>
def browser_OS(data):
data['OS_id_30'] = data['id_30'].str.split(' ', expand=True)[0]
data['version_id_30'] = data['id_30'].str.split(' ', expand=True)[1]
data['browser_id_31'] = data['id_31'].str.split(' ', expand=True)[0]
data['version_id_31'] = data['id_31'].str.split(' ', expand=True)[1]
browser_OS(train)
 browser_OS(test )
all_data['_log_dist_1_2'] = np.log1p(np.where(all_data['dist1'].isna() , all_data['dist2'], all_data['dist1']))<feature_engineering>
freq_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','DeviceType',
'id_30','id_33',
'uid','uid2','uid3','uid4'
]+date_col
def set_freq_col(train,test,col):
prefix='_fq'
temp_df=pd.concat([train[[col]],test[[col]]])
 fq=temp_df[col].value_counts(dropna=False)
 train[col+prefix]=train[col].map(fq)
 test[col+prefix]=test[col].map(fq)
 for c in freq_cols:
set_freq_col(train,test,c)
 periods = ['month','year_weekday','weekday_hour']
uids = ['uid','uid2','uid3','uid4']
def set_uid_period(train,test,periods,uids):
for period in periods:
for col in uids:
new_column = col + '_' + period
temp_df = pd.concat([train[[col,period]], test[[col,period]]])
 temp_df[new_column] = temp_df[col].astype(str)+ '_' +(temp_df[period] ).astype(str)
 fq_encode = temp_df[new_column].value_counts()
train[new_column] =(train[col].astype(str)+ '_' + train[period].astype(str)).map(fq_encode)
 test[new_column] =(test[col].astype(str)+ '_' + test[period].astype(str)).map(fq_encode)
 train[new_column] /= train[period+'_fq']
test[new_column] /= test[period+'_fq']
set_uid_period(train,test,periods,uids )
START_DATE = '2017-11-30'
startdate = datetime.datetime.strptime(START_DATE, "%Y-%m-%d")
 trandate = all_data['TransactionDT'].apply(lambda x:(startdate + datetime.timedelta(seconds=x)))
 all_data['_days'] = all_data['TransactionDT'] //(24*60*60)
 all_data['_hour'] = trandate.dt.hour
all_data = encode_loop(all_data, '_hour')
 all_data['_weekday__hour'] = trandate.dt.dayofweek.astype(str)+ '_' + trandate.dt.hour.astype(str )<data_type_conversions>
train=train.replace([np.inf,-np.inf],np.nan)
 test=test.replace([np.inf,-np.inf],np.nan )
all_data['_P_emaildomain__ProductCD'] = all_data['P_emaildomain'] + '__' + all_data['ProductCD']
all_data['_card3__card5'] = all_data['card3'].astype(str)+ '__' + all_data['card5'].astype(str )<data_type_conversions>
tr_na_count=train.isnull().sum() /len(train)
 tr_drop_cols=[c for c in train.columns if tr_na_count[c]>0.70]
tr_big_cols=[c for c in train.columns if train[c].value_counts(normalize=True,dropna=False ).values[0]>0.85]
te_na_count=test.isnull().sum() /len(test)
 te_drop_cols=[c for c in test.columns if te_na_count[c]>0.70]
te_big_cols=[c for c in test.columns if test[c].value_counts(normalize=True,dropna=False ).values[0]>0.85]
drop_cols=list(set(tr_drop_cols+tr_big_cols+te_drop_cols+te_big_cols))
drop_cols.remove('isFraud')
 print(len(drop_cols))
response=train['isFraud']
train.drop(columns=drop_cols+['isFraud'],inplace=True)
 test.drop(columns=drop_cols,inplace=True )
all_data['_uid1'] =(all_data['_days'] - all_data['D1'] ).astype(str)+ '__' + all_data['P_emaildomain'].astype(str)
 all_data['_uid2'] = all_data['card1'].astype(str)+ '__' + all_data['addr1'].astype(str)+ '__' + all_data['_uid1']
group_key = ['_uid2']
all_data = all_data.assign(
 _day_lag_uid2 = all_data['TransactionDT'] - all_data.groupby(group_key)['TransactionDT'].shift(1)
 ,_amount_lag_pct_uid2 = np.abs(all_data.groupby(group_key)['TransactionAmt'].pct_change())
)
 _=<feature_engineering>
excess_col=['date','TransactionDT']
train.drop(columns=excess_col,inplace=True)
 test.drop(columns=excess_col,inplace=True )
all_data['_amount_decimal'] =(( all_data['TransactionAmt'] - all_data['TransactionAmt'].astype(int)) * 1000 ).astype(int)
 all_data['_amount_decimal_len'] = all_data['TransactionAmt'].apply(lambda x: len(re.sub('0+$', '', str(x)).split('.')[1]))
all_data['_amount_fraction'] = all_data['TransactionAmt'].apply(lambda x: float('0.'+re.sub('^[0-9]|\.|0+$', '', str(x))))<groupby>
na_count_post=train.isna().sum()
print(na_count_post[na_count_post>0]/train.shape[0] )
def values_agg(df, periods, columns, aggs=['max']):
for period in periods:
for col in columns:
if col in df.columns:
new_col = f'{col}_{period}'
grouped_col = df.groupby([period])[col]
for a in aggs:
df[f'_{a}_{new_col}'] = df[period].map(grouped_col.agg(a ).to_dict())
 return df
all_data = values_agg(all_data, ['_uid2'], ccols)
<groupby>
categorical_cols=categorical_cols
amt_cols = ['_uid2','_P_emaildomain__ProductCD']
all_data = values_agg(all_data, amt_cols, ['TransactionAmt'], aggs=['max','mean','var'] )<count_values>
train_ip=train.copy()
test_ip=test.copy()
for f in ccols + amt_cols:
vc = all_data[f].value_counts(dropna=False)
 all_data[f'_count_full_{f}'] = all_data[f].map(vc )<data_type_conversions>
for col in numerical_cols:
train_ip[col] = train_ip[col].fillna(value=train_ip[col].mean(skipna=True))
test_ip[col] = test_ip[col].fillna(value=test_ip[col].mean(skipna=True))
all_data['_all_na'] = all_data.isna().sum(axis=1 ).astype(np.int8)
<data_type_conversions>
for col in categorical_cols:
train_ip[col] = train_ip[col].fillna(train_ip[col].mode() [0])
 test_ip[col] = test_ip[col].fillna(test_ip[col].mode() [0] )
all_data['_ccols_nonzero'] = all_data[ccols].apply(lambda x: len(x.to_numpy().nonzero() [0]), axis=1)
 all_data['_ccols_sum'] = all_data[ccols].sum(axis=1 ).astype(np.int8)
 all_data['_ccols_0_bin'] = ''
for c in ccols:
all_data['_ccols_0_bin'] +=(all_data[c] == 0 ).astype(int ).astype(str)
 all_data.drop(ccols, axis=1, inplace=True )<data_type_conversions>
na_count_post2=train_ip.isna().sum()
print(na_count_post[na_count_post2>0]/train_ip.shape[0] )
all_data['_dcol_na'] = all_data[dcols].isna().sum(axis=1 ).astype(np.int8)
 all_data['_dcols_na_bin'] = ''
for c in dcols:
all_data['_dcols_na_bin'] += all_data[c].isna().astype(int ).astype(str)
 for f in ['D1','D2']:
all_data[f] = all_data[f].fillna(0)- all_data['_days']
for f in ['D3','D4','D5','D6','D7','D10','D11','D12','D13','D14','D15']:
all_data[f] = all_data[f].fillna(0)- all_data['_days'] 
<data_type_conversions>
def labelencoder(train,test,col):
cod=list(train[col].values)+list(test[col].values)
 le=LabelEncoder().fit(cod)
 train[col]=le.transform(train[col])
 test[col]=le.transform(test[col])
 for c in categorical_cols:
labelencoder(train_ip,test_ip,c )
all_data['_mcols_na_bin'] = ''
for c in mcols:
all_data['_mcols_na_bin'] += all_data[c].isna().astype(int ).astype(str )<categorify>
params = {'num_leaves': int(( 2**10)*0.72),
'min_child_weight': 0.17,
'feature_fraction': 0.72,
'bagging_fraction': 0.72,
'min_data_in_leaf': 179,
'objective': 'binary',
'max_depth': -1,
'learning_rate': 0.006,
"boosting_type": "gbdt",
"bagging_seed": 13,
"metric": 'auc',
"verbosity": -1,
'reg_alpha': 0.3299927210061127,
'reg_lambda': 0.3885237330340494,
'random_state': 4,
}
vcols = [f'V{i}' for i in range(1,340)]
sc = preprocessing.MinMaxScaler()
dec = PCA(n_components=2, random_state=42)
 vcol_dec = dec.fit_transform(sc.fit_transform(all_data[vcols].fillna(-1)))
 all_data['_vcols_dec0'] = vcol_dec[:,0]
all_data['_vcols_dec1'] = vcol_dec[:,1]
all_data['_vcols_na'] = all_data[vcols].isna().sum(axis=1 ).astype(np.int8)
 for f in ['V144','V145','V150','V151','V159','V160','V307']:
vcols.remove(f)
 all_data['_vcols_sum'] = all_data[vcols].sum(axis=1 ).astype(np.int8)
 all_data.drop(vcols, axis=1, inplace=True )<data_type_conversions>
%%time
NFOLDS = 5
folds = KFold(n_splits=NFOLDS)
 columns = train_ip.columns
splits = folds.split(train_ip, response)
 y_preds = np.zeros(test_ip.shape[0])
 y_oof = np.zeros(train_ip.shape[0])
 score = 0
feature_importances = pd.DataFrame()
feature_importances['feature'] = columns
for fold_n,(train_index, valid_index)in enumerate(splits):
X_train, X_valid = train_ip[columns].iloc[train_index], train_ip[columns].iloc[valid_index]
y_train, y_valid = response.iloc[train_index], response.iloc[valid_index]
dtrain = lgb.Dataset(X_train, label=y_train)
 dvalid = lgb.Dataset(X_valid, label=y_valid)
 clf = lgb.train(params, dtrain, 10000, valid_sets = [dtrain, dvalid], verbose_eval=200, early_stopping_rounds=300)
 feature_importances[f'fold_{fold_n + 1}'] = clf.feature_importance()
y_pred_valid = clf.predict(X_valid)
 y_oof[valid_index] = y_pred_valid
print(f"Fold {fold_n + 1} | AUC: {roc_auc_score(y_valid, y_pred_valid)}")
 score += roc_auc_score(y_valid, y_pred_valid)/ NFOLDS
y_preds += clf.predict(test_ip)/ NFOLDS
del X_train, X_valid, y_train, y_valid
gc.collect()
print(f"
Mean AUC = {score}" )
_=
cnt_day = all_data['_days'].value_counts()
all_data['_count_trns_day'] = all_data['_days'].map(cnt_day.to_dict())
 daily_cols = ccols
for f in daily_cols:
if f in all_data.columns:
val_day = all_data[f].astype(str)+ '__' + all_data['_days'].astype(str)
 vc = val_day.value_counts(dropna=True)
 all_data[f'_count_day_{f}'] = val_day.map(vc)
 all_data[f'_count_pct_day_{f}'] = all_data[f'_count_day_{f}'] / all_data['_count_trns_day']
all_data.drop('_count_trns_day', axis=1, inplace=True )<drop_column>
sub = pd.read_csv(f'.. /input/ieee-fraud-detection/sample_submission.csv' )
<drop_column><EOS> 
sub['isFraud']= y_preds
sub.to_csv('submission.csv', index=False )