Classification on imbalanced data

View on TensorFlow.org Run in Google Colab View source on GitHub Download notebook

This tutorial demonstrates how to classify a highly imbalanced dataset in which the number of examples in one class greatly outnumbers the examples in another. You will work with the Credit Card Fraud Detection dataset hosted on Kaggle. The aim is to detect a mere 492 fraudulent transactions from 284,807 transactions in total. You will use Keras to define the model and class weights to help the model learn from the imbalanced data. .

This tutorial contains complete code to:

  • Load a CSV file using Pandas.
  • Create train, validation, and test sets.
  • Define and train a model using Keras (including setting class weights).
  • Evaluate the model using various metrics (including precision and recall).
  • Try common tecniques for dealing with imbalanced data like:
    • Class weighting
    • Oversampling

Setup

from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
from tensorflow import keras

import os
import tempfile

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns

import sklearn
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
mpl.rcParams['figure.figsize'] = (12, 10)
colors = plt.rcParams['axes.prop_cycle'].by_key()['color']

Data processing and exploration

Download the Kaggle Credit Card Fraud data set

Pandas is a Python library with many helpful utilities for loading and working with structured data and can be used to download CSVs into a dataframe.

file = tf.keras.utils
raw_df = pd.read_csv('https://storage.googleapis.com/download.tensorflow.org/data/creditcard.csv')
raw_df.head()
raw_df[['Time', 'V1', 'V2', 'V3', 'V4', 'V5', 'V26', 'V27', 'V28', 'Amount', 'Class']].describe()

Examine the class label imbalance

Let's look at the dataset imbalance:

neg, pos = np.bincount(raw_df['Class'])
total = neg + pos
print('Examples:\n    Total: {}\n    Positive: {} ({:.2f}% of total)\n'.format(
    total, pos, 100 * pos / total))
Examples:
    Total: 284807
    Positive: 492 (0.17% of total)

This shows the small fraction of positive samples.

Clean, split and normalize the data

The raw data has a few issues. First the Time and Amount columns are too variable to use directly. Drop the Time column (since it's not clear what it means) and take the log of the Amount column to reduce its range.

cleaned_df = raw_df.copy()

# You don't want the `Time` column.
cleaned_df.pop('Time')

# The `Amount` column covers a huge range. Convert to log-space.
eps=0.001 # 0 => 0.1¢
cleaned_df['Log Ammount'] = np.log(cleaned_df.pop('Amount')+eps)

Split the dataset into train, validation, and test sets. The validation set is used during the model fitting to evaluate the loss and any metrics, however the model is not fit with this data. The test set is completely unused during the training phase and is only used at the end to evaluate how well the model generalizes to new data. This is especially important with imbalanced datasets where overfitting is a significant concern from the lack of training data.

# Use a utility from sklearn to split and shuffle our dataset.
train_df, test_df = train_test_split(cleaned_df, test_size=0.2)
train_df, val_df = train_test_split(train_df, test_size=0.2)

# Form np arrays of labels and features.
train_labels = np.array(train_df.pop('Class'))
bool_train_labels = train_labels != 0
val_labels = np.array(val_df.pop('Class'))
test_labels = np.array(test_df.pop('Class'))

train_features = np.array(train_df)
val_features = np.array(val_df)
test_features = np.array(test_df)

Normalize the input features using the sklearn StandardScaler. This will set the mean to 0 and standard deviation to 1.

scaler = StandardScaler()
train_features = scaler.fit_transform(train_features)

val_features = scaler.transform(val_features)
test_features = scaler.transform(test_features)

train_features = np.clip(train_features, -5, 5)
val_features = np.clip(val_features, -5, 5)
test_features = np.clip(test_features, -5, 5)


print('Training labels shape:', train_labels.shape)
print('Validation labels shape:', val_labels.shape)
print('Test labels shape:', test_labels.shape)

print('Training features shape:', train_features.shape)
print('Validation features shape:', val_features.shape)
print('Test features shape:', test_features.shape)

Training labels shape: (182276,)
Validation labels shape: (45569,)
Test labels shape: (56962,)
Training features shape: (182276, 29)
Validation features shape: (45569, 29)
Test features shape: (56962, 29)

Look at the data distribution

Next compare the distributions of the positive and negative examples over a few features. Good questions to ask yourself at this point are:

  • Do these distributions make sense?
    • Yes. You've normalized the input and these are mostly concentrated in the +/- 2 range.
  • Can you see the difference between the ditributions?
    • Yes the positive examples contain a much higher rate of extreme values.
pos_df = pd.DataFrame(train_features[ bool_train_labels], columns = train_df.columns)
neg_df = pd.DataFrame(train_features[~bool_train_labels], columns = train_df.columns)

sns.jointplot(pos_df['V5'], pos_df['V6'],
              kind='hex', xlim = (-5,5), ylim = (-5,5))
plt.suptitle("Positive distribution")

sns.jointplot(neg_df['V5'], neg_df['V6'],
              kind='hex', xlim = (-5,5), ylim = (-5,5))
_ = plt.suptitle("Negative distribution")

png

png

Define the model and metrics

Define a function that creates a simple neural network with a densly connected hidden layer, a dropout layer to reduce overfitting, and an output sigmoid layer that returns the probability of a transaction being fraudulent:

METRICS = [
      keras.metrics.TruePositives(name='tp'),
      keras.metrics.FalsePositives(name='fp'),
      keras.metrics.TrueNegatives(name='tn'),
      keras.metrics.FalseNegatives(name='fn'), 
      keras.metrics.BinaryAccuracy(name='accuracy'),
      keras.metrics.Precision(name='precision'),
      keras.metrics.Recall(name='recall'),
      keras.metrics.AUC(name='auc'),
]

def make_model(metrics = METRICS, output_bias=None):
  if output_bias is not None:
    output_bias = tf.keras.initializers.Constant(output_bias)
  model = keras.Sequential([
      keras.layers.Dense(
          16, activation='relu',
          input_shape=(train_features.shape[-1],)),
      keras.layers.Dropout(0.5),
      keras.layers.Dense(1, activation='sigmoid',
                         bias_initializer=output_bias),
  ])

  model.compile(
      optimizer=keras.optimizers.Adam(lr=1e-3),
      loss=keras.losses.BinaryCrossentropy(),
      metrics=metrics)

  return model

Understanding useful metrics

Notice that there are a few metrics defined above that can be computed by the model that will be helpful when evaluating the performance.

  • False negatives and false positives are samples that were incorrectly classified
  • True negatives and true positives are samples that were correctly classified
  • Accuracy is the percentage of examples correctly classified > $\frac{\text{true samples}}{\text{total samples}}$
  • Precision is the percentage of predicted positives that were correctly classified > $\frac{\text{true positives}}{\text{true positives + false positives}}$
  • Recall is the percentage of actual positives that were correctly classified > $\frac{\text{true positives}}{\text{true positives + false negatives}}$
  • AUC refers to the Area Under the Curve of a Receiver Operating Characteristic curve (ROC-AUC). This metric is equal to the probability that a classifier will rank a random positive sample higher than than a random negative sample.

Read more:

Baseline model

Build the model

Now create and train your model using the function that was defined earlier. Notice that the model is fit using a larger than default batch size of 2048, this is important to ensure that each batch has a decent chance of containing a few positive samples. If the batch size was too small, they would likely have no fraudulent transactions to learn from.

EPOCHS = 100
BATCH_SIZE = 2048

early_stopping = tf.keras.callbacks.EarlyStopping(
    monitor='val_auc', 
    verbose=1,
    patience=10,
    mode='max',
    restore_best_weights=True)
model = make_model()
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 16)                480       
_________________________________________________________________
dropout (Dropout)            (None, 16)                0         
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 17        
=================================================================
Total params: 497
Trainable params: 497
Non-trainable params: 0
_________________________________________________________________

Test run the model:

model.predict(train_features[:10])
array([[0.26807803],
       [0.33758447],
       [0.46022922],
       [0.29806566],
       [0.19197516],
       [0.49315304],
       [0.3052543 ],
       [0.20242073],
       [0.05214652],
       [0.3978653 ]], dtype=float32)

Optional: Set the correct initial bias.

These are initial guesses are not great. You know the dataset is imbalanced. Set the output layer's bias to reflect that (See: A Recipe for Training Neural Networks: "init well"). This can help with initial convergence.

With the default bias initialization the loss should be about math.log(2) = 0.69314

results = model.evaluate(train_features, train_labels, batch_size=BATCH_SIZE, verbose=0)
print("Loss: {:0.4f}".format(results[0]))
Loss: 0.4946

The correct bias to set can be derived from:

$$ p_0 = pos/(pos + neg) = 1/(1+e^{-b_0}) $$
$$ b_0 = -log_e(1/p_0 - 1) $$
$$ b_0 = log_e(pos/neg)$$
initial_bias = np.log([pos/neg])
initial_bias
array([-6.35935934])

Set that as the initial bias, and the model will give much more reasonable initial guesses.

It should be near: pos/total = 0.0018

model = make_model(output_bias = initial_bias)
model.predict(train_features[:10])
array([[0.00191282],
       [0.00225013],
       [0.00159295],
       [0.00177584],
       [0.00145299],
       [0.00286585],
       [0.00078522],
       [0.00144365],
       [0.00229752],
       [0.00095429]], dtype=float32)

With this initialization the initial loss should be approximately:

$$-p_0log(p_0)-(1-p_0)log(1-p_0) = 0.01317$$
results = model.evaluate(train_features, train_labels, batch_size=BATCH_SIZE, verbose=0)
print("Loss: {:0.4f}".format(results[0]))
Loss: 0.0167

This initial loss is about 50 times less than if would have been with naive initilization.

This way the model doesn't need to spend the first few epochs just learning that positive examples are unlikely. This also makes it easier to read plots of the loss during training.

Checkpoint the initial weights

To make the various training runs more comparable, keep this initial model's weights in a checkpoint file, and load them into each model before training.

initial_weights = os.path.join(tempfile.mkdtemp(),'initial_weights')
model.save_weights(initial_weights)

Confirm that the bias fix helps

Before moving on, confirm quick that the careful bias initialization actually helped.

Train the model for 20 epochs, with and without this careful initialization, and compare the losses:

model = make_model()
model.load_weights(initial_weights)
model.layers[-1].bias.assign([0.0])
zero_bias_history = model.fit(
    train_features,
    train_labels,
    batch_size=BATCH_SIZE,
    epochs=20,
    validation_data=(val_features, val_labels), 
    verbose=0)
model = make_model()
model.load_weights(initial_weights)
careful_bias_history = model.fit(
    train_features,
    train_labels,
    batch_size=BATCH_SIZE,
    epochs=20,
    validation_data=(val_features, val_labels), 
    verbose=0)
def plot_loss(history, label, n):
  # Use a log scale to show the wide range of values.
  plt.semilogy(history.epoch,  history.history['loss'],
               color=colors[n], label='Train '+label)
  plt.semilogy(history.epoch,  history.history['val_loss'],
          color=colors[n], label='Val '+label,
          linestyle="--")
  plt.xlabel('Epoch')
  plt.ylabel('Loss')
  
  plt.legend()
plot_loss(zero_bias_history, "Zero Bias", 0)
plot_loss(careful_bias_history, "Careful Bias", 1)

png

The above figure makes it clear: In terms of validation loss, on this problem, this careful initialization gives a clear advantage.

Train the model

model = make_model()
model.load_weights(initial_weights)
baseline_history = model.fit(
    train_features,
    train_labels,
    batch_size=BATCH_SIZE,
    epochs=EPOCHS,
    callbacks = [early_stopping],
    validation_data=(val_features, val_labels))
