Kaggle competition: IEEE-CIS Fraud Detection
optimizer = torch.optim.Adam(model.parameters() , lr=LR, betas=(0.9, 0.999), eps=1e-9)
 scheduler = lr_scheduler.ReduceLROnPlateau(optimizer, mode='max', factor=0.7, patience=3, verbose=True )<train_model>
sub_path = ".. /input/ieeesubmissions"
all_files = os.listdir(sub_path)
 all_files
def train_model(model, criterion, optimizer, scheduler, num_epochs=25):
since = time.time()
best_model_wts = copy.deepcopy(model.state_dict())
 best_acc = 0.0
for epoch in range(num_epochs):
for phase in ['train', 'val']:
if phase == 'train':
model.train()
else:
model.eval()
running_loss = 0.0
running_corrects = 0
for inputs, labels in dataloaders[phase]:
inputs = inputs.to(device)
 labels = labels.to(device)
 optimizer.zero_grad()
with torch.set_grad_enabled(phase == 'train'):
outputs = model(inputs)
 _, preds = torch.max(outputs, 1)
 loss = criterion(outputs, labels)
 if phase == 'train':
loss.backward()
optimizer.step()
running_loss += loss.item() * inputs.size(0)
 running_corrects += torch.sum(preds == labels.data)
 if phase == 'train':
acc = 100.* running_corrects.double() / dataset_sizes[phase]
scheduler.step(acc)
 epoch_loss = running_loss / dataset_sizes[phase]
epoch_acc = running_corrects.double() / dataset_sizes[phase]
if phase == 'train':
print('Epoch {}/{}'.format(epoch, num_epochs - 1))
print('-' * 10)
 print('{} Loss: {:.4f} Acc: {:.4f}'.format(
 phase, epoch_loss, epoch_acc))
if phase == 'val' and epoch_acc > best_acc:
best_acc = epoch_acc
best_model_wts = copy.deepcopy(model.state_dict())
 time_elapsed = time.time() - since
