Coders Packet

Credit card fraud detection using Python

By Akhilesh Ketkar

Designed a Neural network using python that was able to classify Credit Card fraud transactions with 99.9% accuracy

This application can detect Credit card fraud using Keras.

To build this application first we need to create a neural network model that can predict Credit card fraud.

To create neural network model:
Requirments:
                  Python 3.5–3.8
                  pip 19.0 or later (requires manylinux2010 support)
                  Ubuntu 16.04 or later (64-bit)
                  macOS 10.12.6 (Sierra) or later (64-bit) (no GPU support)
                  Windows 7 or later (64-bit)
                  Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017 and 2019
                  GPU support requires a CUDA®-enabled card
PROCEDURE:
                  Install tensorflow using "pip install tensorflow".
                  Install Keras using "pip install keras".
                  We can get training images and testing images from kaggle credit card dataset.
                  We need to create neural network architecture (or We can implement predefined architecture ).
                  After creating neural network next step is to train the model using mnist dataset.
                  Store the weights in '.h5' format.

First, we need to define our Metric.

METRICS = [
        tf.keras.metrics.BinaryAccuracy(),
        tf.keras.metrics.Precision(name="precision"),
        tf.keras.metrics.Recall(name="recall"),
    ]

Then we'll create and train CNN architecture. Here we've applied early stopping which will improve our training time.

model = Sequential(
    [
        Dense(16, activation="relu", name="layer1",input_shape=(train_df.shape[-1],)),
        Dense(32, activation="relu", name="layer2"),
        Dropout(0.5),
        Dense(1, activation='sigmoid',name="layer3",bias_initializer=output_bias)
    ]
)
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
layer1 (Dense)               (None, 16)                352       
_________________________________________________________________
layer2 (Dense)               (None, 32)                544       
_________________________________________________________________
dropout_8 (Dropout)          (None, 32)                0         
_________________________________________________________________
layer3 (Dense)               (None, 1)                 33        
=================================================================
Total params: 929
Trainable params: 929
Non-trainable params: 0
_________________________________________________________________

early_stopping = tf.keras.callbacks.EarlyStopping(
    monitor='val_auc', 
    verbose=1,
    patience=10,
    mode='max',
    restore_best_weights=True)

BATCH_SIZE = 2048
EPOCHS = 50


history = model.fit(
    X_train,
    y_train,
    batch_size=BATCH_SIZE,
    epochs=EPOCHS,
    callbacks = [early_stopping],
    class_weight = class_weight,
    validation_data=(X_val, y_val))

Download Complete Code

Comments

No comments yet