Train on 182276 samples, validate on 45569 samples
Epoch 1/100
182276/182276 [==============================] - 3s 15us/sample - loss: 0.0124 - tp: 45.0000 - fp: 19.0000 - tn: 181938.0000 - fn: 274.0000 - accuracy: 0.9984 - precision: 0.7031 - recall: 0.1411 - auc: 0.6948 - val_loss: 0.0056 - val_tp: 26.0000 - val_fp: 4.0000 - val_tn: 45494.0000 - val_fn: 45.0000 - val_accuracy: 0.9989 - val_precision: 0.8667 - val_recall: 0.3662 - val_auc: 0.9008
Epoch 2/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0079 - tp: 118.0000 - fp: 28.0000 - tn: 181929.0000 - fn: 201.0000 - accuracy: 0.9987 - precision: 0.8082 - recall: 0.3699 - auc: 0.8419 - val_loss: 0.0044 - val_tp: 41.0000 - val_fp: 8.0000 - val_tn: 45490.0000 - val_fn: 30.0000 - val_accuracy: 0.9992 - val_precision: 0.8367 - val_recall: 0.5775 - val_auc: 0.9153
Epoch 3/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0064 - tp: 165.0000 - fp: 24.0000 - tn: 181933.0000 - fn: 154.0000 - accuracy: 0.9990 - precision: 0.8730 - recall: 0.5172 - auc: 0.8751 - val_loss: 0.0040 - val_tp: 45.0000 - val_fp: 8.0000 - val_tn: 45490.0000 - val_fn: 26.0000 - val_accuracy: 0.9993 - val_precision: 0.8491 - val_recall: 0.6338 - val_auc: 0.9153
Epoch 4/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0057 - tp: 169.0000 - fp: 29.0000 - tn: 181928.0000 - fn: 150.0000 - accuracy: 0.9990 - precision: 0.8535 - recall: 0.5298 - auc: 0.8925 - val_loss: 0.0038 - val_tp: 46.0000 - val_fp: 8.0000 - val_tn: 45490.0000 - val_fn: 25.0000 - val_accuracy: 0.9993 - val_precision: 0.8519 - val_recall: 0.6479 - val_auc: 0.9154
Epoch 5/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0056 - tp: 169.0000 - fp: 27.0000 - tn: 181930.0000 - fn: 150.0000 - accuracy: 0.9990 - precision: 0.8622 - recall: 0.5298 - auc: 0.9007 - val_loss: 0.0036 - val_tp: 51.0000 - val_fp: 8.0000 - val_tn: 45490.0000 - val_fn: 20.0000 - val_accuracy: 0.9994 - val_precision: 0.8644 - val_recall: 0.7183 - val_auc: 0.9224
Epoch 6/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0054 - tp: 177.0000 - fp: 33.0000 - tn: 181924.0000 - fn: 142.0000 - accuracy: 0.9990 - precision: 0.8429 - recall: 0.5549 - auc: 0.9042 - val_loss: 0.0033 - val_tp: 52.0000 - val_fp: 8.0000 - val_tn: 45490.0000 - val_fn: 19.0000 - val_accuracy: 0.9994 - val_precision: 0.8667 - val_recall: 0.7324 - val_auc: 0.9223
Epoch 7/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0051 - tp: 176.0000 - fp: 28.0000 - tn: 181929.0000 - fn: 143.0000 - accuracy: 0.9991 - precision: 0.8627 - recall: 0.5517 - auc: 0.9029 - val_loss: 0.0032 - val_tp: 54.0000 - val_fp: 8.0000 - val_tn: 45490.0000 - val_fn: 17.0000 - val_accuracy: 0.9995 - val_precision: 0.8710 - val_recall: 0.7606 - val_auc: 0.9223
Epoch 8/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0050 - tp: 178.0000 - fp: 28.0000 - tn: 181929.0000 - fn: 141.0000 - accuracy: 0.9991 - precision: 0.8641 - recall: 0.5580 - auc: 0.9092 - val_loss: 0.0030 - val_tp: 54.0000 - val_fp: 8.0000 - val_tn: 45490.0000 - val_fn: 17.0000 - val_accuracy: 0.9995 - val_precision: 0.8710 - val_recall: 0.7606 - val_auc: 0.9223
Epoch 9/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0048 - tp: 187.0000 - fp: 28.0000 - tn: 181929.0000 - fn: 132.0000 - accuracy: 0.9991 - precision: 0.8698 - recall: 0.5862 - auc: 0.9125 - val_loss: 0.0029 - val_tp: 54.0000 - val_fp: 8.0000 - val_tn: 45490.0000 - val_fn: 17.0000 - val_accuracy: 0.9995 - val_precision: 0.8710 - val_recall: 0.7606 - val_auc: 0.9223
Epoch 10/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0048 - tp: 178.0000 - fp: 32.0000 - tn: 181925.0000 - fn: 141.0000 - accuracy: 0.9991 - precision: 0.8476 - recall: 0.5580 - auc: 0.9157 - val_loss: 0.0028 - val_tp: 54.0000 - val_fp: 8.0000 - val_tn: 45490.0000 - val_fn: 17.0000 - val_accuracy: 0.9995 - val_precision: 0.8710 - val_recall: 0.7606 - val_auc: 0.9434
Epoch 11/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0046 - tp: 190.0000 - fp: 31.0000 - tn: 181926.0000 - fn: 129.0000 - accuracy: 0.9991 - precision: 0.8597 - recall: 0.5956 - auc: 0.9158 - val_loss: 0.0027 - val_tp: 54.0000 - val_fp: 7.0000 - val_tn: 45491.0000 - val_fn: 17.0000 - val_accuracy: 0.9995 - val_precision: 0.8852 - val_recall: 0.7606 - val_auc: 0.9434
Epoch 12/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0046 - tp: 181.0000 - fp: 23.0000 - tn: 181934.0000 - fn: 138.0000 - accuracy: 0.9991 - precision: 0.8873 - recall: 0.5674 - auc: 0.8985 - val_loss: 0.0027 - val_tp: 55.0000 - val_fp: 8.0000 - val_tn: 45490.0000 - val_fn: 16.0000 - val_accuracy: 0.9995 - val_precision: 0.8730 - val_recall: 0.7746 - val_auc: 0.9434
Epoch 13/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0045 - tp: 190.0000 - fp: 26.0000 - tn: 181931.0000 - fn: 129.0000 - accuracy: 0.9991 - precision: 0.8796 - recall: 0.5956 - auc: 0.9111 - val_loss: 0.0026 - val_tp: 55.0000 - val_fp: 7.0000 - val_tn: 45491.0000 - val_fn: 16.0000 - val_accuracy: 0.9995 - val_precision: 0.8871 - val_recall: 0.7746 - val_auc: 0.9434
Epoch 14/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0042 - tp: 190.0000 - fp: 23.0000 - tn: 181934.0000 - fn: 129.0000 - accuracy: 0.9992 - precision: 0.8920 - recall: 0.5956 - auc: 0.9190 - val_loss: 0.0026 - val_tp: 56.0000 - val_fp: 8.0000 - val_tn: 45490.0000 - val_fn: 15.0000 - val_accuracy: 0.9995 - val_precision: 0.8750 - val_recall: 0.7887 - val_auc: 0.9434
Epoch 15/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0044 - tp: 199.0000 - fp: 26.0000 - tn: 181931.0000 - fn: 120.0000 - accuracy: 0.9992 - precision: 0.8844 - recall: 0.6238 - auc: 0.9159 - val_loss: 0.0026 - val_tp: 55.0000 - val_fp: 7.0000 - val_tn: 45491.0000 - val_fn: 16.0000 - val_accuracy: 0.9995 - val_precision: 0.8871 - val_recall: 0.7746 - val_auc: 0.9434
Epoch 16/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0045 - tp: 188.0000 - fp: 29.0000 - tn: 181928.0000 - fn: 131.0000 - accuracy: 0.9991 - precision: 0.8664 - recall: 0.5893 - auc: 0.9095 - val_loss: 0.0025 - val_tp: 57.0000 - val_fp: 7.0000 - val_tn: 45491.0000 - val_fn: 14.0000 - val_accuracy: 0.9995 - val_precision: 0.8906 - val_recall: 0.8028 - val_auc: 0.9504
Epoch 17/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0042 - tp: 194.0000 - fp: 32.0000 - tn: 181925.0000 - fn: 125.0000 - accuracy: 0.9991 - precision: 0.8584 - recall: 0.6082 - auc: 0.9222 - val_loss: 0.0025 - val_tp: 55.0000 - val_fp: 7.0000 - val_tn: 45491.0000 - val_fn: 16.0000 - val_accuracy: 0.9995 - val_precision: 0.8871 - val_recall: 0.7746 - val_auc: 0.9504
Epoch 18/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0044 - tp: 201.0000 - fp: 29.0000 - tn: 181928.0000 - fn: 118.0000 - accuracy: 0.9992 - precision: 0.8739 - recall: 0.6301 - auc: 0.9206 - val_loss: 0.0024 - val_tp: 54.0000 - val_fp: 7.0000 - val_tn: 45491.0000 - val_fn: 17.0000 - val_accuracy: 0.9995 - val_precision: 0.8852 - val_recall: 0.7606 - val_auc: 0.9504
Epoch 19/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0043 - tp: 188.0000 - fp: 28.0000 - tn: 181929.0000 - fn: 131.0000 - accuracy: 0.9991 - precision: 0.8704 - recall: 0.5893 - auc: 0.9097 - val_loss: 0.0024 - val_tp: 55.0000 - val_fp: 7.0000 - val_tn: 45491.0000 - val_fn: 16.0000 - val_accuracy: 0.9995 - val_precision: 0.8871 - val_recall: 0.7746 - val_auc: 0.9504
Epoch 20/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0039 - tp: 206.0000 - fp: 25.0000 - tn: 181932.0000 - fn: 113.0000 - accuracy: 0.9992 - precision: 0.8918 - recall: 0.6458 - auc: 0.9192 - val_loss: 0.0024 - val_tp: 56.0000 - val_fp: 7.0000 - val_tn: 45491.0000 - val_fn: 15.0000 - val_accuracy: 0.9995 - val_precision: 0.8889 - val_recall: 0.7887 - val_auc: 0.9504
Epoch 21/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0044 - tp: 184.0000 - fp: 33.0000 - tn: 181924.0000 - fn: 135.0000 - accuracy: 0.9991 - precision: 0.8479 - recall: 0.5768 - auc: 0.9207 - val_loss: 0.0024 - val_tp: 56.0000 - val_fp: 7.0000 - val_tn: 45491.0000 - val_fn: 15.0000 - val_accuracy: 0.9995 - val_precision: 0.8889 - val_recall: 0.7887 - val_auc: 0.9434
Epoch 22/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0040 - tp: 206.0000 - fp: 28.0000 - tn: 181929.0000 - fn: 113.0000 - accuracy: 0.9992 - precision: 0.8803 - recall: 0.6458 - auc: 0.9223 - val_loss: 0.0024 - val_tp: 57.0000 - val_fp: 7.0000 - val_tn: 45491.0000 - val_fn: 14.0000 - val_accuracy: 0.9995 - val_precision: 0.8906 - val_recall: 0.8028 - val_auc: 0.9434
Epoch 23/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0039 - tp: 203.0000 - fp: 31.0000 - tn: 181926.0000 - fn: 116.0000 - accuracy: 0.9992 - precision: 0.8675 - recall: 0.6364 - auc: 0.9318 - val_loss: 0.0023 - val_tp: 53.0000 - val_fp: 7.0000 - val_tn: 45491.0000 - val_fn: 18.0000 - val_accuracy: 0.9995 - val_precision: 0.8833 - val_recall: 0.7465 - val_auc: 0.9434
Epoch 24/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0041 - tp: 201.0000 - fp: 27.0000 - tn: 181930.0000 - fn: 118.0000 - accuracy: 0.9992 - precision: 0.8816 - recall: 0.6301 - auc: 0.9160 - val_loss: 0.0023 - val_tp: 57.0000 - val_fp: 7.0000 - val_tn: 45491.0000 - val_fn: 14.0000 - val_accuracy: 0.9995 - val_precision: 0.8906 - val_recall: 0.8028 - val_auc: 0.9575
Epoch 25/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0040 - tp: 186.0000 - fp: 30.0000 - tn: 181927.0000 - fn: 133.0000 - accuracy: 0.9991 - precision: 0.8611 - recall: 0.5831 - auc: 0.9333 - val_loss: 0.0023 - val_tp: 58.0000 - val_fp: 7.0000 - val_tn: 45491.0000 - val_fn: 13.0000 - val_accuracy: 0.9996 - val_precision: 0.8923 - val_recall: 0.8169 - val_auc: 0.9574
Epoch 26/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0038 - tp: 202.0000 - fp: 24.0000 - tn: 181933.0000 - fn: 117.0000 - accuracy: 0.9992 - precision: 0.8938 - recall: 0.6332 - auc: 0.9271 - val_loss: 0.0023 - val_tp: 58.0000 - val_fp: 7.0000 - val_tn: 45491.0000 - val_fn: 13.0000 - val_accuracy: 0.9996 - val_precision: 0.8923 - val_recall: 0.8169 - val_auc: 0.9575
Epoch 27/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0038 - tp: 207.0000 - fp: 33.0000 - tn: 181924.0000 - fn: 112.0000 - accuracy: 0.9992 - precision: 0.8625 - recall: 0.6489 - auc: 0.9271 - val_loss: 0.0023 - val_tp: 58.0000 - val_fp: 7.0000 - val_tn: 45491.0000 - val_fn: 13.0000 - val_accuracy: 0.9996 - val_precision: 0.8923 - val_recall: 0.8169 - val_auc: 0.9575
Epoch 28/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0040 - tp: 201.0000 - fp: 27.0000 - tn: 181930.0000 - fn: 118.0000 - accuracy: 0.9992 - precision: 0.8816 - recall: 0.6301 - auc: 0.9145 - val_loss: 0.0023 - val_tp: 58.0000 - val_fp: 7.0000 - val_tn: 45491.0000 - val_fn: 13.0000 - val_accuracy: 0.9996 - val_precision: 0.8923 - val_recall: 0.8169 - val_auc: 0.9575
Epoch 29/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0037 - tp: 210.0000 - fp: 22.0000 - tn: 181935.0000 - fn: 109.0000 - accuracy: 0.9993 - precision: 0.9052 - recall: 0.6583 - auc: 0.9318 - val_loss: 0.0023 - val_tp: 59.0000 - val_fp: 7.0000 - val_tn: 45491.0000 - val_fn: 12.0000 - val_accuracy: 0.9996 - val_precision: 0.8939 - val_recall: 0.8310 - val_auc: 0.9575
Epoch 30/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0040 - tp: 196.0000 - fp: 29.0000 - tn: 181928.0000 - fn: 123.0000 - accuracy: 0.9992 - precision: 0.8711 - recall: 0.6144 - auc: 0.9301 - val_loss: 0.0023 - val_tp: 58.0000 - val_fp: 7.0000 - val_tn: 45491.0000 - val_fn: 13.0000 - val_accuracy: 0.9996 - val_precision: 0.8923 - val_recall: 0.8169 - val_auc: 0.9575
Epoch 31/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0042 - tp: 193.0000 - fp: 31.0000 - tn: 181926.0000 - fn: 126.0000 - accuracy: 0.9991 - precision: 0.8616 - recall: 0.6050 - auc: 0.9270 - val_loss: 0.0022 - val_tp: 55.0000 - val_fp: 6.0000 - val_tn: 45492.0000 - val_fn: 16.0000 - val_accuracy: 0.9995 - val_precision: 0.9016 - val_recall: 0.7746 - val_auc: 0.9575
Epoch 32/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0039 - tp: 193.0000 - fp: 30.0000 - tn: 181927.0000 - fn: 126.0000 - accuracy: 0.9991 - precision: 0.8655 - recall: 0.6050 - auc: 0.9318 - val_loss: 0.0022 - val_tp: 57.0000 - val_fp: 7.0000 - val_tn: 45491.0000 - val_fn: 14.0000 - val_accuracy: 0.9995 - val_precision: 0.8906 - val_recall: 0.8028 - val_auc: 0.9575
Epoch 33/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0038 - tp: 203.0000 - fp: 28.0000 - tn: 181929.0000 - fn: 116.0000 - accuracy: 0.9992 - precision: 0.8788 - recall: 0.6364 - auc: 0.9287 - val_loss: 0.0022 - val_tp: 57.0000 - val_fp: 7.0000 - val_tn: 45491.0000 - val_fn: 14.0000 - val_accuracy: 0.9995 - val_precision: 0.8906 - val_recall: 0.8028 - val_auc: 0.9575
Epoch 34/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0041 - tp: 191.0000 - fp: 29.0000 - tn: 181928.0000 - fn: 128.0000 - accuracy: 0.9991 - precision: 0.8682 - recall: 0.5987 - auc: 0.9301 - val_loss: 0.0022 - val_tp: 58.0000 - val_fp: 8.0000 - val_tn: 45490.0000 - val_fn: 13.0000 - val_accuracy: 0.9995 - val_precision: 0.8788 - val_recall: 0.8169 - val_auc: 0.9574
Epoch 35/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0038 - tp: 201.0000 - fp: 32.0000 - tn: 181925.0000 - fn: 118.0000 - accuracy: 0.9992 - precision: 0.8627 - recall: 0.6301 - auc: 0.9270 - val_loss: 0.0022 - val_tp: 56.0000 - val_fp: 6.0000 - val_tn: 45492.0000 - val_fn: 15.0000 - val_accuracy: 0.9995 - val_precision: 0.9032 - val_recall: 0.7887 - val_auc: 0.9505
Epoch 36/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0036 - tp: 209.0000 - fp: 25.0000 - tn: 181932.0000 - fn: 110.0000 - accuracy: 0.9993 - precision: 0.8932 - recall: 0.6552 - auc: 0.9288 - val_loss: 0.0022 - val_tp: 58.0000 - val_fp: 7.0000 - val_tn: 45491.0000 - val_fn: 13.0000 - val_accuracy: 0.9996 - val_precision: 0.8923 - val_recall: 0.8169 - val_auc: 0.9575
Epoch 37/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0040 - tp: 196.0000 - fp: 29.0000 - tn: 181928.0000 - fn: 123.0000 - accuracy: 0.9992 - precision: 0.8711 - recall: 0.6144 - auc: 0.9255 - val_loss: 0.0022 - val_tp: 57.0000 - val_fp: 7.0000 - val_tn: 45491.0000 - val_fn: 14.0000 - val_accuracy: 0.9995 - val_precision: 0.8906 - val_recall: 0.8028 - val_auc: 0.9575
Epoch 38/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0038 - tp: 206.0000 - fp: 25.0000 - tn: 181932.0000 - fn: 113.0000 - accuracy: 0.9992 - precision: 0.8918 - recall: 0.6458 - auc: 0.9365 - val_loss: 0.0022 - val_tp: 57.0000 - val_fp: 6.0000 - val_tn: 45492.0000 - val_fn: 14.0000 - val_accuracy: 0.9996 - val_precision: 0.9048 - val_recall: 0.8028 - val_auc: 0.9575
Epoch 39/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0039 - tp: 193.0000 - fp: 33.0000 - tn: 181924.0000 - fn: 126.0000 - accuracy: 0.9991 - precision: 0.8540 - recall: 0.6050 - auc: 0.9381 - val_loss: 0.0022 - val_tp: 55.0000 - val_fp: 7.0000 - val_tn: 45491.0000 - val_fn: 16.0000 - val_accuracy: 0.9995 - val_precision: 0.8871 - val_recall: 0.7746 - val_auc: 0.9504
Epoch 40/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0035 - tp: 206.0000 - fp: 28.0000 - tn: 181929.0000 - fn: 113.0000 - accuracy: 0.9992 - precision: 0.8803 - recall: 0.6458 - auc: 0.9382 - val_loss: 0.0022 - val_tp: 54.0000 - val_fp: 6.0000 - val_tn: 45492.0000 - val_fn: 17.0000 - val_accuracy: 0.9995 - val_precision: 0.9000 - val_recall: 0.7606 - val_auc: 0.9575
Epoch 41/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0038 - tp: 201.0000 - fp: 30.0000 - tn: 181927.0000 - fn: 118.0000 - accuracy: 0.9992 - precision: 0.8701 - recall: 0.6301 - auc: 0.9318 - val_loss: 0.0022 - val_tp: 56.0000 - val_fp: 7.0000 - val_tn: 45491.0000 - val_fn: 15.0000 - val_accuracy: 0.9995 - val_precision: 0.8889 - val_recall: 0.7887 - val_auc: 0.9575
Epoch 42/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0035 - tp: 218.0000 - fp: 28.0000 - tn: 181929.0000 - fn: 101.0000 - accuracy: 0.9993 - precision: 0.8862 - recall: 0.6834 - auc: 0.9318 - val_loss: 0.0022 - val_tp: 52.0000 - val_fp: 6.0000 - val_tn: 45492.0000 - val_fn: 19.0000 - val_accuracy: 0.9995 - val_precision: 0.8966 - val_recall: 0.7324 - val_auc: 0.9575
Epoch 43/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0038 - tp: 189.0000 - fp: 28.0000 - tn: 181929.0000 - fn: 130.0000 - accuracy: 0.9991 - precision: 0.8710 - recall: 0.5925 - auc: 0.9334 - val_loss: 0.0022 - val_tp: 59.0000 - val_fp: 8.0000 - val_tn: 45490.0000 - val_fn: 12.0000 - val_accuracy: 0.9996 - val_precision: 0.8806 - val_recall: 0.8310 - val_auc: 0.9575
Epoch 44/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0038 - tp: 203.0000 - fp: 30.0000 - tn: 181927.0000 - fn: 116.0000 - accuracy: 0.9992 - precision: 0.8712 - recall: 0.6364 - auc: 0.9287 - val_loss: 0.0022 - val_tp: 52.0000 - val_fp: 6.0000 - val_tn: 45492.0000 - val_fn: 19.0000 - val_accuracy: 0.9995 - val_precision: 0.8966 - val_recall: 0.7324 - val_auc: 0.9505
Epoch 45/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0037 - tp: 203.0000 - fp: 24.0000 - tn: 181933.0000 - fn: 116.0000 - accuracy: 0.9992 - precision: 0.8943 - recall: 0.6364 - auc: 0.9365 - val_loss: 0.0022 - val_tp: 52.0000 - val_fp: 6.0000 - val_tn: 45492.0000 - val_fn: 19.0000 - val_accuracy: 0.9995 - val_precision: 0.8966 - val_recall: 0.7324 - val_auc: 0.9575
Epoch 46/100
182272/182276 [============================>.] - ETA: 0s - loss: 0.0037 - tp: 207.0000 - fp: 28.0000 - tn: 181925.0000 - fn: 112.0000 - accuracy: 0.9992 - precision: 0.8809 - recall: 0.6489 - auc: 0.9303Restoring model weights from the end of the best epoch.
182276/182276 [==============================] - 1s 3us/sample - loss: 0.0037 - tp: 207.0000 - fp: 28.0000 - tn: 181929.0000 - fn: 112.0000 - accuracy: 0.9992 - precision: 0.8809 - recall: 0.6489 - auc: 0.9303 - val_loss: 0.0022 - val_tp: 53.0000 - val_fp: 6.0000 - val_tn: 45492.0000 - val_fn: 18.0000 - val_accuracy: 0.9995 - val_precision: 0.8983 - val_recall: 0.7465 - val_auc: 0.9575
Epoch 00046: early stopping

