Kaggle competition: IEEE-CIS Fraud Detection
<SOS> metric: AUC Kaggle data source: ieee-cis-fraud-detection<predict_on_test>
pd.set_option('display.max_columns', 200)
 warnings.filterwarnings("ignore")
 %matplotlib inline
y_pred1 = model_LeNet5_aug.predict(X_train_aug, batch_size=None, verbose=0)
 y_pred2 = model_LeNet5_2.predict(X_train_aug, batch_size=None, verbose=0)
 mask = np.argmax(y_pred1, axis=1)== np.argmax(y_pred2, axis=1)
 X_train3 = X_train_aug[~mask]
Y_train3 = Y_train_aug[~mask]<choose_model_class>
print(os.listdir(".. /input"))
input_path = '.. /input'
%matplotlib inline
RANDOM_SEED = 42
nan_replace = -999
model_LeNet5_3 = LeNet5BN(name="Net3" )<train_model>
baseline_random_forest = pd.read_csv('.. /input/previous-submissions/baseline_random_forest.csv', index_col='TransactionID')
 baseline_xgboost = pd.read_csv('.. /input/previous-submissions/baseline_xgboost.csv', index_col='TransactionID')
 xgboost_with_tuning = pd.read_csv('.. /input/previous-submissions/xgboost_with_tuning.csv', index_col='TransactionID')
 preprocessed2_xgboost = pd.read_csv('.. /input/previous-submissions/preprocessed2_xgboost.csv', index_col='TransactionID' )
early_stop = EarlyStopping(patience=5, monitor='loss', restore_best_weights=True)
 batch_size = 128
model_LeNet5_3.fit(X_train3, Y_train3,
epochs=100,
callbacks=[early_stop] )<predict_on_test>
ensemble =.025*baseline_random_forest +.05*xgboost_with_tuning +.075*preprocessed2_xgboost +.85*baseline_xgboost
ensemble.head()
def boosted_lenet5(x):
pred1 = model_LeNet5_aug.predict(x,batch_size=None, verbose=0)
 pred2 = model_LeNet5_2.predict(x,batch_size=None, verbose=0)
 pred3 = model_LeNet5_3.predict(x,batch_size=None, verbose=0)
 final_pred = np.argmax(pred1+pred2+pred3, axis = 1)
 return final_pred<find_best_params>
ensemble.to_csv('ensemble6.csv' )
final_pred = boosted_lenet5(X_val )<compute_train_metric>
conf_mat, class_report, accuracy, faults = analyze_results("boosted LeNet5", final_pred, np.argmax(Y_val, axis=1), X_val)
<feature_engineering>
show_faults(faults )<create_dataframe>
acc = [preds1[1], preds2[1], preds3[1], accuracy]
models = ['LeNet5 Base', 'Improved', 'Augmented', 'Boosted LeNet5']
tbl = {"Model": models, "Accuracies": acc}
tbl_df = pd.DataFrame(tbl)
 tbl_df<save_to_csv>
Numerical feature(' + str(df1_description), ')\tFeature name:', feature, '
mean:', get_mean_of_feature(df1, feature))
Numerical feature(' + str(df2_description), ')\tFeature name:', feature, '
mean:', get_mean_of_feature(df2, feature))
Categorical feature(' + str(df1_description), ')\tFeature name:', feature, '
Distribution:
', get_categorical_distribution_of_feature(df1,feature)[:10])
 Categorical feature(' + str(df2_description), ')\tFeature name:', feature, '
Distribution:
', get_categorical_distribution_of_feature(df2,feature)[:10] )
test_label = boosted_lenet5(test)
 result = {"ImageId":range(1,1+test_label.shape[0]), "Label":test_label}
submission = pd.DataFrame(result)
 submission.to_csv('submission.csv', index=False )<prepare_x_and_y>
X_train = []
Y_train = []
with open('/kaggle/input/digit-recognizer/train.csv')as train_file:
for line in train_file.readlines() [1:]:
content = line.strip().split(',')
 label, image_raw_data = content[0], content[1:]