print('Training complete in {:.0f}m {:.0f}s'.format(
 time_elapsed // 60, time_elapsed % 60))
print('Best val Acc: {:4f}'.format(best_acc))
model.load_state_dict(best_model_wts)
 return model<train_model>
all_files.remove('blend02.csv')
 all_files
model = train_model(model, criterion, optimizer, scheduler,
num_epochs=EPOCHS )<find_best_params>
outs = [pd.read_csv(os.path.join(sub_path, f), index_col=0)for f in all_files]
concat_sub = pd.concat(outs, axis=1)
 cols = list(map(lambda x: "ieee" + str(x), range(len(concat_sub.columns))))
concat_sub.columns = cols
concat_sub.reset_index(inplace=True)
 concat_sub.head()
ncol = concat_sub.shape[1]
y_val = validate_df.label.values
x_val = validate_df.loc[:, validate_df.columns != 'label'].values/255
x_val = x_val.reshape(( -1, 1, 28, 28))
print(x_val[0].shape)
 with torch.no_grad() :
model.eval()
preds = model(torch.from_numpy(x_val ).to(DEVICE)).cpu().argmax(dim=1)
<prepare_output>
concat_sub['ieee_max'] = concat_sub.iloc[:, 1:ncol].max(axis=1)
 concat_sub['ieee_min'] = concat_sub.iloc[:, 1:ncol].min(axis=1)
 concat_sub['ieee_mean'] = concat_sub.iloc[:, 1:ncol].mean(axis=1)
 concat_sub['ieee_median'] = concat_sub.iloc[:, 1:ncol].median(axis=1 )
with torch.no_grad() :
model.eval()
sample_sub['Label'] = model(torch.from_numpy(Xt ).to(DEVICE)).cpu().argmax(dim=1 )<save_to_csv>
cutoff_lo = 0.8
cutoff_hi = 0.2
sample_sub.to_csv("submission.csv", index=False )<load_from_csv>
concat_sub['isFraud'] = concat_sub['ieee_mean']
concat_sub[['TransactionID', 'isFraud']].to_csv('stack_mean.csv',
index=False, float_format='%.6f' )
test_df = pd.read_csv('.. /input/digit-recognizer/test.csv')
 train_df = pd.read_csv('.. /input/digit-recognizer/train.csv')
 print('Shape of the testing dataset:', test_df.shape)
 print('Shape of the training dataset:', train_df.shape )<split>
concat_sub['isFraud'] = concat_sub['ieee_median']
concat_sub[['TransactionID', 'isFraud']].to_csv('stack_median.csv',
index=False, float_format='%.6f' )
train_labels = train_df['label']
train_images = train_df.drop('label', axis=1)
 train_images = train_images.astype('float32')/255
test_images = test_df.astype('float32')/255
train_images = np.array(train_images ).reshape(42000,28,28,1)
 test_images = np.array(test_images ).reshape(28000,28,28,1)
 train_images, validation_images, train_labels, validation_labels = train_test_split(train_images, train_labels, test_size=0.1, random_state=0)
 train_labels = to_categorical(train_labels)
 validation_labels = to_categorical(validation_labels )<set_options>
concat_sub['isFraud'] = np.where(np.all(concat_sub.iloc[:,1:ncol] > cutoff_lo, axis=1), 1,
np.where(np.all(concat_sub.iloc[:,1:ncol] < cutoff_hi, axis=1),
0, concat_sub['ieee_median']))
concat_sub[['TransactionID', 'isFraud']].to_csv('stack_pushout_median.csv',
index=False, float_format='%.6f' )
datagen = ImageDataGenerator(rotation_range=10,
width_shift_range=0.05,
height_shift_range=0.05,
shear_range=0.1,
zoom_range=0.05,
horizontal_flip=False,
fill_mode='nearest')
 datagen.fit(train_images )<choose_model_class>
concat_sub['isFraud'] = np.where(np.all(concat_sub.iloc[:,1:ncol] > cutoff_lo, axis=1),
concat_sub['ieee_max'],
np.where(np.all(concat_sub.iloc[:,1:ncol] < cutoff_hi, axis=1),
concat_sub['ieee_min'],
concat_sub['ieee_mean']))
concat_sub[['TransactionID', 'isFraud']].to_csv('stack_minmax_mean.csv',
index=False, float_format='%.6f' )
def createModel() :
model = models.Sequential()
model.add(layers.Conv2D(32,(3, 3), activation='relu', padding='same', input_shape=(28, 28, 1)))
 model.add(layers.BatchNormalization())
 model.add(layers.MaxPooling2D(( 2, 2)))
 model.add(layers.Dropout(0.2))
model.add(layers.Conv2D(64,(3, 3), activation='relu', padding='same'))
model.add(layers.BatchNormalization())
 model.add(layers.MaxPooling2D(( 2, 2)))
 model.add(layers.Dropout(0.2))
model.add(layers.Conv2D(128,(3, 3), activation='relu', padding='same'))
model.add(layers.BatchNormalization())
 model.add(layers.MaxPooling2D(( 2, 2)))
 model.add(layers.Dropout(0.2))
model.add(layers.Conv2D(256,(3, 3), activation='relu', padding='same'))
model.add(layers.BatchNormalization())
 model.add(layers.MaxPooling2D(( 2, 2)))
 model.add(layers.Dropout(0.2))
model.add(layers.Flatten())
 model.add(layers.Dense(256, activation='relu'))
model.add(layers.BatchNormalization())
 model.add(layers.Dropout(0.2))
model.add(layers.Dense(10, activation='softmax'))
model.compile(optimizer=optimizers.Adam(lr=1e-3),
loss='categorical_crossentropy',
metrics=['acc'])
 return model<choose_model_class>
concat_sub['isFraud'] = np.where(np.all(concat_sub.iloc[:,1:ncol] > cutoff_lo, axis=1),
concat_sub['ieee_max'],
np.where(np.all(concat_sub.iloc[:,1:ncol] < cutoff_hi, axis=1),
concat_sub['ieee_min'],
concat_sub['ieee_median']))
concat_sub[['TransactionID', 'isFraud']].to_csv('stack_minmax_median.csv',
index=False, float_format='%.6f' )
lr_reduction = ReduceLROnPlateau(monitor = 'val_acc', factor = 0.5, min_lr = 1e-6)
 checkpoint = ModelCheckpoint('./trainedModel.hdf5',monitor = 'val_acc', mode = "max", save_best_model = True )<train_model>
sub_base = pd.read_csv('.. /input/ieeesubmissions/blend01.csv' )
<predict_on_test><EOS> 
concat_sub['ieee_base'] = sub_base['isFraud']
concat_sub['isFraud'] = np.where(np.all(concat_sub.iloc[:,1:ncol] > cutoff_lo, axis=1),
concat_sub['ieee_max'],
np.where(np.all(concat_sub.iloc[:,1:ncol] < cutoff_hi, axis=1),
concat_sub['ieee_min'],
concat_sub['ieee_base']))
concat_sub[['TransactionID', 'isFraud']].to_csv('stack_minmax_bestbase.csv',
index=False, float_format='%.6f' )