Check training history

In this section, you will produce plots of your model's accuracy and loss on the training and validation set. These are useful to check for overfitting, which you can learn more about in this tutorial.

Additionally, you can produce these plots for any of the metrics you created above. False negatives are included as an example.

def plot_metrics(history):
  metrics =  ['loss', 'auc', 'precision', 'recall']
  for n, metric in enumerate(metrics):
    name = metric.replace("_"," ").capitalize()
    plt.subplot(2,2,n+1)
    plt.plot(history.epoch,  history.history[metric], color=colors[0], label='Train')
    plt.plot(history.epoch, history.history['val_'+metric],
             color=colors[0], linestyle="--", label='Val')
    plt.xlabel('Epoch')
    plt.ylabel(name)
    if metric == 'loss':
      plt.ylim([0, plt.ylim()[1]])
    elif metric == 'auc':
      plt.ylim([0.8,1])
    else:
      plt.ylim([0,1])

    plt.legend()

plot_metrics(baseline_history)

png

Evaluate metrics

You can use a confusion matrix to summarize the actual vs. predicted labels where the X axis is the predicted label and the Y axis is the actual label.

train_predictions_baseline = model.predict(train_features, batch_size=BATCH_SIZE)
test_predictions_baseline = model.predict(test_features, batch_size=BATCH_SIZE)
def plot_cm(labels, predictions, p=0.5):
  cm = confusion_matrix(labels, predictions > p)
  plt.figure(figsize=(5,5))
  sns.heatmap(cm, annot=True, fmt="d")
  plt.title('Confusion matrix @{:.2f}'.format(p))
  plt.ylabel('Actual label')
  plt.xlabel('Predicted label')

  print('Legitimate Transactions Detected (True Negatives): ', cm[0][0])
  print('Legitimate Transactions Incorrectly Detected (False Positives): ', cm[0][1])
  print('Fraudulent Transactions Missed (False Negatives): ', cm[1][0])
  print('Fraudulent Transactions Detected (True Positives): ', cm[1][1])
  print('Total Fraudulent Transactions: ', np.sum(cm[1]))

Evaluate your model on the test dataset and display the results for the metrics you created above.

baseline_results = model.evaluate(test_features, test_labels,
                                  batch_size=BATCH_SIZE, verbose=0)
for name, value in zip(model.metrics_names, baseline_results):
  print(name, ': ', value)
print()

plot_cm(test_labels, test_predictions_baseline)
loss :  0.0033584331309548607
tp :  80.0
fp :  13.0
tn :  56847.0
fn :  22.0
accuracy :  0.99938554
precision :  0.86021507
recall :  0.78431374
auc :  0.9309929

Legitimate Transactions Detected (True Negatives):  56847
Legitimate Transactions Incorrectly Detected (False Positives):  13
Fraudulent Transactions Missed (False Negatives):  22
Fraudulent Transactions Detected (True Positives):  80
Total Fraudulent Transactions:  102

png

If the model had predicted everything perfectly, this would be a diagonal matrix where values off the main diagonal, indicating incorrect predictions, would be zero. In this case the matrix shows that you have relatively few false positives, meaning that there were relatively few legitimate transactions that were incorrectly flagged. However, you would likely want to have even fewer false negatives despite the cost of increasing the number of false positives. This trade off may be preferable because false negatives would allow fraudulent transactions to go through, whereas false positives may cause an email to be sent to a customer to ask them to verify their card activity.

Plot the ROC

Now plot the ROC. This plot is useful because it shows, at a glance, the range of performance the model can reach just by tuning the output threshold.

def plot_roc(name, labels, predictions, **kwargs):
  fp, tp, _ = sklearn.metrics.roc_curve(labels, predictions)

  plt.plot(100*fp, 100*tp, label=name, linewidth=2, **kwargs)
  plt.xlabel('False positives [%]')
  plt.ylabel('True positives [%]')
  plt.xlim([-0.5,20])
  plt.ylim([80,100.5])
  plt.grid(True)
  ax = plt.gca()
  ax.set_aspect('equal')
plot_roc("Train Baseline", train_labels, train_predictions_baseline, color=colors[0])
plot_roc("Test Baseline", test_labels, test_predictions_baseline, color=colors[0], linestyle='--')
plt.legend(loc='lower right')
<matplotlib.legend.Legend at 0x7f94b42d6e80>

png

It looks like the precision is relatively high, but the recall and the area under the ROC curve (AUC) aren't as high as you might like. Classifiers often face challenges when trying to maximize both precision and recall, which is especially true when working with imbalanced datasets. It is important to consider the costs of different types of errors in the context of the problem you care about. In this example, a false negative (a fraudulent transaction is missed) may have a financial cost, while a false positive (a transaction is incorrectly flagged as fraudulent) may decrease user happiness.

Class weights

Calculate class weights

The goal is to identify fradulent transactions, but you don't have very many of those positive samples to work with, so you would want to have the classifier heavily weight the few examples that are available. You can do this by passing Keras weights for each class through a parameter. These will cause the model to "pay more attention" to examples from an under-represented class.

# Scaling by total/2 helps keep the loss to a similar magnitude.
# The sum of the weights of all examples stays the same.
weight_for_0 = (1 / neg)*(total)/2.0 
weight_for_1 = (1 / pos)*(total)/2.0

class_weight = {0: weight_for_0, 1: weight_for_1}

print('Weight for class 0: {:.2f}'.format(weight_for_0))
print('Weight for class 1: {:.2f}'.format(weight_for_1))
Weight for class 0: 0.50
Weight for class 1: 289.44

Train a model with class weights

Now try re-training and evaluating the model with class weights to see how that affects the predictions.

weighted_model = make_model()
weighted_model.load_weights(initial_weights)

weighted_history = weighted_model.fit(
    train_features,
    train_labels,
    batch_size=BATCH_SIZE,
    epochs=EPOCHS,
    callbacks = [early_stopping],
    validation_data=(val_features, val_labels),
    # The class weights go here
    class_weight=class_weight) 
WARNING:tensorflow:sample_weight modes were coerced from
  ...
    to  
  ['...']
WARNING:tensorflow:sample_weight modes were coerced from
  ...
    to  
  ['...']
