Kaggle competition: IEEE-CIS Fraud Detection
%matplotlib inline
np.random.seed(2 )<load_from_csv>
sub_path = ".. /input/ieeesubmissions"
all_files = os.listdir(sub_path)
 all_files
test = pd.read_csv('.. /input/digit-recognizer/test.csv')
<train_model>
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]
( X_train, Y_train),(X_val, Y_val)= mnist.load_data()<data_type_conversions>
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 )
X_train = X_train.reshape(-1, 28, 28, 1)
 X_val = X_val.reshape(-1, 28, 28, 1)
 X_train = X_train.astype('float32')
 X_val = X_val.astype('float32')
 test = test.values.reshape(-1, 28, 28, 1)
 print(X_train.shape,', ',X_val.shape,', ', test.shape )<split>
cutoff_lo = 0.8
cutoff_hi = 0.2
Y_train = to_categorical(Y_train, num_classes = 10)
 Y_val = to_categorical(Y_val, num_classes = 10 )<choose_model_class>
concat_sub['isFraud'] = concat_sub['ieee_mean']
concat_sub[['TransactionID', 'isFraud']].to_csv('stack_mean.csv',
index=False, float_format='%.6f' )
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5, 5), padding = "Same", activation = "relu",
kernel_initializer = 'he_uniform', input_shape =(28, 28, 1)))
 model.add(Conv2D(filters = 32, kernel_size =(5, 5), padding = "Same", activation = "relu",
kernel_initializer = 'he_uniform'))
model.add(MaxPooling2D(pool_size =(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3, 3), padding = "Same", activation = "relu",
kernel_initializer = 'he_uniform'))
model.add(Conv2D(filters = 64, kernel_size =(3, 3), padding = "Same", activation = "relu",
kernel_initializer = 'he_uniform'))
model.add(MaxPooling2D(pool_size =(2, 2), strides =(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation="relu", kernel_initializer = 'he_uniform'))
model.add(Dropout(0.5))
model.add(Dense(units = 10, activation="softmax", kernel_initializer = 'he_uniform'))<choose_model_class>
concat_sub['isFraud'] = concat_sub['ieee_median']
concat_sub[['TransactionID', 'isFraud']].to_csv('stack_median.csv',
index=False, float_format='%.6f' )
optimizer = RMSprop(lr = 0.001, rho = 0.9, epsilon = 1e-08, decay = 0.0)
 model.compile(optimizer = optimizer , loss = 'categorical_crossentropy', metrics = ['accuracy'] )<choose_model_class>
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' )
checkpoint = ModelCheckpoint('./mod_best.hdf5',monitor = 'val_loss', mode = "min", verbose = 1, save_best_model = True)
 earlystop = EarlyStopping(monitor = 'val_loss', patience = 3, min_delta = 0,verbose =1, restore_best_weights = True)
 reduce_lr = ReduceLROnPlateau(monitor='val_loss', patience=3, verbose=1, factor=0.5, min_lr=0.00001)
 callbacks = [reduce_lr,checkpoint]<init_hyperparams>
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' )
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train )<load_pretrained>
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' )
<train_model><EOS> 
concat_sub['isFraud'] = concat_sub['ieee0'].rank(method ='min')+ concat_sub['ieee1'].rank(method ='min')+ concat_sub['ieee2'].rank(method ='min')+concat_sub['ieee3'].rank(method ='min')
 concat_sub['isFraud'] =(concat_sub['isFraud']-concat_sub['isFraud'].min())/(concat_sub['isFraud'].max() - concat_sub['isFraud'].min())
 concat_sub.describe()
concat_sub[['TransactionID', 'isFraud']].to_csv('stack_rank.csv', index=False, float_format='%.8f' )