image_np = np.array_split(np.array(image_raw_data), 28)
 X_train.append(image_np)
 Y_train.append(label)
 X_train = np.array(X_train ).astype(float)
 X_train = np.expand_dims(X_train, axis=3)
 Y_train = np.array(Y_train ).astype(float)
 print(X_train.shape)
 print(Y_train.shape )<load_from_csv>
X_test = []
with open('/kaggle/input/digit-recognizer/test.csv')as test_file:
for line in test_file.readlines() [1:]:
content = line.strip().split(',')
 image_raw_data = content[:]
image_np = np.array_split(np.array(image_raw_data), 28)
 X_test.append(image_np)
 X_test = np.array(X_test ).astype(float)
 X_test = np.expand_dims(X_test, axis=3)
 print(X_test.shape )<load_from_url>
!wget --recursive --no-parent 'https://github.com/google/fonts/raw/master/apache/opensans/OpenSans-Regular.ttf' -P /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf
!wget --recursive --no-parent 'https://github.com/google/fonts/raw/master/apache/opensans/OpenSans-Light.ttf' -P /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf
!wget --recursive --no-parent 'https://github.com/google/fonts/raw/master/apache/opensans/OpenSans-SemiBold.ttf' -P /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf
!wget --recursive --no-parent 'https://github.com/google/fonts/raw/master/apache/opensans/OpenSans-Bold.ttf' -P /usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf<choose_model_class>
model = tf.keras.models.Sequential([
tf.keras.Input(shape=(28, 28, 1)) ,
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Conv2D(32,(5,5), activation='relu', kernel_initializer = 'he_uniform', padding="SAME"),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Conv2D(32,(5,5), activation='relu', kernel_initializer = 'he_uniform', padding="SAME"),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Conv2D(64,(3,3), activation='relu', kernel_initializer = 'he_uniform', padding="SAME"),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Conv2D(64,(3,3), activation='relu', kernel_initializer = 'he_uniform', padding="SAME"),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Conv2D(128,(3,3), activation='relu', kernel_initializer = 'he_uniform', padding="SAME"),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Conv2D(128,(3,3), activation='relu', kernel_initializer = 'he_uniform', padding="SAME"),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(512, activation='relu', kernel_initializer = 'he_uniform'),
tf.keras.layers.Dropout(rate=0.5),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dense(256, activation='relu', kernel_initializer = 'he_uniform'),
tf.keras.layers.Dropout(rate=0.5),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dense(128, activation='relu', kernel_initializer = 'he_uniform'),
tf.keras.layers.Dropout(rate=0.5),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dense(64, activation='relu', kernel_initializer = 'he_uniform'),
tf.keras.layers.Dropout(rate=0.5),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dense(10, activation='softmax')
 ])
 model.compile(
 optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'] 
)
 model.summary()
early_stopping_callback = tf.keras.callbacks.EarlyStopping(monitor="accuracy", mode='max', patience=25)
 model_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint('./best_model.hdf5', monitor='accuracy',
mode='max', verbose=1, save_best_model=True)
 reduce_lr_callback = tf.keras.callbacks.ReduceLROnPlateau(monitor='accuracy', mode='max', patience=15,
verbose=1, factor=0.5, min_lr=0.0001)
 history = model.fit(
 X_train / 255,
tf.keras.utils.to_categorical(Y_train, num_classes=10),
epochs=1000,
shuffle=True,
callbacks=[early_stopping_callback, model_checkpoint_callback,reduce_lr_callback]
)<save_to_csv>
predictions = model.predict(X_test / 255)
 y_pred = np.argmax(predictions, axis=-1)
 submission_df = pd.DataFrame(data={
'ImageId': np.arange(1, X_test.shape[0] + 1),
'label': y_pred
})
 submission_df.to_csv('submission.csv', index=False)
 submission_df<define_search_space>