Train on 182276 samples, validate on 45569 samples
Epoch 1/100
182276/182276 [==============================] - 3s 15us/sample - loss: 2.8163 - tp: 50.0000 - fp: 78.0000 - tn: 181879.0000 - fn: 269.0000 - accuracy: 0.9981 - precision: 0.3906 - recall: 0.1567 - auc: 0.6928 - val_loss: 0.8991 - val_tp: 37.0000 - val_fp: 9.0000 - val_tn: 45489.0000 - val_fn: 34.0000 - val_accuracy: 0.9991 - val_precision: 0.8043 - val_recall: 0.5211 - val_auc: 0.9039
Epoch 2/100
182276/182276 [==============================] - 1s 3us/sample - loss: 1.2110 - tp: 164.0000 - fp: 277.0000 - tn: 181680.0000 - fn: 155.0000 - accuracy: 0.9976 - precision: 0.3719 - recall: 0.5141 - auc: 0.8466 - val_loss: 0.5250 - val_tp: 56.0000 - val_fp: 10.0000 - val_tn: 45488.0000 - val_fn: 15.0000 - val_accuracy: 0.9995 - val_precision: 0.8485 - val_recall: 0.7887 - val_auc: 0.9025
Epoch 3/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.8136 - tp: 199.0000 - fp: 634.0000 - tn: 181323.0000 - fn: 120.0000 - accuracy: 0.9959 - precision: 0.2389 - recall: 0.6238 - auc: 0.8969 - val_loss: 0.4336 - val_tp: 58.0000 - val_fp: 21.0000 - val_tn: 45477.0000 - val_fn: 13.0000 - val_accuracy: 0.9993 - val_precision: 0.7342 - val_recall: 0.8169 - val_auc: 0.9235
Epoch 4/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.6813 - tp: 222.0000 - fp: 1146.0000 - tn: 180811.0000 - fn: 97.0000 - accuracy: 0.9932 - precision: 0.1623 - recall: 0.6959 - auc: 0.9056 - val_loss: 0.3864 - val_tp: 59.0000 - val_fp: 32.0000 - val_tn: 45466.0000 - val_fn: 12.0000 - val_accuracy: 0.9990 - val_precision: 0.6484 - val_recall: 0.8310 - val_auc: 0.9348
Epoch 5/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.6028 - tp: 237.0000 - fp: 1701.0000 - tn: 180256.0000 - fn: 82.0000 - accuracy: 0.9902 - precision: 0.1223 - recall: 0.7429 - auc: 0.9107 - val_loss: 0.3419 - val_tp: 59.0000 - val_fp: 61.0000 - val_tn: 45437.0000 - val_fn: 12.0000 - val_accuracy: 0.9984 - val_precision: 0.4917 - val_recall: 0.8310 - val_auc: 0.9427
Epoch 6/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.5403 - tp: 243.0000 - fp: 2346.0000 - tn: 179611.0000 - fn: 76.0000 - accuracy: 0.9867 - precision: 0.0939 - recall: 0.7618 - auc: 0.9108 - val_loss: 0.2976 - val_tp: 60.0000 - val_fp: 100.0000 - val_tn: 45398.0000 - val_fn: 11.0000 - val_accuracy: 0.9976 - val_precision: 0.3750 - val_recall: 0.8451 - val_auc: 0.9457
Epoch 7/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.4739 - tp: 250.0000 - fp: 3037.0000 - tn: 178920.0000 - fn: 69.0000 - accuracy: 0.9830 - precision: 0.0761 - recall: 0.7837 - auc: 0.9257 - val_loss: 0.2650 - val_tp: 60.0000 - val_fp: 151.0000 - val_tn: 45347.0000 - val_fn: 11.0000 - val_accuracy: 0.9964 - val_precision: 0.2844 - val_recall: 0.8451 - val_auc: 0.9519
Epoch 8/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.3824 - tp: 259.0000 - fp: 3898.0000 - tn: 178059.0000 - fn: 60.0000 - accuracy: 0.9783 - precision: 0.0623 - recall: 0.8119 - auc: 0.9385 - val_loss: 0.2376 - val_tp: 61.0000 - val_fp: 199.0000 - val_tn: 45299.0000 - val_fn: 10.0000 - val_accuracy: 0.9954 - val_precision: 0.2346 - val_recall: 0.8592 - val_auc: 0.9562
Epoch 9/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.3480 - tp: 267.0000 - fp: 4566.0000 - tn: 177391.0000 - fn: 52.0000 - accuracy: 0.9747 - precision: 0.0552 - recall: 0.8370 - auc: 0.9469 - val_loss: 0.2142 - val_tp: 63.0000 - val_fp: 299.0000 - val_tn: 45199.0000 - val_fn: 8.0000 - val_accuracy: 0.9933 - val_precision: 0.1740 - val_recall: 0.8873 - val_auc: 0.9621
Epoch 10/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.3761 - tp: 269.0000 - fp: 5310.0000 - tn: 176647.0000 - fn: 50.0000 - accuracy: 0.9706 - precision: 0.0482 - recall: 0.8433 - auc: 0.9340 - val_loss: 0.1977 - val_tp: 63.0000 - val_fp: 392.0000 - val_tn: 45106.0000 - val_fn: 8.0000 - val_accuracy: 0.9912 - val_precision: 0.1385 - val_recall: 0.8873 - val_auc: 0.9676
Epoch 11/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.3576 - tp: 267.0000 - fp: 5866.0000 - tn: 176091.0000 - fn: 52.0000 - accuracy: 0.9675 - precision: 0.0435 - recall: 0.8370 - auc: 0.9438 - val_loss: 0.1825 - val_tp: 64.0000 - val_fp: 521.0000 - val_tn: 44977.0000 - val_fn: 7.0000 - val_accuracy: 0.9884 - val_precision: 0.1094 - val_recall: 0.9014 - val_auc: 0.9713
Epoch 12/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.2967 - tp: 275.0000 - fp: 6378.0000 - tn: 175579.0000 - fn: 44.0000 - accuracy: 0.9648 - precision: 0.0413 - recall: 0.8621 - auc: 0.9549 - val_loss: 0.1708 - val_tp: 64.0000 - val_fp: 666.0000 - val_tn: 44832.0000 - val_fn: 7.0000 - val_accuracy: 0.9852 - val_precision: 0.0877 - val_recall: 0.9014 - val_auc: 0.9738
Epoch 13/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.3398 - tp: 264.0000 - fp: 7050.0000 - tn: 174907.0000 - fn: 55.0000 - accuracy: 0.9610 - precision: 0.0361 - recall: 0.8276 - auc: 0.9458 - val_loss: 0.1590 - val_tp: 64.0000 - val_fp: 809.0000 - val_tn: 44689.0000 - val_fn: 7.0000 - val_accuracy: 0.9821 - val_precision: 0.0733 - val_recall: 0.9014 - val_auc: 0.9793
Epoch 14/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.3061 - tp: 274.0000 - fp: 7231.0000 - tn: 174726.0000 - fn: 45.0000 - accuracy: 0.9601 - precision: 0.0365 - recall: 0.8589 - auc: 0.9527 - val_loss: 0.1526 - val_tp: 64.0000 - val_fp: 867.0000 - val_tn: 44631.0000 - val_fn: 7.0000 - val_accuracy: 0.9808 - val_precision: 0.0687 - val_recall: 0.9014 - val_auc: 0.9801
Epoch 15/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.2507 - tp: 282.0000 - fp: 7411.0000 - tn: 174546.0000 - fn: 37.0000 - accuracy: 0.9591 - precision: 0.0367 - recall: 0.8840 - auc: 0.9626 - val_loss: 0.1484 - val_tp: 65.0000 - val_fp: 873.0000 - val_tn: 44625.0000 - val_fn: 6.0000 - val_accuracy: 0.9807 - val_precision: 0.0693 - val_recall: 0.9155 - val_auc: 0.9822
Epoch 16/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.2954 - tp: 274.0000 - fp: 7533.0000 - tn: 174424.0000 - fn: 45.0000 - accuracy: 0.9584 - precision: 0.0351 - recall: 0.8589 - auc: 0.9543 - val_loss: 0.1411 - val_tp: 65.0000 - val_fp: 942.0000 - val_tn: 44556.0000 - val_fn: 6.0000 - val_accuracy: 0.9792 - val_precision: 0.0645 - val_recall: 0.9155 - val_auc: 0.9843
Epoch 17/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.2805 - tp: 280.0000 - fp: 7678.0000 - tn: 174279.0000 - fn: 39.0000 - accuracy: 0.9577 - precision: 0.0352 - recall: 0.8777 - auc: 0.9541 - val_loss: 0.1388 - val_tp: 65.0000 - val_fp: 939.0000 - val_tn: 44559.0000 - val_fn: 6.0000 - val_accuracy: 0.9793 - val_precision: 0.0647 - val_recall: 0.9155 - val_auc: 0.9858
Epoch 18/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.2654 - tp: 280.0000 - fp: 7731.0000 - tn: 174226.0000 - fn: 39.0000 - accuracy: 0.9574 - precision: 0.0350 - recall: 0.8777 - auc: 0.9603 - val_loss: 0.1345 - val_tp: 65.0000 - val_fp: 950.0000 - val_tn: 44548.0000 - val_fn: 6.0000 - val_accuracy: 0.9790 - val_precision: 0.0640 - val_recall: 0.9155 - val_auc: 0.9864
Epoch 19/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.2688 - tp: 277.0000 - fp: 7455.0000 - tn: 174502.0000 - fn: 42.0000 - accuracy: 0.9589 - precision: 0.0358 - recall: 0.8683 - auc: 0.9601 - val_loss: 0.1304 - val_tp: 66.0000 - val_fp: 948.0000 - val_tn: 44550.0000 - val_fn: 5.0000 - val_accuracy: 0.9791 - val_precision: 0.0651 - val_recall: 0.9296 - val_auc: 0.9881
Epoch 20/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.2534 - tp: 282.0000 - fp: 7450.0000 - tn: 174507.0000 - fn: 37.0000 - accuracy: 0.9589 - precision: 0.0365 - recall: 0.8840 - auc: 0.9637 - val_loss: 0.1286 - val_tp: 66.0000 - val_fp: 942.0000 - val_tn: 44556.0000 - val_fn: 5.0000 - val_accuracy: 0.9792 - val_precision: 0.0655 - val_recall: 0.9296 - val_auc: 0.9885
Epoch 21/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.2354 - tp: 287.0000 - fp: 7348.0000 - tn: 174609.0000 - fn: 32.0000 - accuracy: 0.9595 - precision: 0.0376 - recall: 0.8997 - auc: 0.9649 - val_loss: 0.1270 - val_tp: 66.0000 - val_fp: 933.0000 - val_tn: 44565.0000 - val_fn: 5.0000 - val_accuracy: 0.9794 - val_precision: 0.0661 - val_recall: 0.9296 - val_auc: 0.9889
Epoch 22/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.2474 - tp: 279.0000 - fp: 7169.0000 - tn: 174788.0000 - fn: 40.0000 - accuracy: 0.9605 - precision: 0.0375 - recall: 0.8746 - auc: 0.9628 - val_loss: 0.1255 - val_tp: 66.0000 - val_fp: 951.0000 - val_tn: 44547.0000 - val_fn: 5.0000 - val_accuracy: 0.9790 - val_precision: 0.0649 - val_recall: 0.9296 - val_auc: 0.9888
Epoch 23/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.2509 - tp: 283.0000 - fp: 7381.0000 - tn: 174576.0000 - fn: 36.0000 - accuracy: 0.9593 - precision: 0.0369 - recall: 0.8871 - auc: 0.9630 - val_loss: 0.1242 - val_tp: 66.0000 - val_fp: 956.0000 - val_tn: 44542.0000 - val_fn: 5.0000 - val_accuracy: 0.9789 - val_precision: 0.0646 - val_recall: 0.9296 - val_auc: 0.9898
Epoch 24/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.2356 - tp: 281.0000 - fp: 7303.0000 - tn: 174654.0000 - fn: 38.0000 - accuracy: 0.9597 - precision: 0.0371 - recall: 0.8809 - auc: 0.9697 - val_loss: 0.1234 - val_tp: 66.0000 - val_fp: 972.0000 - val_tn: 44526.0000 - val_fn: 5.0000 - val_accuracy: 0.9786 - val_precision: 0.0636 - val_recall: 0.9296 - val_auc: 0.9898
Epoch 25/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.2258 - tp: 288.0000 - fp: 7017.0000 - tn: 174940.0000 - fn: 31.0000 - accuracy: 0.9613 - precision: 0.0394 - recall: 0.9028 - auc: 0.9661 - val_loss: 0.1223 - val_tp: 66.0000 - val_fp: 908.0000 - val_tn: 44590.0000 - val_fn: 5.0000 - val_accuracy: 0.9800 - val_precision: 0.0678 - val_recall: 0.9296 - val_auc: 0.9898
Epoch 26/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.2378 - tp: 283.0000 - fp: 7238.0000 - tn: 174719.0000 - fn: 36.0000 - accuracy: 0.9601 - precision: 0.0376 - recall: 0.8871 - auc: 0.9663 - val_loss: 0.1212 - val_tp: 66.0000 - val_fp: 897.0000 - val_tn: 44601.0000 - val_fn: 5.0000 - val_accuracy: 0.9802 - val_precision: 0.0685 - val_recall: 0.9296 - val_auc: 0.9902
Epoch 27/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.2382 - tp: 281.0000 - fp: 7132.0000 - tn: 174825.0000 - fn: 38.0000 - accuracy: 0.9607 - precision: 0.0379 - recall: 0.8809 - auc: 0.9681 - val_loss: 0.1184 - val_tp: 66.0000 - val_fp: 931.0000 - val_tn: 44567.0000 - val_fn: 5.0000 - val_accuracy: 0.9795 - val_precision: 0.0662 - val_recall: 0.9296 - val_auc: 0.9906
Epoch 28/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.2036 - tp: 288.0000 - fp: 7061.0000 - tn: 174896.0000 - fn: 31.0000 - accuracy: 0.9611 - precision: 0.0392 - recall: 0.9028 - auc: 0.9742 - val_loss: 0.1178 - val_tp: 66.0000 - val_fp: 912.0000 - val_tn: 44586.0000 - val_fn: 5.0000 - val_accuracy: 0.9799 - val_precision: 0.0675 - val_recall: 0.9296 - val_auc: 0.9911
Epoch 29/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.2360 - tp: 287.0000 - fp: 6795.0000 - tn: 175162.0000 - fn: 32.0000 - accuracy: 0.9625 - precision: 0.0405 - recall: 0.8997 - auc: 0.9655 - val_loss: 0.1173 - val_tp: 66.0000 - val_fp: 870.0000 - val_tn: 44628.0000 - val_fn: 5.0000 - val_accuracy: 0.9808 - val_precision: 0.0705 - val_recall: 0.9296 - val_auc: 0.9913
Epoch 30/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.2093 - tp: 289.0000 - fp: 6395.0000 - tn: 175562.0000 - fn: 30.0000 - accuracy: 0.9648 - precision: 0.0432 - recall: 0.9060 - auc: 0.9697 - val_loss: 0.1162 - val_tp: 66.0000 - val_fp: 819.0000 - val_tn: 44679.0000 - val_fn: 5.0000 - val_accuracy: 0.9819 - val_precision: 0.0746 - val_recall: 0.9296 - val_auc: 0.9915
Epoch 31/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.2084 - tp: 290.0000 - fp: 6308.0000 - tn: 175649.0000 - fn: 29.0000 - accuracy: 0.9652 - precision: 0.0440 - recall: 0.9091 - auc: 0.9701 - val_loss: 0.1166 - val_tp: 66.0000 - val_fp: 807.0000 - val_tn: 44691.0000 - val_fn: 5.0000 - val_accuracy: 0.9822 - val_precision: 0.0756 - val_recall: 0.9296 - val_auc: 0.9913
Epoch 32/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.2055 - tp: 285.0000 - fp: 6239.0000 - tn: 175718.0000 - fn: 34.0000 - accuracy: 0.9656 - precision: 0.0437 - recall: 0.8934 - auc: 0.9736 - val_loss: 0.1154 - val_tp: 66.0000 - val_fp: 844.0000 - val_tn: 44654.0000 - val_fn: 5.0000 - val_accuracy: 0.9814 - val_precision: 0.0725 - val_recall: 0.9296 - val_auc: 0.9912
Epoch 33/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.2361 - tp: 283.0000 - fp: 6642.0000 - tn: 175315.0000 - fn: 36.0000 - accuracy: 0.9634 - precision: 0.0409 - recall: 0.8871 - auc: 0.9650 - val_loss: 0.1135 - val_tp: 66.0000 - val_fp: 844.0000 - val_tn: 44654.0000 - val_fn: 5.0000 - val_accuracy: 0.9814 - val_precision: 0.0725 - val_recall: 0.9296 - val_auc: 0.9919
Epoch 34/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.2212 - tp: 288.0000 - fp: 6355.0000 - tn: 175602.0000 - fn: 31.0000 - accuracy: 0.9650 - precision: 0.0434 - recall: 0.9028 - auc: 0.9674 - val_loss: 0.1129 - val_tp: 66.0000 - val_fp: 818.0000 - val_tn: 44680.0000 - val_fn: 5.0000 - val_accuracy: 0.9819 - val_precision: 0.0747 - val_recall: 0.9296 - val_auc: 0.9921
Epoch 35/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.2067 - tp: 284.0000 - fp: 6239.0000 - tn: 175718.0000 - fn: 35.0000 - accuracy: 0.9656 - precision: 0.0435 - recall: 0.8903 - auc: 0.9723 - val_loss: 0.1125 - val_tp: 66.0000 - val_fp: 828.0000 - val_tn: 44670.0000 - val_fn: 5.0000 - val_accuracy: 0.9817 - val_precision: 0.0738 - val_recall: 0.9296 - val_auc: 0.9921
Epoch 36/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.2081 - tp: 288.0000 - fp: 6487.0000 - tn: 175470.0000 - fn: 31.0000 - accuracy: 0.9642 - precision: 0.0425 - recall: 0.9028 - auc: 0.9720 - val_loss: 0.1106 - val_tp: 66.0000 - val_fp: 835.0000 - val_tn: 44663.0000 - val_fn: 5.0000 - val_accuracy: 0.9816 - val_precision: 0.0733 - val_recall: 0.9296 - val_auc: 0.9924
Epoch 37/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.2145 - tp: 287.0000 - fp: 6467.0000 - tn: 175490.0000 - fn: 32.0000 - accuracy: 0.9643 - precision: 0.0425 - recall: 0.8997 - auc: 0.9701 - val_loss: 0.1085 - val_tp: 66.0000 - val_fp: 855.0000 - val_tn: 44643.0000 - val_fn: 5.0000 - val_accuracy: 0.9811 - val_precision: 0.0717 - val_recall: 0.9296 - val_auc: 0.9927
Epoch 38/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.1877 - tp: 289.0000 - fp: 6232.0000 - tn: 175725.0000 - fn: 30.0000 - accuracy: 0.9656 - precision: 0.0443 - recall: 0.9060 - auc: 0.9788 - val_loss: 0.1090 - val_tp: 66.0000 - val_fp: 857.0000 - val_tn: 44641.0000 - val_fn: 5.0000 - val_accuracy: 0.9811 - val_precision: 0.0715 - val_recall: 0.9296 - val_auc: 0.9928
Epoch 39/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.1876 - tp: 291.0000 - fp: 6050.0000 - tn: 175907.0000 - fn: 28.0000 - accuracy: 0.9667 - precision: 0.0459 - recall: 0.9122 - auc: 0.9760 - val_loss: 0.1092 - val_tp: 66.0000 - val_fp: 813.0000 - val_tn: 44685.0000 - val_fn: 5.0000 - val_accuracy: 0.9820 - val_precision: 0.0751 - val_recall: 0.9296 - val_auc: 0.9926
Epoch 40/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.2011 - tp: 286.0000 - fp: 6124.0000 - tn: 175833.0000 - fn: 33.0000 - accuracy: 0.9662 - precision: 0.0446 - recall: 0.8966 - auc: 0.9740 - val_loss: 0.1078 - val_tp: 66.0000 - val_fp: 825.0000 - val_tn: 44673.0000 - val_fn: 5.0000 - val_accuracy: 0.9818 - val_precision: 0.0741 - val_recall: 0.9296 - val_auc: 0.9927
Epoch 41/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.1775 - tp: 290.0000 - fp: 5921.0000 - tn: 176036.0000 - fn: 29.0000 - accuracy: 0.9674 - precision: 0.0467 - recall: 0.9091 - auc: 0.9803 - val_loss: 0.1080 - val_tp: 66.0000 - val_fp: 795.0000 - val_tn: 44703.0000 - val_fn: 5.0000 - val_accuracy: 0.9824 - val_precision: 0.0767 - val_recall: 0.9296 - val_auc: 0.9928
Epoch 42/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.1991 - tp: 287.0000 - fp: 6100.0000 - tn: 175857.0000 - fn: 32.0000 - accuracy: 0.9664 - precision: 0.0449 - recall: 0.8997 - auc: 0.9737 - val_loss: 0.1072 - val_tp: 66.0000 - val_fp: 815.0000 - val_tn: 44683.0000 - val_fn: 5.0000 - val_accuracy: 0.9820 - val_precision: 0.0749 - val_recall: 0.9296 - val_auc: 0.9930
Epoch 43/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.2115 - tp: 285.0000 - fp: 6499.0000 - tn: 175458.0000 - fn: 34.0000 - accuracy: 0.9642 - precision: 0.0420 - recall: 0.8934 - auc: 0.9741 - val_loss: 0.1051 - val_tp: 67.0000 - val_fp: 937.0000 - val_tn: 44561.0000 - val_fn: 4.0000 - val_accuracy: 0.9793 - val_precision: 0.0667 - val_recall: 0.9437 - val_auc: 0.9932
Epoch 44/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.1714 - tp: 294.0000 - fp: 6448.0000 - tn: 175509.0000 - fn: 25.0000 - accuracy: 0.9645 - precision: 0.0436 - recall: 0.9216 - auc: 0.9811 - val_loss: 0.1044 - val_tp: 67.0000 - val_fp: 857.0000 - val_tn: 44641.0000 - val_fn: 4.0000 - val_accuracy: 0.9811 - val_precision: 0.0725 - val_recall: 0.9437 - val_auc: 0.9932
Epoch 45/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.1767 - tp: 290.0000 - fp: 6214.0000 - tn: 175743.0000 - fn: 29.0000 - accuracy: 0.9657 - precision: 0.0446 - recall: 0.9091 - auc: 0.9815 - val_loss: 0.1046 - val_tp: 66.0000 - val_fp: 853.0000 - val_tn: 44645.0000 - val_fn: 5.0000 - val_accuracy: 0.9812 - val_precision: 0.0718 - val_recall: 0.9296 - val_auc: 0.9932
Epoch 46/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.1999 - tp: 285.0000 - fp: 5921.0000 - tn: 176036.0000 - fn: 34.0000 - accuracy: 0.9673 - precision: 0.0459 - recall: 0.8934 - auc: 0.9742 - val_loss: 0.1055 - val_tp: 66.0000 - val_fp: 811.0000 - val_tn: 44687.0000 - val_fn: 5.0000 - val_accuracy: 0.9821 - val_precision: 0.0753 - val_recall: 0.9296 - val_auc: 0.9931
Epoch 47/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.2106 - tp: 283.0000 - fp: 6402.0000 - tn: 175555.0000 - fn: 36.0000 - accuracy: 0.9647 - precision: 0.0423 - recall: 0.8871 - auc: 0.9747 - val_loss: 0.1051 - val_tp: 67.0000 - val_fp: 829.0000 - val_tn: 44669.0000 - val_fn: 4.0000 - val_accuracy: 0.9817 - val_precision: 0.0748 - val_recall: 0.9437 - val_auc: 0.9930
Epoch 48/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.1814 - tp: 288.0000 - fp: 6113.0000 - tn: 175844.0000 - fn: 31.0000 - accuracy: 0.9663 - precision: 0.0450 - recall: 0.9028 - auc: 0.9798 - val_loss: 0.1052 - val_tp: 67.0000 - val_fp: 814.0000 - val_tn: 44684.0000 - val_fn: 4.0000 - val_accuracy: 0.9820 - val_precision: 0.0760 - val_recall: 0.9437 - val_auc: 0.9930
Epoch 49/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.1935 - tp: 287.0000 - fp: 6187.0000 - tn: 175770.0000 - fn: 32.0000 - accuracy: 0.9659 - precision: 0.0443 - recall: 0.8997 - auc: 0.9779 - val_loss: 0.1042 - val_tp: 67.0000 - val_fp: 867.0000 - val_tn: 44631.0000 - val_fn: 4.0000 - val_accuracy: 0.9809 - val_precision: 0.0717 - val_recall: 0.9437 - val_auc: 0.9930
Epoch 50/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.1592 - tp: 292.0000 - fp: 6179.0000 - tn: 175778.0000 - fn: 27.0000 - accuracy: 0.9660 - precision: 0.0451 - recall: 0.9154 - auc: 0.9851 - val_loss: 0.1048 - val_tp: 67.0000 - val_fp: 827.0000 - val_tn: 44671.0000 - val_fn: 4.0000 - val_accuracy: 0.9818 - val_precision: 0.0749 - val_recall: 0.9437 - val_auc: 0.9930
Epoch 51/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.1930 - tp: 288.0000 - fp: 6132.0000 - tn: 175825.0000 - fn: 31.0000 - accuracy: 0.9662 - precision: 0.0449 - recall: 0.9028 - auc: 0.9761 - val_loss: 0.1032 - val_tp: 67.0000 - val_fp: 789.0000 - val_tn: 44709.0000 - val_fn: 4.0000 - val_accuracy: 0.9826 - val_precision: 0.0783 - val_recall: 0.9437 - val_auc: 0.9930
Epoch 52/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.1574 - tp: 293.0000 - fp: 6012.0000 - tn: 175945.0000 - fn: 26.0000 - accuracy: 0.9669 - precision: 0.0465 - recall: 0.9185 - auc: 0.9850 - val_loss: 0.1015 - val_tp: 67.0000 - val_fp: 784.0000 - val_tn: 44714.0000 - val_fn: 4.0000 - val_accuracy: 0.9827 - val_precision: 0.0787 - val_recall: 0.9437 - val_auc: 0.9934
Epoch 53/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.1996 - tp: 288.0000 - fp: 5669.0000 - tn: 176288.0000 - fn: 31.0000 - accuracy: 0.9687 - precision: 0.0483 - recall: 0.9028 - auc: 0.9755 - val_loss: 0.1013 - val_tp: 67.0000 - val_fp: 733.0000 - val_tn: 44765.0000 - val_fn: 4.0000 - val_accuracy: 0.9838 - val_precision: 0.0838 - val_recall: 0.9437 - val_auc: 0.9934
Epoch 54/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.1735 - tp: 289.0000 - fp: 5697.0000 - tn: 176260.0000 - fn: 30.0000 - accuracy: 0.9686 - precision: 0.0483 - recall: 0.9060 - auc: 0.9822 - val_loss: 0.1001 - val_tp: 67.0000 - val_fp: 762.0000 - val_tn: 44736.0000 - val_fn: 4.0000 - val_accuracy: 0.9832 - val_precision: 0.0808 - val_recall: 0.9437 - val_auc: 0.9937
Epoch 55/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.1761 - tp: 291.0000 - fp: 5902.0000 - tn: 176055.0000 - fn: 28.0000 - accuracy: 0.9675 - precision: 0.0470 - recall: 0.9122 - auc: 0.9804 - val_loss: 0.0998 - val_tp: 67.0000 - val_fp: 769.0000 - val_tn: 44729.0000 - val_fn: 4.0000 - val_accuracy: 0.9830 - val_precision: 0.0801 - val_recall: 0.9437 - val_auc: 0.9936
Epoch 56/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.1816 - tp: 287.0000 - fp: 6165.0000 - tn: 175792.0000 - fn: 32.0000 - accuracy: 0.9660 - precision: 0.0445 - recall: 0.8997 - auc: 0.9783 - val_loss: 0.1000 - val_tp: 67.0000 - val_fp: 750.0000 - val_tn: 44748.0000 - val_fn: 4.0000 - val_accuracy: 0.9835 - val_precision: 0.0820 - val_recall: 0.9437 - val_auc: 0.9934
Epoch 57/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.1865 - tp: 285.0000 - fp: 5715.0000 - tn: 176242.0000 - fn: 34.0000 - accuracy: 0.9685 - precision: 0.0475 - recall: 0.8934 - auc: 0.9803 - val_loss: 0.1005 - val_tp: 67.0000 - val_fp: 824.0000 - val_tn: 44674.0000 - val_fn: 4.0000 - val_accuracy: 0.9818 - val_precision: 0.0752 - val_recall: 0.9437 - val_auc: 0.9934
Epoch 58/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.1597 - tp: 295.0000 - fp: 6078.0000 - tn: 175879.0000 - fn: 24.0000 - accuracy: 0.9665 - precision: 0.0463 - recall: 0.9248 - auc: 0.9829 - val_loss: 0.1011 - val_tp: 67.0000 - val_fp: 775.0000 - val_tn: 44723.0000 - val_fn: 4.0000 - val_accuracy: 0.9829 - val_precision: 0.0796 - val_recall: 0.9437 - val_auc: 0.9933
Epoch 59/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.1572 - tp: 289.0000 - fp: 5677.0000 - tn: 176280.0000 - fn: 30.0000 - accuracy: 0.9687 - precision: 0.0484 - recall: 0.9060 - auc: 0.9862 - val_loss: 0.1009 - val_tp: 67.0000 - val_fp: 774.0000 - val_tn: 44724.0000 - val_fn: 4.0000 - val_accuracy: 0.9829 - val_precision: 0.0797 - val_recall: 0.9437 - val_auc: 0.9932
Epoch 60/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.1771 - tp: 292.0000 - fp: 5485.0000 - tn: 176472.0000 - fn: 27.0000 - accuracy: 0.9698 - precision: 0.0505 - recall: 0.9154 - auc: 0.9768 - val_loss: 0.1011 - val_tp: 67.0000 - val_fp: 721.0000 - val_tn: 44777.0000 - val_fn: 4.0000 - val_accuracy: 0.9841 - val_precision: 0.0850 - val_recall: 0.9437 - val_auc: 0.9934
Epoch 61/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.1840 - tp: 287.0000 - fp: 5503.0000 - tn: 176454.0000 - fn: 32.0000 - accuracy: 0.9696 - precision: 0.0496 - recall: 0.8997 - auc: 0.9792 - val_loss: 0.1006 - val_tp: 67.0000 - val_fp: 792.0000 - val_tn: 44706.0000 - val_fn: 4.0000 - val_accuracy: 0.9825 - val_precision: 0.0780 - val_recall: 0.9437 - val_auc: 0.9931
Epoch 62/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.1966 - tp: 288.0000 - fp: 5806.0000 - tn: 176151.0000 - fn: 31.0000 - accuracy: 0.9680 - precision: 0.0473 - recall: 0.9028 - auc: 0.9772 - val_loss: 0.0993 - val_tp: 67.0000 - val_fp: 812.0000 - val_tn: 44686.0000 - val_fn: 4.0000 - val_accuracy: 0.9821 - val_precision: 0.0762 - val_recall: 0.9437 - val_auc: 0.9932
Epoch 63/100
182276/182276 [==============================] - 1s 3us/sample - loss: 0.1723 - tp: 289.0000 - fp: 5850.0000 - tn: 176107.0000 - fn: 30.0000 - accuracy: 0.9677 - precision: 0.0471 - recall: 0.9060 - auc: 0.9820 - val_loss: 0.0989 - val_tp: 67.0000 - val_fp: 781.0000 - val_tn: 44717.0000 - val_fn: 4.0000 - val_accuracy: 0.9828 - val_precision: 0.0790 - val_recall: 0.9437 - val_auc: 0.9934
Epoch 64/100
172032/182276 [===========================>..] - ETA: 0s - loss: 0.1812 - tp: 270.0000 - fp: 5273.0000 - tn: 166459.0000 - fn: 30.0000 - accuracy: 0.9692 - precision: 0.0487 - recall: 0.9000 - auc: 0.9816Restoring model weights from the end of the best epoch.
182276/182276 [==============================] - 1s 3us/sample - loss: 0.1826 - tp: 287.0000 - fp: 5609.0000 - tn: 176348.0000 - fn: 32.0000 - accuracy: 0.9691 - precision: 0.0487 - recall: 0.8997 - auc: 0.9808 - val_loss: 0.0977 - val_tp: 67.0000 - val_fp: 800.0000 - val_tn: 44698.0000 - val_fn: 4.0000 - val_accuracy: 0.9824 - val_precision: 0.0773 - val_recall: 0.9437 - val_auc: 0.9934
Epoch 00064: early stopping

Check training history

plot_metrics(weighted_history)

png

Evaluate metrics

train_predictions_weighted = weighted_model.predict(train_features, batch_size=BATCH_SIZE)
test_predictions_weighted = weighted_model.predict(test_features, batch_size=BATCH_SIZE)
weighted_results = weighted_model.evaluate(test_features, test_labels,
                                           batch_size=BATCH_SIZE, verbose=0)
for name, value in zip(weighted_model.metrics_names, weighted_results):
  print(name, ': ', value)
print()

plot_cm(test_labels, test_predictions_weighted)
loss :  0.07764337301026158
tp :  89.0
fp :  1007.0
tn :  55853.0
fn :  13.0
accuracy :  0.98209333
precision :  0.08120438
recall :  0.872549
auc :  0.98502827

Legitimate Transactions Detected (True Negatives):  55853
Legitimate Transactions Incorrectly Detected (False Positives):  1007
Fraudulent Transactions Missed (False Negatives):  13
Fraudulent Transactions Detected (True Positives):  89
Total Fraudulent Transactions:  102

png

Here you can see that with class weights the accuracy and precision are lower because there are more false positives, but conversely the recall and AUC are higher because the model also found more true positives. Despite having lower accuracy, this model has higher recall (and identifies more fraudulent transactions). Of course, there is a cost to both types of error (you wouldn't want to bug users by flagging too many legitimate transactions as fraudulent, either). Carefully consider the trade offs between these different types of errors for your application.

Plot the ROC

plot_roc("Train Baseline", train_labels, train_predictions_baseline, color=colors[0])
plot_roc("Test Baseline", test_labels, test_predictions_baseline, color=colors[0], linestyle='--')

plot_roc("Train Weighted", train_labels, train_predictions_weighted, color=colors[1])
plot_roc("Test Weighted", test_labels, test_predictions_weighted, color=colors[1], linestyle='--')


plt.legend(loc='lower right')
<matplotlib.legend.Legend at 0x7f94745dfb38>

png

Oversampling

Oversample the minority class

A related approach would be to resample the dataset by oversampling the minority class.

pos_features = train_features[bool_train_labels]
neg_features = train_features[~bool_train_labels]

pos_labels = train_labels[bool_train_labels]
neg_labels = train_labels[~bool_train_labels]

Using NumPy

You can balance the dataset manually by choosing the right number of random indices from the positive examples:

ids = np.arange(len(pos_features))
choices = np.random.choice(ids, len(neg_features))

res_pos_features = pos_features[choices]
res_pos_labels = pos_labels[choices]

res_pos_features.shape
(181957, 29)
resampled_features = np.concatenate([res_pos_features, neg_features], axis=0)
resampled_labels = np.concatenate([res_pos_labels, neg_labels], axis=0)

order = np.arange(len(resampled_labels))
np.random.shuffle(order)
resampled_features = resampled_features[order]
resampled_labels = resampled_labels[order]

resampled_features.shape
(363914, 29)

Using tf.data

If you're using tf.data the easiest way to produce balanced examples is to start with a positive and a negative dataset, and merge them. See the tf.data guide for more examples.

BUFFER_SIZE = 100000

def make_ds(features, labels):
  ds = tf.data.Dataset.from_tensor_slices((features, labels))#.cache()
  ds = ds.shuffle(BUFFER_SIZE).repeat()
  return ds

pos_ds = make_ds(pos_features, pos_labels)
neg_ds = make_ds(neg_features, neg_labels)

Each dataset provides (feature, label) pairs:

for features, label in pos_ds.take(1):
  print("Features:\n", features.numpy())
  print()
  print("Label: ", label.numpy())
Features:
 [-2.47546504e+00 -1.63868504e+00 -3.07535212e+00  1.76617584e+00
 -1.44167423e+00  4.58854180e-01 -2.78281652e+00  1.56018311e+00
 -1.01765018e+00 -4.70557659e+00  2.89178867e+00 -4.12158724e+00
 -4.78876561e-01 -4.54970476e+00  1.34290485e-03 -5.00000000e+00
 -5.00000000e+00 -4.13090435e+00  7.32245355e-01  3.63250699e-01
  1.53591211e+00  1.42151098e+00 -1.28897428e+00 -2.49796202e+00
 -3.66920903e-01  1.67115022e-01  2.97790558e+00 -2.67900058e+00
  1.70315343e+00]

Label:  1

Merge the two together using experimental.sample_from_datasets:

resampled_ds = tf.data.experimental.sample_from_datasets([pos_ds, neg_ds], weights=[0.5, 0.5])
resampled_ds = resampled_ds.batch(BATCH_SIZE).prefetch(2)
for features, label in resampled_ds.take(1):
  print(label.numpy().mean())
0.50927734375

To use this dataset, you'll need the number of steps per epoch.

The definition of "epoch" in this case is less clear. Say it's the number of batches required to see each negative example once:

resampled_steps_per_epoch = np.ceil(2.0*neg/BATCH_SIZE)
resampled_steps_per_epoch
278.0

Train on the oversampled data

Now try training the model with the resampled data set instead of using class weights to see how these methods compare.

resampled_model = make_model()
resampled_model.load_weights(initial_weights)

# Reset the bias to zero, since this dataset is balanced.
output_layer = resampled_model.layers[-1] 
output_layer.bias.assign([0])

val_ds = tf.data.Dataset.from_tensor_slices((val_features, val_labels)).cache()
val_ds = val_ds.batch(BATCH_SIZE).prefetch(2) 

resampled_history = resampled_model.fit(
    resampled_ds,
    epochs=EPOCHS,
    steps_per_epoch=resampled_steps_per_epoch,
    callbacks = [early_stopping],
    validation_data=val_ds)
Train for 278.0 steps, validate for 23 steps
Epoch 1/100
278/278 [==============================] - 10s 36ms/step - loss: 0.5194 - tp: 221500.0000 - fp: 70705.0000 - tn: 213684.0000 - fn: 63455.0000 - accuracy: 0.7644 - precision: 0.7580 - recall: 0.7773 - auc: 0.8402 - val_loss: 0.2314 - val_tp: 65.0000 - val_fp: 1100.0000 - val_tn: 44398.0000 - val_fn: 6.0000 - val_accuracy: 0.9757 - val_precision: 0.0558 - val_recall: 0.9155 - val_auc: 0.9846
Epoch 2/100
278/278 [==============================] - 8s 27ms/step - loss: 0.2290 - tp: 250180.0000 - fp: 18978.0000 - tn: 264943.0000 - fn: 35243.0000 - accuracy: 0.9048 - precision: 0.9295 - recall: 0.8765 - auc: 0.9641 - val_loss: 0.1216 - val_tp: 66.0000 - val_fp: 714.0000 - val_tn: 44784.0000 - val_fn: 5.0000 - val_accuracy: 0.9842 - val_precision: 0.0846 - val_recall: 0.9296 - val_auc: 0.9932
Epoch 3/100
278/278 [==============================] - 8s 27ms/step - loss: 0.1803 - tp: 254687.0000 - fp: 11583.0000 - tn: 273142.0000 - fn: 29932.0000 - accuracy: 0.9271 - precision: 0.9565 - recall: 0.8948 - auc: 0.9788 - val_loss: 0.0934 - val_tp: 67.0000 - val_fp: 793.0000 - val_tn: 44705.0000 - val_fn: 4.0000 - val_accuracy: 0.9825 - val_precision: 0.0779 - val_recall: 0.9437 - val_auc: 0.9905
Epoch 4/100
278/278 [==============================] - 8s 28ms/step - loss: 0.1554 - tp: 258805.0000 - fp: 10024.0000 - tn: 274561.0000 - fn: 25954.0000 - accuracy: 0.9368 - precision: 0.9627 - recall: 0.9089 - auc: 0.9850 - val_loss: 0.0791 - val_tp: 67.0000 - val_fp: 823.0000 - val_tn: 44675.0000 - val_fn: 4.0000 - val_accuracy: 0.9819 - val_precision: 0.0753 - val_recall: 0.9437 - val_auc: 0.9892
Epoch 5/100
278/278 [==============================] - 8s 29ms/step - loss: 0.1363 - tp: 262550.0000 - fp: 8841.0000 - tn: 276074.0000 - fn: 21879.0000 - accuracy: 0.9460 - precision: 0.9674 - recall: 0.9231 - auc: 0.9886 - val_loss: 0.0677 - val_tp: 67.0000 - val_fp: 745.0000 - val_tn: 44753.0000 - val_fn: 4.0000 - val_accuracy: 0.9836 - val_precision: 0.0825 - val_recall: 0.9437 - val_auc: 0.9855
Epoch 6/100
278/278 [==============================] - 8s 29ms/step - loss: 0.1260 - tp: 263299.0000 - fp: 8004.0000 - tn: 276617.0000 - fn: 21424.0000 - accuracy: 0.9483 - precision: 0.9705 - recall: 0.9248 - auc: 0.9903 - val_loss: 0.0605 - val_tp: 67.0000 - val_fp: 672.0000 - val_tn: 44826.0000 - val_fn: 4.0000 - val_accuracy: 0.9852 - val_precision: 0.0907 - val_recall: 0.9437 - val_auc: 0.9859
Epoch 7/100
278/278 [==============================] - 8s 28ms/step - loss: 0.1194 - tp: 263850.0000 - fp: 7513.0000 - tn: 277344.0000 - fn: 20637.0000 - accuracy: 0.9506 - precision: 0.9723 - recall: 0.9275 - auc: 0.9915 - val_loss: 0.0553 - val_tp: 67.0000 - val_fp: 612.0000 - val_tn: 44886.0000 - val_fn: 4.0000 - val_accuracy: 0.9865 - val_precision: 0.0987 - val_recall: 0.9437 - val_auc: 0.9862
Epoch 8/100
278/278 [==============================] - 8s 28ms/step - loss: 0.1129 - tp: 264963.0000 - fp: 7216.0000 - tn: 277485.0000 - fn: 19680.0000 - accuracy: 0.9528 - precision: 0.9735 - recall: 0.9309 - auc: 0.9926 - val_loss: 0.0519 - val_tp: 67.0000 - val_fp: 593.0000 - val_tn: 44905.0000 - val_fn: 4.0000 - val_accuracy: 0.9869 - val_precision: 0.1015 - val_recall: 0.9437 - val_auc: 0.9864
Epoch 9/100
278/278 [==============================] - 8s 28ms/step - loss: 0.1074 - tp: 264971.0000 - fp: 6875.0000 - tn: 278829.0000 - fn: 18669.0000 - accuracy: 0.9551 - precision: 0.9747 - recall: 0.9342 - auc: 0.9933 - val_loss: 0.0487 - val_tp: 67.0000 - val_fp: 571.0000 - val_tn: 44927.0000 - val_fn: 4.0000 - val_accuracy: 0.9874 - val_precision: 0.1050 - val_recall: 0.9437 - val_auc: 0.9863
Epoch 10/100
278/278 [==============================] - 8s 28ms/step - loss: 0.1033 - tp: 266945.0000 - fp: 6746.0000 - tn: 277911.0000 - fn: 17742.0000 - accuracy: 0.9570 - precision: 0.9754 - recall: 0.9377 - auc: 0.9939 - val_loss: 0.0454 - val_tp: 68.0000 - val_fp: 555.0000 - val_tn: 44943.0000 - val_fn: 3.0000 - val_accuracy: 0.9878 - val_precision: 0.1091 - val_recall: 0.9577 - val_auc: 0.9861
Epoch 11/100
278/278 [==============================] - 8s 28ms/step - loss: 0.0991 - tp: 267663.0000 - fp: 6653.0000 - tn: 277987.0000 - fn: 17041.0000 - accuracy: 0.9584 - precision: 0.9757 - recall: 0.9401 - auc: 0.9944 - val_loss: 0.0428 - val_tp: 68.0000 - val_fp: 514.0000 - val_tn: 44984.0000 - val_fn: 3.0000 - val_accuracy: 0.9887 - val_precision: 0.1168 - val_recall: 0.9577 - val_auc: 0.9856
Epoch 12/100
277/278 [============================>.] - ETA: 0s - loss: 0.0959 - tp: 267939.0000 - fp: 6508.0000 - tn: 276513.0000 - fn: 16336.0000 - accuracy: 0.9597 - precision: 0.9763 - recall: 0.9425 - auc: 0.9950Restoring model weights from the end of the best epoch.
278/278 [==============================] - 8s 28ms/step - loss: 0.0959 - tp: 268905.0000 - fp: 6531.0000 - tn: 277514.0000 - fn: 16394.0000 - accuracy: 0.9597 - precision: 0.9763 - recall: 0.9425 - auc: 0.9950 - val_loss: 0.0402 - val_tp: 68.0000 - val_fp: 512.0000 - val_tn: 44986.0000 - val_fn: 3.0000 - val_accuracy: 0.9887 - val_precision: 0.1172 - val_recall: 0.9577 - val_auc: 0.9855
Epoch 00012: early stopping

If the training process were considering the whole dataset on each gradient update, this oversampling would be basically identical to the class weighting.

But when training the model batch-wise, as you did here, the oversampled data provides a smoother gradient signal: Instead of each positive example being shown in one batch with a large weight, they're shown in many different batches each time with a small weight.

This smoother gradient signal makes it easier to train the model.

Check training history

Note that the distributions of metrics will be different here, because the training data has a totally different distribution from the validation and test data.

plot_metrics(resampled_history )

png

Re-train

Because training is easier on the balanced data, the above training procedure may overfit quickly.

So break up the epochs to give the callbacks.EarlyStopping finer control over when to stop training.

resampled_model = make_model()
resampled_model.load_weights(initial_weights)

# Reset the bias to zero, since this dataset is balanced.
output_layer = resampled_model.layers[-1] 
output_layer.bias.assign([0])

resampled_history = resampled_model.fit(
    resampled_ds,
    # These are not real epochs
    steps_per_epoch = 20,
    epochs=10*EPOCHS,
    callbacks = [early_stopping],
    validation_data=(val_ds))
Train for 20 steps, validate for 23 steps
Epoch 1/1000
20/20 [==============================] - 3s 141ms/step - loss: 1.4540 - tp: 8245.0000 - fp: 8685.0000 - tn: 11778.0000 - fn: 12252.0000 - accuracy: 0.4888 - precision: 0.4870 - recall: 0.4023 - auc: 0.4323 - val_loss: 0.6583 - val_tp: 43.0000 - val_fp: 17222.0000 - val_tn: 28276.0000 - val_fn: 28.0000 - val_accuracy: 0.6215 - val_precision: 0.0025 - val_recall: 0.6056 - val_auc: 0.6543
Epoch 2/1000
20/20 [==============================] - 0s 21ms/step - loss: 0.9107 - tp: 12142.0000 - fp: 8796.0000 - tn: 11616.0000 - fn: 8406.0000 - accuracy: 0.5800 - precision: 0.5799 - recall: 0.5909 - auc: 0.6152 - val_loss: 0.6412 - val_tp: 56.0000 - val_fp: 16443.0000 - val_tn: 29055.0000 - val_fn: 15.0000 - val_accuracy: 0.6388 - val_precision: 0.0034 - val_recall: 0.7887 - val_auc: 0.8338
Epoch 3/1000
20/20 [==============================] - 1s 29ms/step - loss: 0.6853 - tp: 14417.0000 - fp: 8306.0000 - tn: 12188.0000 - fn: 6049.0000 - accuracy: 0.6495 - precision: 0.6345 - recall: 0.7044 - auc: 0.7293 - val_loss: 0.5984 - val_tp: 59.0000 - val_fp: 13942.0000 - val_tn: 31556.0000 - val_fn: 12.0000 - val_accuracy: 0.6938 - val_precision: 0.0042 - val_recall: 0.8310 - val_auc: 0.8800
Epoch 4/1000
20/20 [==============================] - 1s 30ms/step - loss: 0.5718 - tp: 15490.0000 - fp: 7308.0000 - tn: 13147.0000 - fn: 5015.0000 - accuracy: 0.6991 - precision: 0.6794 - recall: 0.7554 - auc: 0.7947 - val_loss: 0.5462 - val_tp: 59.0000 - val_fp: 10911.0000 - val_tn: 34587.0000 - val_fn: 12.0000 - val_accuracy: 0.7603 - val_precision: 0.0054 - val_recall: 0.8310 - val_auc: 0.9051
Epoch 5/1000
20/20 [==============================] - 1s 25ms/step - loss: 0.4990 - tp: 16381.0000 - fp: 6124.0000 - tn: 14189.0000 - fn: 4266.0000 - accuracy: 0.7463 - precision: 0.7279 - recall: 0.7934 - auc: 0.8381 - val_loss: 0.4961 - val_tp: 60.0000 - val_fp: 8113.0000 - val_tn: 37385.0000 - val_fn: 11.0000 - val_accuracy: 0.8217 - val_precision: 0.0073 - val_recall: 0.8451 - val_auc: 0.9270
Epoch 6/1000
20/20 [==============================] - 1s 28ms/step - loss: 0.4525 - tp: 16694.0000 - fp: 5534.0000 - tn: 14890.0000 - fn: 3842.0000 - accuracy: 0.7711 - precision: 0.7510 - recall: 0.8129 - auc: 0.8648 - val_loss: 0.4504 - val_tp: 61.0000 - val_fp: 6059.0000 - val_tn: 39439.0000 - val_fn: 10.0000 - val_accuracy: 0.8668 - val_precision: 0.0100 - val_recall: 0.8592 - val_auc: 0.9435
Epoch 7/1000
20/20 [==============================] - 1s 26ms/step - loss: 0.4128 - tp: 16907.0000 - fp: 4887.0000 - tn: 15722.0000 - fn: 3444.0000 - accuracy: 0.7966 - precision: 0.7758 - recall: 0.8308 - auc: 0.8871 - val_loss: 0.4082 - val_tp: 63.0000 - val_fp: 4551.0000 - val_tn: 40947.0000 - val_fn: 8.0000 - val_accuracy: 0.9000 - val_precision: 0.0137 - val_recall: 0.8873 - val_auc: 0.9543
Epoch 8/1000
20/20 [==============================] - 1s 27ms/step - loss: 0.3838 - tp: 17049.0000 - fp: 4324.0000 - tn: 16324.0000 - fn: 3263.0000 - accuracy: 0.8148 - precision: 0.7977 - recall: 0.8394 - auc: 0.9026 - val_loss: 0.3707 - val_tp: 64.0000 - val_fp: 3336.0000 - val_tn: 42162.0000 - val_fn: 7.0000 - val_accuracy: 0.9266 - val_precision: 0.0188 - val_recall: 0.9014 - val_auc: 0.9626
Epoch 9/1000
20/20 [==============================] - 1s 33ms/step - loss: 0.3595 - tp: 17467.0000 - fp: 3665.0000 - tn: 16693.0000 - fn: 3135.0000 - accuracy: 0.8340 - precision: 0.8266 - recall: 0.8478 - auc: 0.9133 - val_loss: 0.3385 - val_tp: 64.0000 - val_fp: 2486.0000 - val_tn: 43012.0000 - val_fn: 7.0000 - val_accuracy: 0.9453 - val_precision: 0.0251 - val_recall: 0.9014 - val_auc: 0.9689
Epoch 10/1000
20/20 [==============================] - 1s 28ms/step - loss: 0.3345 - tp: 17518.0000 - fp: 3162.0000 - tn: 17263.0000 - fn: 3017.0000 - accuracy: 0.8491 - precision: 0.8471 - recall: 0.8531 - auc: 0.9237 - val_loss: 0.3116 - val_tp: 65.0000 - val_fp: 1997.0000 - val_tn: 43501.0000 - val_fn: 6.0000 - val_accuracy: 0.9560 - val_precision: 0.0315 - val_recall: 0.9155 - val_auc: 0.9734
Epoch 11/1000
20/20 [==============================] - 1s 29ms/step - loss: 0.3185 - tp: 17665.0000 - fp: 2994.0000 - tn: 17423.0000 - fn: 2878.0000 - accuracy: 0.8566 - precision: 0.8551 - recall: 0.8599 - auc: 0.9314 - val_loss: 0.2871 - val_tp: 65.0000 - val_fp: 1620.0000 - val_tn: 43878.0000 - val_fn: 6.0000 - val_accuracy: 0.9643 - val_precision: 0.0386 - val_recall: 0.9155 - val_auc: 0.9771
Epoch 12/1000
20/20 [==============================] - 1s 28ms/step - loss: 0.3061 - tp: 17634.0000 - fp: 2667.0000 - tn: 17805.0000 - fn: 2854.0000 - accuracy: 0.8652 - precision: 0.8686 - recall: 0.8607 - auc: 0.9368 - val_loss: 0.2656 - val_tp: 65.0000 - val_fp: 1353.0000 - val_tn: 44145.0000 - val_fn: 6.0000 - val_accuracy: 0.9702 - val_precision: 0.0458 - val_recall: 0.9155 - val_auc: 0.9803
Epoch 13/1000
20/20 [==============================] - 1s 32ms/step - loss: 0.2918 - tp: 17688.0000 - fp: 2403.0000 - tn: 18078.0000 - fn: 2791.0000 - accuracy: 0.8732 - precision: 0.8804 - recall: 0.8637 - auc: 0.9417 - val_loss: 0.2466 - val_tp: 65.0000 - val_fp: 1215.0000 - val_tn: 44283.0000 - val_fn: 6.0000 - val_accuracy: 0.9732 - val_precision: 0.0508 - val_recall: 0.9155 - val_auc: 0.9830
Epoch 14/1000
20/20 [==============================] - 1s 29ms/step - loss: 0.2809 - tp: 17686.0000 - fp: 2155.0000 - tn: 18367.0000 - fn: 2752.0000 - accuracy: 0.8802 - precision: 0.8914 - recall: 0.8653 - auc: 0.9464 - val_loss: 0.2295 - val_tp: 65.0000 - val_fp: 1088.0000 - val_tn: 44410.0000 - val_fn: 6.0000 - val_accuracy: 0.9760 - val_precision: 0.0564 - val_recall: 0.9155 - val_auc: 0.9850
Epoch 15/1000
20/20 [==============================] - 1s 28ms/step - loss: 0.2697 - tp: 17776.0000 - fp: 1991.0000 - tn: 18395.0000 - fn: 2798.0000 - accuracy: 0.8831 - precision: 0.8993 - recall: 0.8640 - auc: 0.9494 - val_loss: 0.2144 - val_tp: 65.0000 - val_fp: 1013.0000 - val_tn: 44485.0000 - val_fn: 6.0000 - val_accuracy: 0.9776 - val_precision: 0.0603 - val_recall: 0.9155 - val_auc: 0.9869
Epoch 16/1000
20/20 [==============================] - 1s 29ms/step - loss: 0.2593 - tp: 17811.0000 - fp: 1773.0000 - tn: 18646.0000 - fn: 2730.0000 - accuracy: 0.8901 - precision: 0.9095 - recall: 0.8671 - auc: 0.9536 - val_loss: 0.2014 - val_tp: 65.0000 - val_fp: 929.0000 - val_tn: 44569.0000 - val_fn: 6.0000 - val_accuracy: 0.9795 - val_precision: 0.0654 - val_recall: 0.9155 - val_auc: 0.9884
Epoch 17/1000
20/20 [==============================] - 1s 27ms/step - loss: 0.2522 - tp: 17862.0000 - fp: 1639.0000 - tn: 18738.0000 - fn: 2721.0000 - accuracy: 0.8936 - precision: 0.9160 - recall: 0.8678 - auc: 0.9561 - val_loss: 0.1900 - val_tp: 65.0000 - val_fp: 889.0000 - val_tn: 44609.0000 - val_fn: 6.0000 - val_accuracy: 0.9804 - val_precision: 0.0681 - val_recall: 0.9155 - val_auc: 0.9897
Epoch 18/1000
20/20 [==============================] - 1s 34ms/step - loss: 0.2479 - tp: 17840.0000 - fp: 1609.0000 - tn: 18785.0000 - fn: 2726.0000 - accuracy: 0.8942 - precision: 0.9173 - recall: 0.8675 - auc: 0.9575 - val_loss: 0.1794 - val_tp: 65.0000 - val_fp: 843.0000 - val_tn: 44655.0000 - val_fn: 6.0000 - val_accuracy: 0.9814 - val_precision: 0.0716 - val_recall: 0.9155 - val_auc: 0.9907
Epoch 19/1000
20/20 [==============================] - 1s 28ms/step - loss: 0.2378 - tp: 17941.0000 - fp: 1551.0000 - tn: 18891.0000 - fn: 2577.0000 - accuracy: 0.8992 - precision: 0.9204 - recall: 0.8744 - auc: 0.9606 - val_loss: 0.1700 - val_tp: 65.0000 - val_fp: 804.0000 - val_tn: 44694.0000 - val_fn: 6.0000 - val_accuracy: 0.9822 - val_precision: 0.0748 - val_recall: 0.9155 - val_auc: 0.9914
Epoch 20/1000
20/20 [==============================] - 1s 29ms/step - loss: 0.2337 - tp: 17868.0000 - fp: 1395.0000 - tn: 19078.0000 - fn: 2619.0000 - accuracy: 0.9020 - precision: 0.9276 - recall: 0.8722 - auc: 0.9621 - val_loss: 0.1618 - val_tp: 65.0000 - val_fp: 782.0000 - val_tn: 44716.0000 - val_fn: 6.0000 - val_accuracy: 0.9827 - val_precision: 0.0767 - val_recall: 0.9155 - val_auc: 0.9919
Epoch 21/1000
20/20 [==============================] - 1s 30ms/step - loss: 0.2259 - tp: 18021.0000 - fp: 1329.0000 - tn: 19072.0000 - fn: 2538.0000 - accuracy: 0.9056 - precision: 0.9313 - recall: 0.8766 - auc: 0.9654 - val_loss: 0.1543 - val_tp: 65.0000 - val_fp: 747.0000 - val_tn: 44751.0000 - val_fn: 6.0000 - val_accuracy: 0.9835 - val_precision: 0.0800 - val_recall: 0.9155 - val_auc: 0.9923
Epoch 22/1000
20/20 [==============================] - 1s 30ms/step - loss: 0.2231 - tp: 18085.0000 - fp: 1280.0000 - tn: 19080.0000 - fn: 2515.0000 - accuracy: 0.9073 - precision: 0.9339 - recall: 0.8779 - auc: 0.9657 - val_loss: 0.1477 - val_tp: 66.0000 - val_fp: 739.0000 - val_tn: 44759.0000 - val_fn: 5.0000 - val_accuracy: 0.9837 - val_precision: 0.0820 - val_recall: 0.9296 - val_auc: 0.9926
Epoch 23/1000
20/20 [==============================] - 0s 25ms/step - loss: 0.2194 - tp: 17949.0000 - fp: 1222.0000 - tn: 19278.0000 - fn: 2511.0000 - accuracy: 0.9089 - precision: 0.9363 - recall: 0.8773 - auc: 0.9673 - val_loss: 0.1413 - val_tp: 66.0000 - val_fp: 717.0000 - val_tn: 44781.0000 - val_fn: 5.0000 - val_accuracy: 0.9842 - val_precision: 0.0843 - val_recall: 0.9296 - val_auc: 0.9928
Epoch 24/1000
20/20 [==============================] - 1s 28ms/step - loss: 0.2125 - tp: 18083.0000 - fp: 1148.0000 - tn: 19269.0000 - fn: 2460.0000 - accuracy: 0.9119 - precision: 0.9403 - recall: 0.8803 - auc: 0.9694 - val_loss: 0.1358 - val_tp: 66.0000 - val_fp: 706.0000 - val_tn: 44792.0000 - val_fn: 5.0000 - val_accuracy: 0.9844 - val_precision: 0.0855 - val_recall: 0.9296 - val_auc: 0.9929
Epoch 25/1000
20/20 [==============================] - 1s 26ms/step - loss: 0.2073 - tp: 18146.0000 - fp: 1080.0000 - tn: 19307.0000 - fn: 2427.0000 - accuracy: 0.9144 - precision: 0.9438 - recall: 0.8820 - auc: 0.9710 - val_loss: 0.1309 - val_tp: 66.0000 - val_fp: 706.0000 - val_tn: 44792.0000 - val_fn: 5.0000 - val_accuracy: 0.9844 - val_precision: 0.0855 - val_recall: 0.9296 - val_auc: 0.9932
Epoch 26/1000
20/20 [==============================] - 1s 26ms/step - loss: 0.2029 - tp: 18064.0000 - fp: 1095.0000 - tn: 19433.0000 - fn: 2368.0000 - accuracy: 0.9155 - precision: 0.9428 - recall: 0.8841 - auc: 0.9721 - val_loss: 0.1272 - val_tp: 66.0000 - val_fp: 710.0000 - val_tn: 44788.0000 - val_fn: 5.0000 - val_accuracy: 0.9843 - val_precision: 0.0851 - val_recall: 0.9296 - val_auc: 0.9931
Epoch 27/1000
20/20 [==============================] - 1s 36ms/step - loss: 0.2028 - tp: 18250.0000 - fp: 1015.0000 - tn: 19343.0000 - fn: 2352.0000 - accuracy: 0.9178 - precision: 0.9473 - recall: 0.8858 - auc: 0.9726 - val_loss: 0.1236 - val_tp: 66.0000 - val_fp: 710.0000 - val_tn: 44788.0000 - val_fn: 5.0000 - val_accuracy: 0.9843 - val_precision: 0.0851 - val_recall: 0.9296 - val_auc: 0.9932
Epoch 28/1000
20/20 [==============================] - 1s 32ms/step - loss: 0.1994 - tp: 18111.0000 - fp: 979.0000 - tn: 19565.0000 - fn: 2305.0000 - accuracy: 0.9198 - precision: 0.9487 - recall: 0.8871 - auc: 0.9731 - val_loss: 0.1204 - val_tp: 66.0000 - val_fp: 716.0000 - val_tn: 44782.0000 - val_fn: 5.0000 - val_accuracy: 0.9842 - val_precision: 0.0844 - val_recall: 0.9296 - val_auc: 0.9930
Epoch 29/1000
20/20 [==============================] - 1s 33ms/step - loss: 0.1949 - tp: 18357.0000 - fp: 925.0000 - tn: 19343.0000 - fn: 2335.0000 - accuracy: 0.9204 - precision: 0.9520 - recall: 0.8872 - auc: 0.9744 - val_loss: 0.1175 - val_tp: 66.0000 - val_fp: 723.0000 - val_tn: 44775.0000 - val_fn: 5.0000 - val_accuracy: 0.9840 - val_precision: 0.0837 - val_recall: 0.9296 - val_auc: 0.9930
Epoch 30/1000
20/20 [==============================] - 1s 35ms/step - loss: 0.1905 - tp: 18135.0000 - fp: 924.0000 - tn: 19621.0000 - fn: 2280.0000 - accuracy: 0.9218 - precision: 0.9515 - recall: 0.8883 - auc: 0.9761 - val_loss: 0.1144 - val_tp: 67.0000 - val_fp: 733.0000 - val_tn: 44765.0000 - val_fn: 4.0000 - val_accuracy: 0.9838 - val_precision: 0.0838 - val_recall: 0.9437 - val_auc: 0.9927
Epoch 31/1000
20/20 [==============================] - 1s 29ms/step - loss: 0.1878 - tp: 18190.0000 - fp: 903.0000 - tn: 19672.0000 - fn: 2195.0000 - accuracy: 0.9244 - precision: 0.9527 - recall: 0.8923 - auc: 0.9769 - val_loss: 0.1113 - val_tp: 67.0000 - val_fp: 727.0000 - val_tn: 44771.0000 - val_fn: 4.0000 - val_accuracy: 0.9840 - val_precision: 0.0844 - val_recall: 0.9437 - val_auc: 0.9924
Epoch 32/1000
20/20 [==============================] - 0s 25ms/step - loss: 0.1856 - tp: 18295.0000 - fp: 868.0000 - tn: 19611.0000 - fn: 2186.0000 - accuracy: 0.9254 - precision: 0.9547 - recall: 0.8933 - auc: 0.9772 - val_loss: 0.1083 - val_tp: 67.0000 - val_fp: 726.0000 - val_tn: 44772.0000 - val_fn: 4.0000 - val_accuracy: 0.9840 - val_precision: 0.0845 - val_recall: 0.9437 - val_auc: 0.9923
Epoch 33/1000
20/20 [==============================] - 1s 27ms/step - loss: 0.1859 - tp: 18318.0000 - fp: 920.0000 - tn: 19502.0000 - fn: 2220.0000 - accuracy: 0.9233 - precision: 0.9522 - recall: 0.8919 - auc: 0.9772 - val_loss: 0.1058 - val_tp: 67.0000 - val_fp: 721.0000 - val_tn: 44777.0000 - val_fn: 4.0000 - val_accuracy: 0.9841 - val_precision: 0.0850 - val_recall: 0.9437 - val_auc: 0.9922
Epoch 34/1000
20/20 [==============================] - 1s 27ms/step - loss: 0.1811 - tp: 18319.0000 - fp: 840.0000 - tn: 19621.0000 - fn: 2180.0000 - accuracy: 0.9263 - precision: 0.9562 - recall: 0.8937 - auc: 0.9782 - val_loss: 0.1042 - val_tp: 67.0000 - val_fp: 731.0000 - val_tn: 44767.0000 - val_fn: 4.0000 - val_accuracy: 0.9839 - val_precision: 0.0840 - val_recall: 0.9437 - val_auc: 0.9920
Epoch 35/1000
20/20 [==============================] - 1s 28ms/step - loss: 0.1808 - tp: 18345.0000 - fp: 833.0000 - tn: 19724.0000 - fn: 2058.0000 - accuracy: 0.9294 - precision: 0.9566 - recall: 0.8991 - auc: 0.9791 - val_loss: 0.1024 - val_tp: 67.0000 - val_fp: 736.0000 - val_tn: 44762.0000 - val_fn: 4.0000 - val_accuracy: 0.9838 - val_precision: 0.0834 - val_recall: 0.9437 - val_auc: 0.9919
Epoch 36/1000
20/20 [==============================] - 1s 34ms/step - loss: 0.1811 - tp: 18482.0000 - fp: 822.0000 - tn: 19460.0000 - fn: 2196.0000 - accuracy: 0.9263 - precision: 0.9574 - recall: 0.8938 - auc: 0.9788 - val_loss: 0.1005 - val_tp: 67.0000 - val_fp: 736.0000 - val_tn: 44762.0000 - val_fn: 4.0000 - val_accuracy: 0.9838 - val_precision: 0.0834 - val_recall: 0.9437 - val_auc: 0.9916
Epoch 37/1000
18/20 [==========================>...] - ETA: 0s - loss: 0.1728 - tp: 16491.0000 - fp: 643.0000 - tn: 17825.0000 - fn: 1905.0000 - accuracy: 0.9309 - precision: 0.9625 - recall: 0.8964 - auc: 0.9804Restoring model weights from the end of the best epoch.
20/20 [==============================] - 1s 29ms/step - loss: 0.1728 - tp: 18357.0000 - fp: 712.0000 - tn: 19775.0000 - fn: 2116.0000 - accuracy: 0.9310 - precision: 0.9627 - recall: 0.8966 - auc: 0.9804 - val_loss: 0.0993 - val_tp: 67.0000 - val_fp: 745.0000 - val_tn: 44753.0000 - val_fn: 4.0000 - val_accuracy: 0.9836 - val_precision: 0.0825 - val_recall: 0.9437 - val_auc: 0.9913
Epoch 00037: early stopping

Re-check training history

plot_metrics(resampled_history)

png

Evaluate metrics

train_predictions_resampled = resampled_model.predict(train_features, batch_size=BATCH_SIZE)
test_predictions_resampled = resampled_model.predict(test_features, batch_size=BATCH_SIZE)
resampled_results = resampled_model.evaluate(test_features, test_labels,
                                             batch_size=BATCH_SIZE, verbose=0)
for name, value in zip(resampled_model.metrics_names, resampled_results):
  print(name, ': ', value)
print()

plot_cm(test_labels, test_predictions_weighted)
loss :  0.1262956837339392
tp :  88.0
fp :  998.0
tn :  55862.0
fn :  14.0
accuracy :  0.98223376
precision :  0.08103131
recall :  0.8627451
auc :  0.9784746

Legitimate Transactions Detected (True Negatives):  55853
Legitimate Transactions Incorrectly Detected (False Positives):  1007
Fraudulent Transactions Missed (False Negatives):  13
Fraudulent Transactions Detected (True Positives):  89
Total Fraudulent Transactions:  102

png

Plot the ROC

plot_roc("Train Baseline", train_labels, train_predictions_baseline, color=colors[0])
plot_roc("Test Baseline", test_labels, test_predictions_baseline, color=colors[0], linestyle='--')

plot_roc("Train Weighted", train_labels, train_predictions_weighted, color=colors[1])
plot_roc("Test Weighted", test_labels, test_predictions_weighted, color=colors[1], linestyle='--')

plot_roc("Train Resampled", train_labels, train_predictions_resampled,  color=colors[2])
plot_roc("Test Resampled", test_labels, test_predictions_resampled,  color=colors[2], linestyle='--')
plt.legend(loc='lower right')
<matplotlib.legend.Legend at 0x7f944c094f60>

png

Applying this tutorial to your problem

Imbalanced data classification is an inherantly difficult task since there are so few samples to learn from. You should always start with the data first and do your best to collect as many samples as possible and give substantial thought to what features may be relevant so the model can get the most out of your minority class. At some point your model may struggle to improve and yield the results you want, so it is important to keep in mind the context of your problem and the trade offs between different types of errors.