Attend the Women in ML Symposium on December 7 Register now

Classification on imbalanced data

Stay organized with collections Save and categorize content based on your preferences.

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 techniques for dealing with imbalanced data like:
    • Class weighting
    • Oversampling

Setup

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
2022-11-23 03:15:20.369934: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory
2022-11-23 03:15:20.370037: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory
2022-11-23 03:15:20.370047: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
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. It can be used to download CSVs into a Pandas 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 Amount'] = 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 your 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 distributions?
    • 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(x=pos_df['V5'], y=pos_df['V6'],
              kind='hex', xlim=(-5,5), ylim=(-5,5))
plt.suptitle("Positive distribution")

sns.jointplot(x=neg_df['V5'], y=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'),
      keras.metrics.AUC(name='prc', curve='PR'), # precision-recall curve
]

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(learning_rate=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 a random negative sample.
  • AUPRC refers to Area Under the Curve of the Precision-Recall Curve. This metric computes precision-recall pairs for different probability thresholds.

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_prc', 
    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])
1/1 [==============================] - 0s 389ms/step
array([[0.7512164 ],
       [0.8738115 ],
       [0.56720275],
       [0.8727235 ],
       [0.34886083],
       [0.6854036 ],
       [0.542899  ],
       [0.2788855 ],
       [0.6275276 ],
       [0.7957742 ]], dtype=float32)

Optional: Set the correct initial bias.

These 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: 1.0443

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])
1/1 [==============================] - 0s 45ms/step
array([[0.00221382],
       [0.00197204],
       [0.00133669],
       [0.00278363],
       [0.00134409],
       [0.00426059],
       [0.0029893 ],
       [0.00566873],
       [0.00094923],
       [0.0027906 ]], 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.0159

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

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 on y-axis 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')
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))
Epoch 1/100
90/90 [==============================] - 2s 10ms/step - loss: 0.0139 - tp: 136.0000 - fp: 251.0000 - tn: 227200.0000 - fn: 258.0000 - accuracy: 0.9978 - precision: 0.3514 - recall: 0.3452 - auc: 0.8631 - prc: 0.2079 - val_loss: 0.0068 - val_tp: 20.0000 - val_fp: 4.0000 - val_tn: 45487.0000 - val_fn: 58.0000 - val_accuracy: 0.9986 - val_precision: 0.8333 - val_recall: 0.2564 - val_auc: 0.9043 - val_prc: 0.6299
Epoch 2/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0080 - tp: 128.0000 - fp: 46.0000 - tn: 181914.0000 - fn: 188.0000 - accuracy: 0.9987 - precision: 0.7356 - recall: 0.4051 - auc: 0.8750 - prc: 0.4448 - val_loss: 0.0053 - val_tp: 34.0000 - val_fp: 4.0000 - val_tn: 45487.0000 - val_fn: 44.0000 - val_accuracy: 0.9989 - val_precision: 0.8947 - val_recall: 0.4359 - val_auc: 0.9153 - val_prc: 0.7033
Epoch 3/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0068 - tp: 146.0000 - fp: 40.0000 - tn: 181920.0000 - fn: 170.0000 - accuracy: 0.9988 - precision: 0.7849 - recall: 0.4620 - auc: 0.9026 - prc: 0.5494 - val_loss: 0.0046 - val_tp: 43.0000 - val_fp: 7.0000 - val_tn: 45484.0000 - val_fn: 35.0000 - val_accuracy: 0.9991 - val_precision: 0.8600 - val_recall: 0.5513 - val_auc: 0.9225 - val_prc: 0.7323
Epoch 4/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0059 - tp: 163.0000 - fp: 37.0000 - tn: 181923.0000 - fn: 153.0000 - accuracy: 0.9990 - precision: 0.8150 - recall: 0.5158 - auc: 0.9073 - prc: 0.6207 - val_loss: 0.0042 - val_tp: 47.0000 - val_fp: 7.0000 - val_tn: 45484.0000 - val_fn: 31.0000 - val_accuracy: 0.9992 - val_precision: 0.8704 - val_recall: 0.6026 - val_auc: 0.9292 - val_prc: 0.7544
Epoch 5/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0054 - tp: 181.0000 - fp: 41.0000 - tn: 181919.0000 - fn: 135.0000 - accuracy: 0.9990 - precision: 0.8153 - recall: 0.5728 - auc: 0.9247 - prc: 0.6601 - val_loss: 0.0039 - val_tp: 45.0000 - val_fp: 7.0000 - val_tn: 45484.0000 - val_fn: 33.0000 - val_accuracy: 0.9991 - val_precision: 0.8654 - val_recall: 0.5769 - val_auc: 0.9292 - val_prc: 0.7821
Epoch 6/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0050 - tp: 178.0000 - fp: 29.0000 - tn: 181931.0000 - fn: 138.0000 - accuracy: 0.9991 - precision: 0.8599 - recall: 0.5633 - auc: 0.9286 - prc: 0.6846 - val_loss: 0.0036 - val_tp: 54.0000 - val_fp: 7.0000 - val_tn: 45484.0000 - val_fn: 24.0000 - val_accuracy: 0.9993 - val_precision: 0.8852 - val_recall: 0.6923 - val_auc: 0.9355 - val_prc: 0.7929
Epoch 7/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0050 - tp: 182.0000 - fp: 35.0000 - tn: 181925.0000 - fn: 134.0000 - accuracy: 0.9991 - precision: 0.8387 - recall: 0.5759 - auc: 0.9272 - prc: 0.6675 - val_loss: 0.0035 - val_tp: 52.0000 - val_fp: 7.0000 - val_tn: 45484.0000 - val_fn: 26.0000 - val_accuracy: 0.9993 - val_precision: 0.8814 - val_recall: 0.6667 - val_auc: 0.9355 - val_prc: 0.8063
Epoch 8/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0047 - tp: 177.0000 - fp: 31.0000 - tn: 181929.0000 - fn: 139.0000 - accuracy: 0.9991 - precision: 0.8510 - recall: 0.5601 - auc: 0.9242 - prc: 0.6983 - val_loss: 0.0033 - val_tp: 57.0000 - val_fp: 7.0000 - val_tn: 45484.0000 - val_fn: 21.0000 - val_accuracy: 0.9994 - val_precision: 0.8906 - val_recall: 0.7308 - val_auc: 0.9355 - val_prc: 0.8092
Epoch 9/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0047 - tp: 190.0000 - fp: 38.0000 - tn: 181922.0000 - fn: 126.0000 - accuracy: 0.9991 - precision: 0.8333 - recall: 0.6013 - auc: 0.9261 - prc: 0.6976 - val_loss: 0.0032 - val_tp: 57.0000 - val_fp: 7.0000 - val_tn: 45484.0000 - val_fn: 21.0000 - val_accuracy: 0.9994 - val_precision: 0.8906 - val_recall: 0.7308 - val_auc: 0.9356 - val_prc: 0.8134
Epoch 10/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0042 - tp: 201.0000 - fp: 28.0000 - tn: 181932.0000 - fn: 115.0000 - accuracy: 0.9992 - precision: 0.8777 - recall: 0.6361 - auc: 0.9231 - prc: 0.7444 - val_loss: 0.0031 - val_tp: 56.0000 - val_fp: 7.0000 - val_tn: 45484.0000 - val_fn: 22.0000 - val_accuracy: 0.9994 - val_precision: 0.8889 - val_recall: 0.7179 - val_auc: 0.9356 - val_prc: 0.8211
Epoch 11/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0045 - tp: 186.0000 - fp: 30.0000 - tn: 181930.0000 - fn: 130.0000 - accuracy: 0.9991 - precision: 0.8611 - recall: 0.5886 - auc: 0.9276 - prc: 0.6878 - val_loss: 0.0030 - val_tp: 60.0000 - val_fp: 7.0000 - val_tn: 45484.0000 - val_fn: 18.0000 - val_accuracy: 0.9995 - val_precision: 0.8955 - val_recall: 0.7692 - val_auc: 0.9355 - val_prc: 0.8200
Epoch 12/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0042 - tp: 204.0000 - fp: 34.0000 - tn: 181926.0000 - fn: 112.0000 - accuracy: 0.9992 - precision: 0.8571 - recall: 0.6456 - auc: 0.9358 - prc: 0.7174 - val_loss: 0.0030 - val_tp: 57.0000 - val_fp: 7.0000 - val_tn: 45484.0000 - val_fn: 21.0000 - val_accuracy: 0.9994 - val_precision: 0.8906 - val_recall: 0.7308 - val_auc: 0.9356 - val_prc: 0.8272
Epoch 13/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0043 - tp: 185.0000 - fp: 30.0000 - tn: 181930.0000 - fn: 131.0000 - accuracy: 0.9991 - precision: 0.8605 - recall: 0.5854 - auc: 0.9261 - prc: 0.7139 - val_loss: 0.0029 - val_tp: 60.0000 - val_fp: 7.0000 - val_tn: 45484.0000 - val_fn: 18.0000 - val_accuracy: 0.9995 - val_precision: 0.8955 - val_recall: 0.7692 - val_auc: 0.9356 - val_prc: 0.8269
Epoch 14/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0041 - tp: 170.0000 - fp: 22.0000 - tn: 181938.0000 - fn: 146.0000 - accuracy: 0.9991 - precision: 0.8854 - recall: 0.5380 - auc: 0.9342 - prc: 0.7374 - val_loss: 0.0030 - val_tp: 52.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 26.0000 - val_accuracy: 0.9994 - val_precision: 0.9455 - val_recall: 0.6667 - val_auc: 0.9356 - val_prc: 0.8307
Epoch 15/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0040 - tp: 189.0000 - fp: 25.0000 - tn: 181935.0000 - fn: 127.0000 - accuracy: 0.9992 - precision: 0.8832 - recall: 0.5981 - auc: 0.9326 - prc: 0.7216 - val_loss: 0.0029 - val_tp: 60.0000 - val_fp: 6.0000 - val_tn: 45485.0000 - val_fn: 18.0000 - val_accuracy: 0.9995 - val_precision: 0.9091 - val_recall: 0.7692 - val_auc: 0.9356 - val_prc: 0.8273
Epoch 16/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0044 - tp: 182.0000 - fp: 36.0000 - tn: 181924.0000 - fn: 134.0000 - accuracy: 0.9991 - precision: 0.8349 - recall: 0.5759 - auc: 0.9215 - prc: 0.6964 - val_loss: 0.0029 - val_tp: 54.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 24.0000 - val_accuracy: 0.9994 - val_precision: 0.9474 - val_recall: 0.6923 - val_auc: 0.9356 - val_prc: 0.8338
Epoch 17/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0042 - tp: 190.0000 - fp: 31.0000 - tn: 181929.0000 - fn: 126.0000 - accuracy: 0.9991 - precision: 0.8597 - recall: 0.6013 - auc: 0.9374 - prc: 0.7287 - val_loss: 0.0029 - val_tp: 57.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 21.0000 - val_accuracy: 0.9995 - val_precision: 0.9500 - val_recall: 0.7308 - val_auc: 0.9355 - val_prc: 0.8313
Epoch 18/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0038 - tp: 185.0000 - fp: 27.0000 - tn: 181933.0000 - fn: 131.0000 - accuracy: 0.9991 - precision: 0.8726 - recall: 0.5854 - auc: 0.9296 - prc: 0.7529 - val_loss: 0.0029 - val_tp: 60.0000 - val_fp: 6.0000 - val_tn: 45485.0000 - val_fn: 18.0000 - val_accuracy: 0.9995 - val_precision: 0.9091 - val_recall: 0.7692 - val_auc: 0.9419 - val_prc: 0.8361
Epoch 19/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0037 - tp: 205.0000 - fp: 30.0000 - tn: 181930.0000 - fn: 111.0000 - accuracy: 0.9992 - precision: 0.8723 - recall: 0.6487 - auc: 0.9375 - prc: 0.7635 - val_loss: 0.0029 - val_tp: 55.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 23.0000 - val_accuracy: 0.9994 - val_precision: 0.9483 - val_recall: 0.7051 - val_auc: 0.9356 - val_prc: 0.8367
Epoch 20/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0038 - tp: 199.0000 - fp: 28.0000 - tn: 181932.0000 - fn: 117.0000 - accuracy: 0.9992 - precision: 0.8767 - recall: 0.6297 - auc: 0.9279 - prc: 0.7616 - val_loss: 0.0028 - val_tp: 60.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 18.0000 - val_accuracy: 0.9995 - val_precision: 0.9524 - val_recall: 0.7692 - val_auc: 0.9356 - val_prc: 0.8373
Epoch 21/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0037 - tp: 203.0000 - fp: 30.0000 - tn: 181930.0000 - fn: 113.0000 - accuracy: 0.9992 - precision: 0.8712 - recall: 0.6424 - auc: 0.9390 - prc: 0.7627 - val_loss: 0.0028 - val_tp: 55.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 23.0000 - val_accuracy: 0.9994 - val_precision: 0.9483 - val_recall: 0.7051 - val_auc: 0.9356 - val_prc: 0.8379
Epoch 22/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0039 - tp: 194.0000 - fp: 27.0000 - tn: 181933.0000 - fn: 122.0000 - accuracy: 0.9992 - precision: 0.8778 - recall: 0.6139 - auc: 0.9343 - prc: 0.7375 - val_loss: 0.0028 - val_tp: 60.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 18.0000 - val_accuracy: 0.9995 - val_precision: 0.9524 - val_recall: 0.7692 - val_auc: 0.9356 - val_prc: 0.8377
Epoch 23/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0038 - tp: 207.0000 - fp: 26.0000 - tn: 181934.0000 - fn: 109.0000 - accuracy: 0.9993 - precision: 0.8884 - recall: 0.6551 - auc: 0.9295 - prc: 0.7433 - val_loss: 0.0028 - val_tp: 61.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 17.0000 - val_accuracy: 0.9996 - val_precision: 0.9531 - val_recall: 0.7821 - val_auc: 0.9356 - val_prc: 0.8372
Epoch 24/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0038 - tp: 199.0000 - fp: 33.0000 - tn: 181927.0000 - fn: 117.0000 - accuracy: 0.9992 - precision: 0.8578 - recall: 0.6297 - auc: 0.9390 - prc: 0.7347 - val_loss: 0.0028 - val_tp: 60.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 18.0000 - val_accuracy: 0.9995 - val_precision: 0.9524 - val_recall: 0.7692 - val_auc: 0.9356 - val_prc: 0.8374
Epoch 25/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0037 - tp: 213.0000 - fp: 32.0000 - tn: 181928.0000 - fn: 103.0000 - accuracy: 0.9993 - precision: 0.8694 - recall: 0.6741 - auc: 0.9295 - prc: 0.7382 - val_loss: 0.0028 - val_tp: 55.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 23.0000 - val_accuracy: 0.9994 - val_precision: 0.9483 - val_recall: 0.7051 - val_auc: 0.9356 - val_prc: 0.8389
Epoch 26/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0039 - tp: 187.0000 - fp: 30.0000 - tn: 181930.0000 - fn: 129.0000 - accuracy: 0.9991 - precision: 0.8618 - recall: 0.5918 - auc: 0.9359 - prc: 0.7364 - val_loss: 0.0028 - val_tp: 61.0000 - val_fp: 6.0000 - val_tn: 45485.0000 - val_fn: 17.0000 - val_accuracy: 0.9995 - val_precision: 0.9104 - val_recall: 0.7821 - val_auc: 0.9355 - val_prc: 0.8369
Epoch 27/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0039 - tp: 201.0000 - fp: 35.0000 - tn: 181925.0000 - fn: 115.0000 - accuracy: 0.9992 - precision: 0.8517 - recall: 0.6361 - auc: 0.9295 - prc: 0.7204 - val_loss: 0.0028 - val_tp: 61.0000 - val_fp: 4.0000 - val_tn: 45487.0000 - val_fn: 17.0000 - val_accuracy: 0.9995 - val_precision: 0.9385 - val_recall: 0.7821 - val_auc: 0.9356 - val_prc: 0.8380
Epoch 28/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0036 - tp: 204.0000 - fp: 36.0000 - tn: 181924.0000 - fn: 112.0000 - accuracy: 0.9992 - precision: 0.8500 - recall: 0.6456 - auc: 0.9375 - prc: 0.7490 - val_loss: 0.0028 - val_tp: 58.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 20.0000 - val_accuracy: 0.9995 - val_precision: 0.9508 - val_recall: 0.7436 - val_auc: 0.9356 - val_prc: 0.8392
Epoch 29/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0038 - tp: 197.0000 - fp: 26.0000 - tn: 181934.0000 - fn: 119.0000 - accuracy: 0.9992 - precision: 0.8834 - recall: 0.6234 - auc: 0.9359 - prc: 0.7430 - val_loss: 0.0028 - val_tp: 58.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 20.0000 - val_accuracy: 0.9995 - val_precision: 0.9508 - val_recall: 0.7436 - val_auc: 0.9356 - val_prc: 0.8391
Epoch 30/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0036 - tp: 209.0000 - fp: 31.0000 - tn: 181929.0000 - fn: 107.0000 - accuracy: 0.9992 - precision: 0.8708 - recall: 0.6614 - auc: 0.9327 - prc: 0.7534 - val_loss: 0.0027 - val_tp: 60.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 18.0000 - val_accuracy: 0.9995 - val_precision: 0.9524 - val_recall: 0.7692 - val_auc: 0.9356 - val_prc: 0.8390
Epoch 31/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - tp: 218.0000 - fp: 32.0000 - tn: 181928.0000 - fn: 98.0000 - accuracy: 0.9993 - precision: 0.8720 - recall: 0.6899 - auc: 0.9312 - prc: 0.7592 - val_loss: 0.0027 - val_tp: 62.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 16.0000 - val_accuracy: 0.9996 - val_precision: 0.9538 - val_recall: 0.7949 - val_auc: 0.9356 - val_prc: 0.8393
Epoch 32/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0037 - tp: 200.0000 - fp: 31.0000 - tn: 181929.0000 - fn: 116.0000 - accuracy: 0.9992 - precision: 0.8658 - recall: 0.6329 - auc: 0.9438 - prc: 0.7520 - val_loss: 0.0027 - val_tp: 62.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 16.0000 - val_accuracy: 0.9996 - val_precision: 0.9538 - val_recall: 0.7949 - val_auc: 0.9355 - val_prc: 0.8400
Epoch 33/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - tp: 213.0000 - fp: 34.0000 - tn: 181926.0000 - fn: 103.0000 - accuracy: 0.9992 - precision: 0.8623 - recall: 0.6741 - auc: 0.9343 - prc: 0.7630 - val_loss: 0.0028 - val_tp: 56.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 22.0000 - val_accuracy: 0.9995 - val_precision: 0.9492 - val_recall: 0.7179 - val_auc: 0.9356 - val_prc: 0.8396
Epoch 34/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - tp: 204.0000 - fp: 30.0000 - tn: 181930.0000 - fn: 112.0000 - accuracy: 0.9992 - precision: 0.8718 - recall: 0.6456 - auc: 0.9439 - prc: 0.7702 - val_loss: 0.0028 - val_tp: 57.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 21.0000 - val_accuracy: 0.9995 - val_precision: 0.9500 - val_recall: 0.7308 - val_auc: 0.9356 - val_prc: 0.8394
Epoch 35/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0038 - tp: 198.0000 - fp: 34.0000 - tn: 181926.0000 - fn: 118.0000 - accuracy: 0.9992 - precision: 0.8534 - recall: 0.6266 - auc: 0.9311 - prc: 0.7275 - val_loss: 0.0028 - val_tp: 57.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 21.0000 - val_accuracy: 0.9995 - val_precision: 0.9500 - val_recall: 0.7308 - val_auc: 0.9356 - val_prc: 0.8393
Epoch 36/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0036 - tp: 207.0000 - fp: 30.0000 - tn: 181930.0000 - fn: 109.0000 - accuracy: 0.9992 - precision: 0.8734 - recall: 0.6551 - auc: 0.9296 - prc: 0.7619 - val_loss: 0.0028 - val_tp: 57.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 21.0000 - val_accuracy: 0.9995 - val_precision: 0.9500 - val_recall: 0.7308 - val_auc: 0.9356 - val_prc: 0.8390
Epoch 37/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0037 - tp: 194.0000 - fp: 29.0000 - tn: 181931.0000 - fn: 122.0000 - accuracy: 0.9992 - precision: 0.8700 - recall: 0.6139 - auc: 0.9327 - prc: 0.7550 - val_loss: 0.0027 - val_tp: 59.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 19.0000 - val_accuracy: 0.9995 - val_precision: 0.9516 - val_recall: 0.7564 - val_auc: 0.9356 - val_prc: 0.8396
Epoch 38/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0034 - tp: 209.0000 - fp: 27.0000 - tn: 181933.0000 - fn: 107.0000 - accuracy: 0.9993 - precision: 0.8856 - recall: 0.6614 - auc: 0.9391 - prc: 0.7804 - val_loss: 0.0027 - val_tp: 59.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 19.0000 - val_accuracy: 0.9995 - val_precision: 0.9516 - val_recall: 0.7564 - val_auc: 0.9356 - val_prc: 0.8412
Epoch 39/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - tp: 213.0000 - fp: 32.0000 - tn: 181928.0000 - fn: 103.0000 - accuracy: 0.9993 - precision: 0.8694 - recall: 0.6741 - auc: 0.9359 - prc: 0.7657 - val_loss: 0.0027 - val_tp: 60.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 18.0000 - val_accuracy: 0.9995 - val_precision: 0.9524 - val_recall: 0.7692 - val_auc: 0.9356 - val_prc: 0.8407
Epoch 40/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0034 - tp: 212.0000 - fp: 27.0000 - tn: 181933.0000 - fn: 104.0000 - accuracy: 0.9993 - precision: 0.8870 - recall: 0.6709 - auc: 0.9471 - prc: 0.7820 - val_loss: 0.0027 - val_tp: 62.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 16.0000 - val_accuracy: 0.9996 - val_precision: 0.9538 - val_recall: 0.7949 - val_auc: 0.9356 - val_prc: 0.8416
Epoch 41/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0037 - tp: 208.0000 - fp: 27.0000 - tn: 181933.0000 - fn: 108.0000 - accuracy: 0.9993 - precision: 0.8851 - recall: 0.6582 - auc: 0.9344 - prc: 0.7614 - val_loss: 0.0027 - val_tp: 62.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 16.0000 - val_accuracy: 0.9996 - val_precision: 0.9538 - val_recall: 0.7949 - val_auc: 0.9356 - val_prc: 0.8407
Epoch 42/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0037 - tp: 201.0000 - fp: 31.0000 - tn: 181929.0000 - fn: 115.0000 - accuracy: 0.9992 - precision: 0.8664 - recall: 0.6361 - auc: 0.9312 - prc: 0.7413 - val_loss: 0.0027 - val_tp: 62.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 16.0000 - val_accuracy: 0.9996 - val_precision: 0.9538 - val_recall: 0.7949 - val_auc: 0.9356 - val_prc: 0.8409
Epoch 43/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0036 - tp: 210.0000 - fp: 36.0000 - tn: 181924.0000 - fn: 106.0000 - accuracy: 0.9992 - precision: 0.8537 - recall: 0.6646 - auc: 0.9343 - prc: 0.7462 - val_loss: 0.0027 - val_tp: 57.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 21.0000 - val_accuracy: 0.9995 - val_precision: 0.9500 - val_recall: 0.7308 - val_auc: 0.9356 - val_prc: 0.8429
Epoch 44/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - tp: 206.0000 - fp: 27.0000 - tn: 181933.0000 - fn: 110.0000 - accuracy: 0.9992 - precision: 0.8841 - recall: 0.6519 - auc: 0.9454 - prc: 0.7644 - val_loss: 0.0027 - val_tp: 62.0000 - val_fp: 4.0000 - val_tn: 45487.0000 - val_fn: 16.0000 - val_accuracy: 0.9996 - val_precision: 0.9394 - val_recall: 0.7949 - val_auc: 0.9356 - val_prc: 0.8399
Epoch 45/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0036 - tp: 209.0000 - fp: 35.0000 - tn: 181925.0000 - fn: 107.0000 - accuracy: 0.9992 - precision: 0.8566 - recall: 0.6614 - auc: 0.9391 - prc: 0.7600 - val_loss: 0.0027 - val_tp: 61.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 17.0000 - val_accuracy: 0.9996 - val_precision: 0.9531 - val_recall: 0.7821 - val_auc: 0.9356 - val_prc: 0.8409
Epoch 46/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0034 - tp: 202.0000 - fp: 30.0000 - tn: 181930.0000 - fn: 114.0000 - accuracy: 0.9992 - precision: 0.8707 - recall: 0.6392 - auc: 0.9407 - prc: 0.7747 - val_loss: 0.0027 - val_tp: 62.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 16.0000 - val_accuracy: 0.9996 - val_precision: 0.9538 - val_recall: 0.7949 - val_auc: 0.9356 - val_prc: 0.8405
Epoch 47/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0037 - tp: 211.0000 - fp: 37.0000 - tn: 181923.0000 - fn: 105.0000 - accuracy: 0.9992 - precision: 0.8508 - recall: 0.6677 - auc: 0.9408 - prc: 0.7377 - val_loss: 0.0027 - val_tp: 59.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 19.0000 - val_accuracy: 0.9995 - val_precision: 0.9516 - val_recall: 0.7564 - val_auc: 0.9356 - val_prc: 0.8403
Epoch 48/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0034 - tp: 207.0000 - fp: 25.0000 - tn: 181935.0000 - fn: 109.0000 - accuracy: 0.9993 - precision: 0.8922 - recall: 0.6551 - auc: 0.9486 - prc: 0.7812 - val_loss: 0.0028 - val_tp: 59.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 19.0000 - val_accuracy: 0.9995 - val_precision: 0.9516 - val_recall: 0.7564 - val_auc: 0.9356 - val_prc: 0.8434
Epoch 49/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - tp: 216.0000 - fp: 35.0000 - tn: 181925.0000 - fn: 100.0000 - accuracy: 0.9993 - precision: 0.8606 - recall: 0.6835 - auc: 0.9359 - prc: 0.7643 - val_loss: 0.0028 - val_tp: 58.0000 - val_fp: 2.0000 - val_tn: 45489.0000 - val_fn: 20.0000 - val_accuracy: 0.9995 - val_precision: 0.9667 - val_recall: 0.7436 - val_auc: 0.9356 - val_prc: 0.8420
Epoch 50/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0034 - tp: 211.0000 - fp: 25.0000 - tn: 181935.0000 - fn: 105.0000 - accuracy: 0.9993 - precision: 0.8941 - recall: 0.6677 - auc: 0.9424 - prc: 0.7756 - val_loss: 0.0027 - val_tp: 62.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 16.0000 - val_accuracy: 0.9996 - val_precision: 0.9538 - val_recall: 0.7949 - val_auc: 0.9356 - val_prc: 0.8414
Epoch 51/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - tp: 221.0000 - fp: 28.0000 - tn: 181932.0000 - fn: 95.0000 - accuracy: 0.9993 - precision: 0.8876 - recall: 0.6994 - auc: 0.9361 - prc: 0.7819 - val_loss: 0.0028 - val_tp: 59.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 19.0000 - val_accuracy: 0.9995 - val_precision: 0.9516 - val_recall: 0.7564 - val_auc: 0.9356 - val_prc: 0.8410
Epoch 52/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - tp: 206.0000 - fp: 28.0000 - tn: 181932.0000 - fn: 110.0000 - accuracy: 0.9992 - precision: 0.8803 - recall: 0.6519 - auc: 0.9503 - prc: 0.7746 - val_loss: 0.0027 - val_tp: 62.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 16.0000 - val_accuracy: 0.9996 - val_precision: 0.9538 - val_recall: 0.7949 - val_auc: 0.9355 - val_prc: 0.8424
Epoch 53/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - tp: 210.0000 - fp: 36.0000 - tn: 181924.0000 - fn: 106.0000 - accuracy: 0.9992 - precision: 0.8537 - recall: 0.6646 - auc: 0.9439 - prc: 0.7790 - val_loss: 0.0027 - val_tp: 59.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 19.0000 - val_accuracy: 0.9995 - val_precision: 0.9516 - val_recall: 0.7564 - val_auc: 0.9356 - val_prc: 0.8440
Epoch 54/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - tp: 218.0000 - fp: 29.0000 - tn: 181931.0000 - fn: 98.0000 - accuracy: 0.9993 - precision: 0.8826 - recall: 0.6899 - auc: 0.9422 - prc: 0.7709 - val_loss: 0.0028 - val_tp: 58.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 20.0000 - val_accuracy: 0.9995 - val_precision: 0.9508 - val_recall: 0.7436 - val_auc: 0.9356 - val_prc: 0.8449
Epoch 55/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0034 - tp: 204.0000 - fp: 20.0000 - tn: 181940.0000 - fn: 112.0000 - accuracy: 0.9993 - precision: 0.9107 - recall: 0.6456 - auc: 0.9392 - prc: 0.7748 - val_loss: 0.0028 - val_tp: 62.0000 - val_fp: 4.0000 - val_tn: 45487.0000 - val_fn: 16.0000 - val_accuracy: 0.9996 - val_precision: 0.9394 - val_recall: 0.7949 - val_auc: 0.9356 - val_prc: 0.8396
Epoch 56/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - tp: 216.0000 - fp: 28.0000 - tn: 181932.0000 - fn: 100.0000 - accuracy: 0.9993 - precision: 0.8852 - recall: 0.6835 - auc: 0.9454 - prc: 0.7688 - val_loss: 0.0028 - val_tp: 59.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 19.0000 - val_accuracy: 0.9995 - val_precision: 0.9516 - val_recall: 0.7564 - val_auc: 0.9356 - val_prc: 0.8426
Epoch 57/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0036 - tp: 196.0000 - fp: 32.0000 - tn: 181928.0000 - fn: 120.0000 - accuracy: 0.9992 - precision: 0.8596 - recall: 0.6203 - auc: 0.9439 - prc: 0.7565 - val_loss: 0.0028 - val_tp: 55.0000 - val_fp: 2.0000 - val_tn: 45489.0000 - val_fn: 23.0000 - val_accuracy: 0.9995 - val_precision: 0.9649 - val_recall: 0.7051 - val_auc: 0.9356 - val_prc: 0.8434
Epoch 58/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - tp: 212.0000 - fp: 32.0000 - tn: 181928.0000 - fn: 104.0000 - accuracy: 0.9993 - precision: 0.8689 - recall: 0.6709 - auc: 0.9423 - prc: 0.7669 - val_loss: 0.0028 - val_tp: 61.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 17.0000 - val_accuracy: 0.9996 - val_precision: 0.9531 - val_recall: 0.7821 - val_auc: 0.9356 - val_prc: 0.8427
Epoch 59/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0034 - tp: 218.0000 - fp: 29.0000 - tn: 181931.0000 - fn: 98.0000 - accuracy: 0.9993 - precision: 0.8826 - recall: 0.6899 - auc: 0.9376 - prc: 0.7696 - val_loss: 0.0029 - val_tp: 55.0000 - val_fp: 2.0000 - val_tn: 45489.0000 - val_fn: 23.0000 - val_accuracy: 0.9995 - val_precision: 0.9649 - val_recall: 0.7051 - val_auc: 0.9356 - val_prc: 0.8431
Epoch 60/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0032 - tp: 206.0000 - fp: 27.0000 - tn: 181933.0000 - fn: 110.0000 - accuracy: 0.9992 - precision: 0.8841 - recall: 0.6519 - auc: 0.9408 - prc: 0.7846 - val_loss: 0.0028 - val_tp: 60.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 18.0000 - val_accuracy: 0.9995 - val_precision: 0.9524 - val_recall: 0.7692 - val_auc: 0.9356 - val_prc: 0.8428
Epoch 61/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - tp: 207.0000 - fp: 28.0000 - tn: 181932.0000 - fn: 109.0000 - accuracy: 0.9992 - precision: 0.8809 - recall: 0.6551 - auc: 0.9423 - prc: 0.7712 - val_loss: 0.0028 - val_tp: 59.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 19.0000 - val_accuracy: 0.9995 - val_precision: 0.9516 - val_recall: 0.7564 - val_auc: 0.9356 - val_prc: 0.8415
Epoch 62/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0037 - tp: 206.0000 - fp: 27.0000 - tn: 181933.0000 - fn: 110.0000 - accuracy: 0.9992 - precision: 0.8841 - recall: 0.6519 - auc: 0.9439 - prc: 0.7576 - val_loss: 0.0028 - val_tp: 57.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 21.0000 - val_accuracy: 0.9995 - val_precision: 0.9500 - val_recall: 0.7308 - val_auc: 0.9356 - val_prc: 0.8434
Epoch 63/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - tp: 206.0000 - fp: 30.0000 - tn: 181930.0000 - fn: 110.0000 - accuracy: 0.9992 - precision: 0.8729 - recall: 0.6519 - auc: 0.9408 - prc: 0.7946 - val_loss: 0.0028 - val_tp: 58.0000 - val_fp: 2.0000 - val_tn: 45489.0000 - val_fn: 20.0000 - val_accuracy: 0.9995 - val_precision: 0.9667 - val_recall: 0.7436 - val_auc: 0.9356 - val_prc: 0.8437
Epoch 64/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0034 - tp: 211.0000 - fp: 32.0000 - tn: 181928.0000 - fn: 105.0000 - accuracy: 0.9992 - precision: 0.8683 - recall: 0.6677 - auc: 0.9392 - prc: 0.7674 - val_loss: 0.0028 - val_tp: 58.0000 - val_fp: 2.0000 - val_tn: 45489.0000 - val_fn: 20.0000 - val_accuracy: 0.9995 - val_precision: 0.9667 - val_recall: 0.7436 - val_auc: 0.9356 - val_prc: 0.8449
Epoch 65/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - tp: 208.0000 - fp: 27.0000 - tn: 181933.0000 - fn: 108.0000 - accuracy: 0.9993 - precision: 0.8851 - recall: 0.6582 - auc: 0.9455 - prc: 0.7834 - val_loss: 0.0028 - val_tp: 62.0000 - val_fp: 4.0000 - val_tn: 45487.0000 - val_fn: 16.0000 - val_accuracy: 0.9996 - val_precision: 0.9394 - val_recall: 0.7949 - val_auc: 0.9356 - val_prc: 0.8436
Epoch 66/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0034 - tp: 214.0000 - fp: 31.0000 - tn: 181929.0000 - fn: 102.0000 - accuracy: 0.9993 - precision: 0.8735 - recall: 0.6772 - auc: 0.9407 - prc: 0.7718 - val_loss: 0.0028 - val_tp: 58.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 20.0000 - val_accuracy: 0.9995 - val_precision: 0.9508 - val_recall: 0.7436 - val_auc: 0.9355 - val_prc: 0.8438
Epoch 67/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - tp: 206.0000 - fp: 25.0000 - tn: 181935.0000 - fn: 110.0000 - accuracy: 0.9993 - precision: 0.8918 - recall: 0.6519 - auc: 0.9343 - prc: 0.7609 - val_loss: 0.0028 - val_tp: 59.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 19.0000 - val_accuracy: 0.9995 - val_precision: 0.9516 - val_recall: 0.7564 - val_auc: 0.9356 - val_prc: 0.8432
Epoch 68/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - tp: 207.0000 - fp: 30.0000 - tn: 181930.0000 - fn: 109.0000 - accuracy: 0.9992 - precision: 0.8734 - recall: 0.6551 - auc: 0.9423 - prc: 0.7658 - val_loss: 0.0029 - val_tp: 56.0000 - val_fp: 2.0000 - val_tn: 45489.0000 - val_fn: 22.0000 - val_accuracy: 0.9995 - val_precision: 0.9655 - val_recall: 0.7179 - val_auc: 0.9355 - val_prc: 0.8434
Epoch 69/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - tp: 201.0000 - fp: 26.0000 - tn: 181934.0000 - fn: 115.0000 - accuracy: 0.9992 - precision: 0.8855 - recall: 0.6361 - auc: 0.9439 - prc: 0.7919 - val_loss: 0.0028 - val_tp: 62.0000 - val_fp: 4.0000 - val_tn: 45487.0000 - val_fn: 16.0000 - val_accuracy: 0.9996 - val_precision: 0.9394 - val_recall: 0.7949 - val_auc: 0.9355 - val_prc: 0.8437
Epoch 70/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - tp: 215.0000 - fp: 32.0000 - tn: 181928.0000 - fn: 101.0000 - accuracy: 0.9993 - precision: 0.8704 - recall: 0.6804 - auc: 0.9454 - prc: 0.7920 - val_loss: 0.0028 - val_tp: 58.0000 - val_fp: 2.0000 - val_tn: 45489.0000 - val_fn: 20.0000 - val_accuracy: 0.9995 - val_precision: 0.9667 - val_recall: 0.7436 - val_auc: 0.9356 - val_prc: 0.8448
Epoch 71/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - tp: 209.0000 - fp: 28.0000 - tn: 181932.0000 - fn: 107.0000 - accuracy: 0.9993 - precision: 0.8819 - recall: 0.6614 - auc: 0.9471 - prc: 0.7806 - val_loss: 0.0028 - val_tp: 61.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 17.0000 - val_accuracy: 0.9996 - val_precision: 0.9531 - val_recall: 0.7821 - val_auc: 0.9356 - val_prc: 0.8444
Epoch 72/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - tp: 215.0000 - fp: 34.0000 - tn: 181926.0000 - fn: 101.0000 - accuracy: 0.9993 - precision: 0.8635 - recall: 0.6804 - auc: 0.9486 - prc: 0.7795 - val_loss: 0.0028 - val_tp: 58.0000 - val_fp: 2.0000 - val_tn: 45489.0000 - val_fn: 20.0000 - val_accuracy: 0.9995 - val_precision: 0.9667 - val_recall: 0.7436 - val_auc: 0.9356 - val_prc: 0.8449
Epoch 73/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0030 - tp: 225.0000 - fp: 23.0000 - tn: 181937.0000 - fn: 91.0000 - accuracy: 0.9994 - precision: 0.9073 - recall: 0.7120 - auc: 0.9471 - prc: 0.8059 - val_loss: 0.0028 - val_tp: 62.0000 - val_fp: 4.0000 - val_tn: 45487.0000 - val_fn: 16.0000 - val_accuracy: 0.9996 - val_precision: 0.9394 - val_recall: 0.7949 - val_auc: 0.9356 - val_prc: 0.8450
Epoch 74/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0032 - tp: 212.0000 - fp: 26.0000 - tn: 181934.0000 - fn: 104.0000 - accuracy: 0.9993 - precision: 0.8908 - recall: 0.6709 - auc: 0.9439 - prc: 0.7912 - val_loss: 0.0028 - val_tp: 59.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 19.0000 - val_accuracy: 0.9995 - val_precision: 0.9516 - val_recall: 0.7564 - val_auc: 0.9356 - val_prc: 0.8430
Epoch 75/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - tp: 206.0000 - fp: 30.0000 - tn: 181930.0000 - fn: 110.0000 - accuracy: 0.9992 - precision: 0.8729 - recall: 0.6519 - auc: 0.9439 - prc: 0.7782 - val_loss: 0.0029 - val_tp: 57.0000 - val_fp: 2.0000 - val_tn: 45489.0000 - val_fn: 21.0000 - val_accuracy: 0.9995 - val_precision: 0.9661 - val_recall: 0.7308 - val_auc: 0.9356 - val_prc: 0.8450
Epoch 76/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - tp: 217.0000 - fp: 25.0000 - tn: 181935.0000 - fn: 99.0000 - accuracy: 0.9993 - precision: 0.8967 - recall: 0.6867 - auc: 0.9470 - prc: 0.7746 - val_loss: 0.0028 - val_tp: 58.0000 - val_fp: 2.0000 - val_tn: 45489.0000 - val_fn: 20.0000 - val_accuracy: 0.9995 - val_precision: 0.9667 - val_recall: 0.7436 - val_auc: 0.9355 - val_prc: 0.8441
Epoch 77/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0034 - tp: 212.0000 - fp: 32.0000 - tn: 181928.0000 - fn: 104.0000 - accuracy: 0.9993 - precision: 0.8689 - recall: 0.6709 - auc: 0.9486 - prc: 0.7558 - val_loss: 0.0029 - val_tp: 58.0000 - val_fp: 2.0000 - val_tn: 45489.0000 - val_fn: 20.0000 - val_accuracy: 0.9995 - val_precision: 0.9667 - val_recall: 0.7436 - val_auc: 0.9356 - val_prc: 0.8448
Epoch 78/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0032 - tp: 208.0000 - fp: 19.0000 - tn: 181941.0000 - fn: 108.0000 - accuracy: 0.9993 - precision: 0.9163 - recall: 0.6582 - auc: 0.9438 - prc: 0.7994 - val_loss: 0.0028 - val_tp: 62.0000 - val_fp: 4.0000 - val_tn: 45487.0000 - val_fn: 16.0000 - val_accuracy: 0.9996 - val_precision: 0.9394 - val_recall: 0.7949 - val_auc: 0.9356 - val_prc: 0.8425
Epoch 79/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - tp: 223.0000 - fp: 32.0000 - tn: 181928.0000 - fn: 93.0000 - accuracy: 0.9993 - precision: 0.8745 - recall: 0.7057 - auc: 0.9408 - prc: 0.7799 - val_loss: 0.0028 - val_tp: 61.0000 - val_fp: 4.0000 - val_tn: 45487.0000 - val_fn: 17.0000 - val_accuracy: 0.9995 - val_precision: 0.9385 - val_recall: 0.7821 - val_auc: 0.9356 - val_prc: 0.8444
Epoch 80/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0032 - tp: 215.0000 - fp: 25.0000 - tn: 181935.0000 - fn: 101.0000 - accuracy: 0.9993 - precision: 0.8958 - recall: 0.6804 - auc: 0.9455 - prc: 0.7974 - val_loss: 0.0029 - val_tp: 58.0000 - val_fp: 2.0000 - val_tn: 45489.0000 - val_fn: 20.0000 - val_accuracy: 0.9995 - val_precision: 0.9667 - val_recall: 0.7436 - val_auc: 0.9356 - val_prc: 0.8440
Epoch 81/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0032 - tp: 217.0000 - fp: 26.0000 - tn: 181934.0000 - fn: 99.0000 - accuracy: 0.9993 - precision: 0.8930 - recall: 0.6867 - auc: 0.9440 - prc: 0.7879 - val_loss: 0.0028 - val_tp: 58.0000 - val_fp: 2.0000 - val_tn: 45489.0000 - val_fn: 20.0000 - val_accuracy: 0.9995 - val_precision: 0.9667 - val_recall: 0.7436 - val_auc: 0.9356 - val_prc: 0.8454
Epoch 82/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0032 - tp: 218.0000 - fp: 27.0000 - tn: 181933.0000 - fn: 98.0000 - accuracy: 0.9993 - precision: 0.8898 - recall: 0.6899 - auc: 0.9455 - prc: 0.7850 - val_loss: 0.0028 - val_tp: 58.0000 - val_fp: 2.0000 - val_tn: 45489.0000 - val_fn: 20.0000 - val_accuracy: 0.9995 - val_precision: 0.9667 - val_recall: 0.7436 - val_auc: 0.9356 - val_prc: 0.8441
Epoch 83/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0032 - tp: 211.0000 - fp: 25.0000 - tn: 181935.0000 - fn: 105.0000 - accuracy: 0.9993 - precision: 0.8941 - recall: 0.6677 - auc: 0.9517 - prc: 0.7934 - val_loss: 0.0030 - val_tp: 57.0000 - val_fp: 2.0000 - val_tn: 45489.0000 - val_fn: 21.0000 - val_accuracy: 0.9995 - val_precision: 0.9661 - val_recall: 0.7308 - val_auc: 0.9356 - val_prc: 0.8425
Epoch 84/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0032 - tp: 211.0000 - fp: 19.0000 - tn: 181941.0000 - fn: 105.0000 - accuracy: 0.9993 - precision: 0.9174 - recall: 0.6677 - auc: 0.9423 - prc: 0.7966 - val_loss: 0.0028 - val_tp: 58.0000 - val_fp: 2.0000 - val_tn: 45489.0000 - val_fn: 20.0000 - val_accuracy: 0.9995 - val_precision: 0.9667 - val_recall: 0.7436 - val_auc: 0.9355 - val_prc: 0.8432
Epoch 85/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0031 - tp: 226.0000 - fp: 28.0000 - tn: 181932.0000 - fn: 90.0000 - accuracy: 0.9994 - precision: 0.8898 - recall: 0.7152 - auc: 0.9454 - prc: 0.7955 - val_loss: 0.0029 - val_tp: 59.0000 - val_fp: 2.0000 - val_tn: 45489.0000 - val_fn: 19.0000 - val_accuracy: 0.9995 - val_precision: 0.9672 - val_recall: 0.7564 - val_auc: 0.9356 - val_prc: 0.8437
Epoch 86/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0031 - tp: 221.0000 - fp: 27.0000 - tn: 181933.0000 - fn: 95.0000 - accuracy: 0.9993 - precision: 0.8911 - recall: 0.6994 - auc: 0.9502 - prc: 0.8023 - val_loss: 0.0029 - val_tp: 58.0000 - val_fp: 2.0000 - val_tn: 45489.0000 - val_fn: 20.0000 - val_accuracy: 0.9995 - val_precision: 0.9667 - val_recall: 0.7436 - val_auc: 0.9356 - val_prc: 0.8399
Epoch 87/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - tp: 210.0000 - fp: 25.0000 - tn: 181935.0000 - fn: 106.0000 - accuracy: 0.9993 - precision: 0.8936 - recall: 0.6646 - auc: 0.9549 - prc: 0.7780 - val_loss: 0.0028 - val_tp: 60.0000 - val_fp: 4.0000 - val_tn: 45487.0000 - val_fn: 18.0000 - val_accuracy: 0.9995 - val_precision: 0.9375 - val_recall: 0.7692 - val_auc: 0.9355 - val_prc: 0.8439
Epoch 88/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0032 - tp: 218.0000 - fp: 25.0000 - tn: 181935.0000 - fn: 98.0000 - accuracy: 0.9993 - precision: 0.8971 - recall: 0.6899 - auc: 0.9471 - prc: 0.7884 - val_loss: 0.0029 - val_tp: 58.0000 - val_fp: 2.0000 - val_tn: 45489.0000 - val_fn: 20.0000 - val_accuracy: 0.9995 - val_precision: 0.9667 - val_recall: 0.7436 - val_auc: 0.9356 - val_prc: 0.8442
Epoch 89/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0031 - tp: 224.0000 - fp: 25.0000 - tn: 181935.0000 - fn: 92.0000 - accuracy: 0.9994 - precision: 0.8996 - recall: 0.7089 - auc: 0.9471 - prc: 0.7974 - val_loss: 0.0029 - val_tp: 61.0000 - val_fp: 3.0000 - val_tn: 45488.0000 - val_fn: 17.0000 - val_accuracy: 0.9996 - val_precision: 0.9531 - val_recall: 0.7821 - val_auc: 0.9356 - val_prc: 0.8451
Epoch 90/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0032 - tp: 213.0000 - fp: 23.0000 - tn: 181937.0000 - fn: 103.0000 - accuracy: 0.9993 - precision: 0.9025 - recall: 0.6741 - auc: 0.9471 - prc: 0.7968 - val_loss: 0.0029 - val_tp: 61.0000 - val_fp: 4.0000 - val_tn: 45487.0000 - val_fn: 17.0000 - val_accuracy: 0.9995 - val_precision: 0.9385 - val_recall: 0.7821 - val_auc: 0.9356 - val_prc: 0.8449
Epoch 91/100
85/90 [===========================>..] - ETA: 0s - loss: 0.0032 - tp: 222.0000 - fp: 24.0000 - tn: 173750.0000 - fn: 84.0000 - accuracy: 0.9994 - precision: 0.9024 - recall: 0.7255 - auc: 0.9454 - prc: 0.7865Restoring model weights from the end of the best epoch: 81.
90/90 [==============================] - 0s 5ms/step - loss: 0.0031 - tp: 230.0000 - fp: 24.0000 - tn: 181936.0000 - fn: 86.0000 - accuracy: 0.9994 - precision: 0.9055 - recall: 0.7278 - auc: 0.9440 - prc: 0.7877 - val_loss: 0.0029 - val_tp: 61.0000 - val_fp: 4.0000 - val_tn: 45487.0000 - val_fn: 17.0000 - val_accuracy: 0.9995 - val_precision: 0.9385 - val_recall: 0.7821 - val_auc: 0.9356 - val_prc: 0.8439
Epoch 91: 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 the Overfit and underfit 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', 'prc', '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)
90/90 [==============================] - 0s 1ms/step
28/28 [==============================] - 0s 1ms/step
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.0036825225688517094
tp :  63.0
fp :  4.0
tn :  56860.0
fn :  35.0
accuracy :  0.9993153214454651
precision :  0.9402984976768494
recall :  0.6428571343421936
auc :  0.912825345993042
prc :  0.7844073176383972

Legitimate Transactions Detected (True Negatives):  56860
Legitimate Transactions Incorrectly Detected (False Positives):  4
Fraudulent Transactions Missed (False Negatives):  35
Fraudulent Transactions Detected (True Positives):  63
Total Fraudulent Transactions:  98

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');

png

Plot the AUPRC

Now plot the AUPRC. Area under the interpolated precision-recall curve, obtained by plotting (recall, precision) points for different values of the classification threshold. Depending on how it's calculated, PR AUC may be equivalent to the average precision of the model.

def plot_prc(name, labels, predictions, **kwargs):
    precision, recall, _ = sklearn.metrics.precision_recall_curve(labels, predictions)

    plt.plot(precision, recall, label=name, linewidth=2, **kwargs)
    plt.xlabel('Precision')
    plt.ylabel('Recall')
    plt.grid(True)
    ax = plt.gca()
    ax.set_aspect('equal')
plot_prc("Train Baseline", train_labels, train_predictions_baseline, color=colors[0])
plot_prc("Test Baseline", test_labels, test_predictions_baseline, color=colors[0], linestyle='--')
plt.legend(loc='lower right');

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 fraudulent 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)
Epoch 1/100
90/90 [==============================] - 2s 10ms/step - loss: 1.1051 - tp: 199.0000 - fp: 940.0000 - tn: 237884.0000 - fn: 215.0000 - accuracy: 0.9952 - precision: 0.1747 - recall: 0.4807 - auc: 0.8791 - prc: 0.1703 - val_loss: 0.0183 - val_tp: 57.0000 - val_fp: 137.0000 - val_tn: 45354.0000 - val_fn: 21.0000 - val_accuracy: 0.9965 - val_precision: 0.2938 - val_recall: 0.7308 - val_auc: 0.9575 - val_prc: 0.4388
Epoch 2/100
90/90 [==============================] - 0s 5ms/step - loss: 0.5908 - tp: 210.0000 - fp: 1537.0000 - tn: 180423.0000 - fn: 106.0000 - accuracy: 0.9910 - precision: 0.1202 - recall: 0.6646 - auc: 0.9269 - prc: 0.2400 - val_loss: 0.0276 - val_tp: 64.0000 - val_fp: 245.0000 - val_tn: 45246.0000 - val_fn: 14.0000 - val_accuracy: 0.9943 - val_precision: 0.2071 - val_recall: 0.8205 - val_auc: 0.9662 - val_prc: 0.5206
Epoch 3/100
90/90 [==============================] - 0s 5ms/step - loss: 0.3410 - tp: 256.0000 - fp: 2273.0000 - tn: 179687.0000 - fn: 60.0000 - accuracy: 0.9872 - precision: 0.1012 - recall: 0.8101 - auc: 0.9566 - prc: 0.2985 - val_loss: 0.0365 - val_tp: 66.0000 - val_fp: 340.0000 - val_tn: 45151.0000 - val_fn: 12.0000 - val_accuracy: 0.9923 - val_precision: 0.1626 - val_recall: 0.8462 - val_auc: 0.9686 - val_prc: 0.5456
Epoch 4/100
90/90 [==============================] - 0s 5ms/step - loss: 0.3697 - tp: 251.0000 - fp: 3100.0000 - tn: 178860.0000 - fn: 65.0000 - accuracy: 0.9826 - precision: 0.0749 - recall: 0.7943 - auc: 0.9463 - prc: 0.2574 - val_loss: 0.0479 - val_tp: 69.0000 - val_fp: 461.0000 - val_tn: 45030.0000 - val_fn: 9.0000 - val_accuracy: 0.9897 - val_precision: 0.1302 - val_recall: 0.8846 - val_auc: 0.9720 - val_prc: 0.5245
Epoch 5/100
90/90 [==============================] - 0s 5ms/step - loss: 0.2880 - tp: 269.0000 - fp: 4151.0000 - tn: 177809.0000 - fn: 47.0000 - accuracy: 0.9770 - precision: 0.0609 - recall: 0.8513 - auc: 0.9585 - prc: 0.2597 - val_loss: 0.0565 - val_tp: 69.0000 - val_fp: 561.0000 - val_tn: 44930.0000 - val_fn: 9.0000 - val_accuracy: 0.9875 - val_precision: 0.1095 - val_recall: 0.8846 - val_auc: 0.9729 - val_prc: 0.5047
Epoch 6/100
90/90 [==============================] - 0s 5ms/step - loss: 0.2795 - tp: 272.0000 - fp: 5254.0000 - tn: 176706.0000 - fn: 44.0000 - accuracy: 0.9709 - precision: 0.0492 - recall: 0.8608 - auc: 0.9603 - prc: 0.2438 - val_loss: 0.0672 - val_tp: 70.0000 - val_fp: 755.0000 - val_tn: 44736.0000 - val_fn: 8.0000 - val_accuracy: 0.9833 - val_precision: 0.0848 - val_recall: 0.8974 - val_auc: 0.9738 - val_prc: 0.4665
Epoch 7/100
90/90 [==============================] - 0s 5ms/step - loss: 0.2460 - tp: 278.0000 - fp: 5985.0000 - tn: 175975.0000 - fn: 38.0000 - accuracy: 0.9670 - precision: 0.0444 - recall: 0.8797 - auc: 0.9661 - prc: 0.2263 - val_loss: 0.0743 - val_tp: 70.0000 - val_fp: 842.0000 - val_tn: 44649.0000 - val_fn: 8.0000 - val_accuracy: 0.9813 - val_precision: 0.0768 - val_recall: 0.8974 - val_auc: 0.9759 - val_prc: 0.4568
Epoch 8/100
90/90 [==============================] - 0s 5ms/step - loss: 0.2423 - tp: 276.0000 - fp: 6348.0000 - tn: 175612.0000 - fn: 40.0000 - accuracy: 0.9650 - precision: 0.0417 - recall: 0.8734 - auc: 0.9684 - prc: 0.2196 - val_loss: 0.0814 - val_tp: 71.0000 - val_fp: 924.0000 - val_tn: 44567.0000 - val_fn: 7.0000 - val_accuracy: 0.9796 - val_precision: 0.0714 - val_recall: 0.9103 - val_auc: 0.9763 - val_prc: 0.4717
Epoch 9/100
90/90 [==============================] - 0s 5ms/step - loss: 0.2155 - tp: 281.0000 - fp: 6729.0000 - tn: 175231.0000 - fn: 35.0000 - accuracy: 0.9629 - precision: 0.0401 - recall: 0.8892 - auc: 0.9729 - prc: 0.2216 - val_loss: 0.0829 - val_tp: 70.0000 - val_fp: 936.0000 - val_tn: 44555.0000 - val_fn: 8.0000 - val_accuracy: 0.9793 - val_precision: 0.0696 - val_recall: 0.8974 - val_auc: 0.9767 - val_prc: 0.4687
Epoch 10/100
90/90 [==============================] - 0s 5ms/step - loss: 0.2016 - tp: 286.0000 - fp: 6663.0000 - tn: 175297.0000 - fn: 30.0000 - accuracy: 0.9633 - precision: 0.0412 - recall: 0.9051 - auc: 0.9750 - prc: 0.2183 - val_loss: 0.0814 - val_tp: 70.0000 - val_fp: 916.0000 - val_tn: 44575.0000 - val_fn: 8.0000 - val_accuracy: 0.9797 - val_precision: 0.0710 - val_recall: 0.8974 - val_auc: 0.9770 - val_prc: 0.4740
Epoch 11/100
90/90 [==============================] - 0s 5ms/step - loss: 0.2341 - tp: 278.0000 - fp: 6699.0000 - tn: 175261.0000 - fn: 38.0000 - accuracy: 0.9630 - precision: 0.0398 - recall: 0.8797 - auc: 0.9678 - prc: 0.2248 - val_loss: 0.0845 - val_tp: 70.0000 - val_fp: 938.0000 - val_tn: 44553.0000 - val_fn: 8.0000 - val_accuracy: 0.9792 - val_precision: 0.0694 - val_recall: 0.8974 - val_auc: 0.9771 - val_prc: 0.4869
Epoch 12/100
90/90 [==============================] - 0s 5ms/step - loss: 0.1889 - tp: 287.0000 - fp: 6676.0000 - tn: 175284.0000 - fn: 29.0000 - accuracy: 0.9632 - precision: 0.0412 - recall: 0.9082 - auc: 0.9775 - prc: 0.2305 - val_loss: 0.0854 - val_tp: 70.0000 - val_fp: 934.0000 - val_tn: 44557.0000 - val_fn: 8.0000 - val_accuracy: 0.9793 - val_precision: 0.0697 - val_recall: 0.8974 - val_auc: 0.9772 - val_prc: 0.4913
Epoch 13/100
83/90 [==========================>...] - ETA: 0s - loss: 0.2193 - tp: 257.0000 - fp: 6451.0000 - tn: 163244.0000 - fn: 32.0000 - accuracy: 0.9619 - precision: 0.0383 - recall: 0.8893 - auc: 0.9719 - prc: 0.2160Restoring model weights from the end of the best epoch: 3.
90/90 [==============================] - 0s 5ms/step - loss: 0.2133 - tp: 283.0000 - fp: 6936.0000 - tn: 175024.0000 - fn: 33.0000 - accuracy: 0.9618 - precision: 0.0392 - recall: 0.8956 - auc: 0.9735 - prc: 0.2175 - val_loss: 0.0890 - val_tp: 70.0000 - val_fp: 986.0000 - val_tn: 44505.0000 - val_fn: 8.0000 - val_accuracy: 0.9782 - val_precision: 0.0663 - val_recall: 0.8974 - val_auc: 0.9772 - val_prc: 0.4834
Epoch 13: 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)
90/90 [==============================] - 0s 1ms/step
28/28 [==============================] - 0s 1ms/step
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.035842619836330414
tp :  76.0
fp :  403.0
tn :  56461.0
fn :  22.0
accuracy :  0.9925388693809509
precision :  0.15866388380527496
recall :  0.7755101919174194
auc :  0.9489410519599915
prc :  0.5275198817253113

Legitimate Transactions Detected (True Negatives):  56461
Legitimate Transactions Incorrectly Detected (False Positives):  403
Fraudulent Transactions Missed (False Negatives):  22
Fraudulent Transactions Detected (True Positives):  76
Total Fraudulent Transactions:  98

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');

png

Plot the AUPRC

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

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


plt.legend(loc='lower right');

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
(181960, 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
(363920, 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:
 [ 0.55048622  0.58281053 -0.18054636  1.93583095  0.29554659 -0.23724915
  0.03108875  0.14900966 -0.87832068 -0.17802301  2.09779545 -0.27353277
 -1.19726626 -1.95269012  0.43650731  1.5548122   1.78388368  1.22320993
 -1.93671114 -0.22109894 -0.01110755 -0.07843217 -0.08560816 -0.04293247
  0.76732118  0.15200643  0.06685898  0.18756067 -4.85856533]

Label:  1

Merge the two together using tf.data.Dataset.sample_from_datasets:

resampled_ds = tf.data.Dataset.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.52490234375

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)
Epoch 1/100
278/278 [==============================] - 8s 22ms/step - loss: 0.3953 - tp: 266190.0000 - fp: 98443.0000 - tn: 243783.0000 - fn: 17890.0000 - accuracy: 0.8143 - precision: 0.7300 - recall: 0.9370 - auc: 0.9499 - prc: 0.9521 - val_loss: 0.2553 - val_tp: 71.0000 - val_fp: 1519.0000 - val_tn: 43972.0000 - val_fn: 7.0000 - val_accuracy: 0.9665 - val_precision: 0.0447 - val_recall: 0.9103 - val_auc: 0.9714 - val_prc: 0.7439
Epoch 2/100
278/278 [==============================] - 6s 21ms/step - loss: 0.1833 - tp: 267167.0000 - fp: 16878.0000 - tn: 267871.0000 - fn: 17428.0000 - accuracy: 0.9397 - precision: 0.9406 - recall: 0.9388 - auc: 0.9824 - prc: 0.9856 - val_loss: 0.1188 - val_tp: 70.0000 - val_fp: 945.0000 - val_tn: 44546.0000 - val_fn: 8.0000 - val_accuracy: 0.9791 - val_precision: 0.0690 - val_recall: 0.8974 - val_auc: 0.9720 - val_prc: 0.7543
Epoch 3/100
278/278 [==============================] - 6s 21ms/step - loss: 0.1389 - tp: 270484.0000 - fp: 10829.0000 - tn: 273565.0000 - fn: 14466.0000 - accuracy: 0.9556 - precision: 0.9615 - recall: 0.9492 - auc: 0.9897 - prc: 0.9910 - val_loss: 0.0821 - val_tp: 71.0000 - val_fp: 821.0000 - val_tn: 44670.0000 - val_fn: 7.0000 - val_accuracy: 0.9818 - val_precision: 0.0796 - val_recall: 0.9103 - val_auc: 0.9735 - val_prc: 0.7605
Epoch 4/100
278/278 [==============================] - 6s 21ms/step - loss: 0.1189 - tp: 272082.0000 - fp: 9401.0000 - tn: 274818.0000 - fn: 13043.0000 - accuracy: 0.9606 - precision: 0.9666 - recall: 0.9543 - auc: 0.9925 - prc: 0.9932 - val_loss: 0.0660 - val_tp: 71.0000 - val_fp: 768.0000 - val_tn: 44723.0000 - val_fn: 7.0000 - val_accuracy: 0.9830 - val_precision: 0.0846 - val_recall: 0.9103 - val_auc: 0.9764 - val_prc: 0.7499
Epoch 5/100
278/278 [==============================] - 6s 22ms/step - loss: 0.1067 - tp: 272546.0000 - fp: 8597.0000 - tn: 276303.0000 - fn: 11898.0000 - accuracy: 0.9640 - precision: 0.9694 - recall: 0.9582 - auc: 0.9941 - prc: 0.9945 - val_loss: 0.0570 - val_tp: 71.0000 - val_fp: 726.0000 - val_tn: 44765.0000 - val_fn: 7.0000 - val_accuracy: 0.9839 - val_precision: 0.0891 - val_recall: 0.9103 - val_auc: 0.9742 - val_prc: 0.7620
Epoch 6/100
278/278 [==============================] - 6s 22ms/step - loss: 0.0981 - tp: 274110.0000 - fp: 8094.0000 - tn: 276017.0000 - fn: 11123.0000 - accuracy: 0.9662 - precision: 0.9713 - recall: 0.9610 - auc: 0.9950 - prc: 0.9953 - val_loss: 0.0497 - val_tp: 70.0000 - val_fp: 656.0000 - val_tn: 44835.0000 - val_fn: 8.0000 - val_accuracy: 0.9854 - val_precision: 0.0964 - val_recall: 0.8974 - val_auc: 0.9747 - val_prc: 0.7522
Epoch 7/100
278/278 [==============================] - 6s 21ms/step - loss: 0.0920 - tp: 273331.0000 - fp: 7512.0000 - tn: 278163.0000 - fn: 10338.0000 - accuracy: 0.9686 - precision: 0.9733 - recall: 0.9636 - auc: 0.9956 - prc: 0.9958 - val_loss: 0.0463 - val_tp: 70.0000 - val_fp: 642.0000 - val_tn: 44849.0000 - val_fn: 8.0000 - val_accuracy: 0.9857 - val_precision: 0.0983 - val_recall: 0.8974 - val_auc: 0.9753 - val_prc: 0.7434
Epoch 8/100
278/278 [==============================] - 6s 21ms/step - loss: 0.0875 - tp: 274791.0000 - fp: 7283.0000 - tn: 277374.0000 - fn: 9896.0000 - accuracy: 0.9698 - precision: 0.9742 - recall: 0.9652 - auc: 0.9960 - prc: 0.9960 - val_loss: 0.0429 - val_tp: 70.0000 - val_fp: 614.0000 - val_tn: 44877.0000 - val_fn: 8.0000 - val_accuracy: 0.9864 - val_precision: 0.1023 - val_recall: 0.8974 - val_auc: 0.9750 - val_prc: 0.7431
Epoch 9/100
278/278 [==============================] - 6s 21ms/step - loss: 0.0845 - tp: 275501.0000 - fp: 7006.0000 - tn: 277151.0000 - fn: 9686.0000 - accuracy: 0.9707 - precision: 0.9752 - recall: 0.9660 - auc: 0.9963 - prc: 0.9962 - val_loss: 0.0398 - val_tp: 70.0000 - val_fp: 575.0000 - val_tn: 44916.0000 - val_fn: 8.0000 - val_accuracy: 0.9872 - val_precision: 0.1085 - val_recall: 0.8974 - val_auc: 0.9746 - val_prc: 0.7448
Epoch 10/100
278/278 [==============================] - 6s 21ms/step - loss: 0.0820 - tp: 275185.0000 - fp: 6785.0000 - tn: 277837.0000 - fn: 9537.0000 - accuracy: 0.9713 - precision: 0.9759 - recall: 0.9665 - auc: 0.9965 - prc: 0.9964 - val_loss: 0.0398 - val_tp: 70.0000 - val_fp: 585.0000 - val_tn: 44906.0000 - val_fn: 8.0000 - val_accuracy: 0.9870 - val_precision: 0.1069 - val_recall: 0.8974 - val_auc: 0.9698 - val_prc: 0.7276
Epoch 11/100
278/278 [==============================] - 6s 22ms/step - loss: 0.0802 - tp: 274757.0000 - fp: 6654.0000 - tn: 278097.0000 - fn: 9836.0000 - accuracy: 0.9710 - precision: 0.9764 - recall: 0.9654 - auc: 0.9966 - prc: 0.9964 - val_loss: 0.0372 - val_tp: 70.0000 - val_fp: 535.0000 - val_tn: 44956.0000 - val_fn: 8.0000 - val_accuracy: 0.9881 - val_precision: 0.1157 - val_recall: 0.8974 - val_auc: 0.9598 - val_prc: 0.7362
Epoch 12/100
278/278 [==============================] - 6s 21ms/step - loss: 0.0770 - tp: 275513.0000 - fp: 6520.0000 - tn: 277555.0000 - fn: 9756.0000 - accuracy: 0.9714 - precision: 0.9769 - recall: 0.9658 - auc: 0.9969 - prc: 0.9966 - val_loss: 0.0359 - val_tp: 70.0000 - val_fp: 509.0000 - val_tn: 44982.0000 - val_fn: 8.0000 - val_accuracy: 0.9887 - val_precision: 0.1209 - val_recall: 0.8974 - val_auc: 0.9603 - val_prc: 0.7277
Epoch 13/100
278/278 [==============================] - 6s 22ms/step - loss: 0.0756 - tp: 275057.0000 - fp: 6341.0000 - tn: 278182.0000 - fn: 9764.0000 - accuracy: 0.9717 - precision: 0.9775 - recall: 0.9657 - auc: 0.9969 - prc: 0.9966 - val_loss: 0.0357 - val_tp: 70.0000 - val_fp: 510.0000 - val_tn: 44981.0000 - val_fn: 8.0000 - val_accuracy: 0.9886 - val_precision: 0.1207 - val_recall: 0.8974 - val_auc: 0.9605 - val_prc: 0.7187
Epoch 14/100
278/278 [==============================] - 6s 22ms/step - loss: 0.0744 - tp: 274646.0000 - fp: 6413.0000 - tn: 278439.0000 - fn: 9846.0000 - accuracy: 0.9714 - precision: 0.9772 - recall: 0.9654 - auc: 0.9970 - prc: 0.9967 - val_loss: 0.0345 - val_tp: 70.0000 - val_fp: 498.0000 - val_tn: 44993.0000 - val_fn: 8.0000 - val_accuracy: 0.9889 - val_precision: 0.1232 - val_recall: 0.8974 - val_auc: 0.9610 - val_prc: 0.7263
Epoch 15/100
277/278 [============================>.] - ETA: 0s - loss: 0.0730 - tp: 274045.0000 - fp: 6136.0000 - tn: 277277.0000 - fn: 9838.0000 - accuracy: 0.9718 - precision: 0.9781 - recall: 0.9653 - auc: 0.9971 - prc: 0.9967Restoring model weights from the end of the best epoch: 5.
278/278 [==============================] - 6s 22ms/step - loss: 0.0731 - tp: 274998.0000 - fp: 6165.0000 - tn: 278305.0000 - fn: 9876.0000 - accuracy: 0.9718 - precision: 0.9781 - recall: 0.9653 - auc: 0.9971 - prc: 0.9967 - val_loss: 0.0332 - val_tp: 70.0000 - val_fp: 475.0000 - val_tn: 45016.0000 - val_fn: 8.0000 - val_accuracy: 0.9894 - val_precision: 0.1284 - val_recall: 0.8974 - val_auc: 0.9609 - val_prc: 0.7184
Epoch 15: 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 tf.keras.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))
Epoch 1/1000
20/20 [==============================] - 2s 48ms/step - loss: 0.7082 - tp: 19210.0000 - fp: 16080.0000 - tn: 49818.0000 - fn: 1421.0000 - accuracy: 0.7977 - precision: 0.5443 - recall: 0.9311 - auc: 0.9420 - prc: 0.8495 - val_loss: 1.0950 - val_tp: 77.0000 - val_fp: 37185.0000 - val_tn: 8306.0000 - val_fn: 1.0000 - val_accuracy: 0.1840 - val_precision: 0.0021 - val_recall: 0.9872 - val_auc: 0.9385 - val_prc: 0.1285
Epoch 2/1000
20/20 [==============================] - 0s 25ms/step - loss: 0.6165 - tp: 19339.0000 - fp: 14306.0000 - tn: 6312.0000 - fn: 1003.0000 - accuracy: 0.6262 - precision: 0.5748 - recall: 0.9507 - auc: 0.8784 - prc: 0.9025 - val_loss: 0.9397 - val_tp: 77.0000 - val_fp: 32360.0000 - val_tn: 13131.0000 - val_fn: 1.0000 - val_accuracy: 0.2898 - val_precision: 0.0024 - val_recall: 0.9872 - val_auc: 0.9517 - val_prc: 0.2705
Epoch 3/1000
20/20 [==============================] - 0s 25ms/step - loss: 0.5398 - tp: 19449.0000 - fp: 12293.0000 - tn: 8103.0000 - fn: 1115.0000 - accuracy: 0.6727 - precision: 0.6127 - recall: 0.9458 - auc: 0.9007 - prc: 0.9236 - val_loss: 0.8122 - val_tp: 76.0000 - val_fp: 26523.0000 - val_tn: 18968.0000 - val_fn: 2.0000 - val_accuracy: 0.4179 - val_precision: 0.0029 - val_recall: 0.9744 - val_auc: 0.9573 - val_prc: 0.3688
Epoch 4/1000
20/20 [==============================] - 1s 28ms/step - loss: 0.4875 - tp: 19338.0000 - fp: 10595.0000 - tn: 9845.0000 - fn: 1182.0000 - accuracy: 0.7125 - precision: 0.6460 - recall: 0.9424 - auc: 0.9159 - prc: 0.9351 - val_loss: 0.7119 - val_tp: 75.0000 - val_fp: 20469.0000 - val_tn: 25022.0000 - val_fn: 3.0000 - val_accuracy: 0.5507 - val_precision: 0.0037 - val_recall: 0.9615 - val_auc: 0.9606 - val_prc: 0.4602
Epoch 5/1000
20/20 [==============================] - 0s 26ms/step - loss: 0.4359 - tp: 19308.0000 - fp: 8902.0000 - tn: 11492.0000 - fn: 1258.0000 - accuracy: 0.7520 - precision: 0.6844 - recall: 0.9388 - auc: 0.9307 - prc: 0.9478 - val_loss: 0.6299 - val_tp: 73.0000 - val_fp: 14903.0000 - val_tn: 30588.0000 - val_fn: 5.0000 - val_accuracy: 0.6728 - val_precision: 0.0049 - val_recall: 0.9359 - val_auc: 0.9631 - val_prc: 0.5622
Epoch 6/1000
20/20 [==============================] - 1s 26ms/step - loss: 0.3978 - tp: 19253.0000 - fp: 7458.0000 - tn: 12991.0000 - fn: 1258.0000 - accuracy: 0.7872 - precision: 0.7208 - recall: 0.9387 - auc: 0.9413 - prc: 0.9555 - val_loss: 0.5609 - val_tp: 73.0000 - val_fp: 10238.0000 - val_tn: 35253.0000 - val_fn: 5.0000 - val_accuracy: 0.7752 - val_precision: 0.0071 - val_recall: 0.9359 - val_auc: 0.9650 - val_prc: 0.6037
Epoch 7/1000
20/20 [==============================] - 1s 29ms/step - loss: 0.3643 - tp: 19202.0000 - fp: 6165.0000 - tn: 14316.0000 - fn: 1277.0000 - accuracy: 0.8183 - precision: 0.7570 - recall: 0.9376 - auc: 0.9495 - prc: 0.9622 - val_loss: 0.5022 - val_tp: 71.0000 - val_fp: 7201.0000 - val_tn: 38290.0000 - val_fn: 7.0000 - val_accuracy: 0.8418 - val_precision: 0.0098 - val_recall: 0.9103 - val_auc: 0.9663 - val_prc: 0.6312
Epoch 8/1000
20/20 [==============================] - 1s 26ms/step - loss: 0.3375 - tp: 19147.0000 - fp: 5184.0000 - tn: 15278.0000 - fn: 1351.0000 - accuracy: 0.8405 - precision: 0.7869 - recall: 0.9341 - auc: 0.9550 - prc: 0.9663 - val_loss: 0.4509 - val_tp: 71.0000 - val_fp: 5169.0000 - val_tn: 40322.0000 - val_fn: 7.0000 - val_accuracy: 0.8864 - val_precision: 0.0135 - val_recall: 0.9103 - val_auc: 0.9663 - val_prc: 0.6690
Epoch 9/1000
20/20 [==============================] - 0s 26ms/step - loss: 0.3150 - tp: 18960.0000 - fp: 4401.0000 - tn: 16263.0000 - fn: 1336.0000 - accuracy: 0.8599 - precision: 0.8116 - recall: 0.9342 - auc: 0.9602 - prc: 0.9695 - val_loss: 0.4056 - val_tp: 71.0000 - val_fp: 3801.0000 - val_tn: 41690.0000 - val_fn: 7.0000 - val_accuracy: 0.9164 - val_precision: 0.0183 - val_recall: 0.9103 - val_auc: 0.9667 - val_prc: 0.7005
Epoch 10/1000
20/20 [==============================] - 0s 25ms/step - loss: 0.2941 - tp: 18982.0000 - fp: 3661.0000 - tn: 16968.0000 - fn: 1349.0000 - accuracy: 0.8777 - precision: 0.8383 - recall: 0.9336 - auc: 0.9640 - prc: 0.9724 - val_loss: 0.3666 - val_tp: 71.0000 - val_fp: 2966.0000 - val_tn: 42525.0000 - val_fn: 7.0000 - val_accuracy: 0.9348 - val_precision: 0.0234 - val_recall: 0.9103 - val_auc: 0.9672 - val_prc: 0.7174
Epoch 11/1000
20/20 [==============================] - 0s 26ms/step - loss: 0.2761 - tp: 19136.0000 - fp: 3149.0000 - tn: 17327.0000 - fn: 1348.0000 - accuracy: 0.8902 - precision: 0.8587 - recall: 0.9342 - auc: 0.9668 - prc: 0.9750 - val_loss: 0.3312 - val_tp: 71.0000 - val_fp: 2313.0000 - val_tn: 43178.0000 - val_fn: 7.0000 - val_accuracy: 0.9491 - val_precision: 0.0298 - val_recall: 0.9103 - val_auc: 0.9675 - val_prc: 0.7197
Epoch 12/1000
20/20 [==============================] - 1s 26ms/step - loss: 0.2567 - tp: 19069.0000 - fp: 2596.0000 - tn: 17897.0000 - fn: 1398.0000 - accuracy: 0.9025 - precision: 0.8802 - recall: 0.9317 - auc: 0.9702 - prc: 0.9773 - val_loss: 0.3015 - val_tp: 71.0000 - val_fp: 1941.0000 - val_tn: 43550.0000 - val_fn: 7.0000 - val_accuracy: 0.9573 - val_precision: 0.0353 - val_recall: 0.9103 - val_auc: 0.9688 - val_prc: 0.7271
Epoch 13/1000
20/20 [==============================] - 0s 26ms/step - loss: 0.2436 - tp: 19061.0000 - fp: 2231.0000 - tn: 18273.0000 - fn: 1395.0000 - accuracy: 0.9115 - precision: 0.8952 - recall: 0.9318 - auc: 0.9714 - prc: 0.9782 - val_loss: 0.2768 - val_tp: 71.0000 - val_fp: 1739.0000 - val_tn: 43752.0000 - val_fn: 7.0000 - val_accuracy: 0.9617 - val_precision: 0.0392 - val_recall: 0.9103 - val_auc: 0.9696 - val_prc: 0.7420
Epoch 14/1000
20/20 [==============================] - 1s 27ms/step - loss: 0.2312 - tp: 19207.0000 - fp: 1977.0000 - tn: 18422.0000 - fn: 1354.0000 - accuracy: 0.9187 - precision: 0.9067 - recall: 0.9341 - auc: 0.9748 - prc: 0.9806 - val_loss: 0.2546 - val_tp: 71.0000 - val_fp: 1521.0000 - val_tn: 43970.0000 - val_fn: 7.0000 - val_accuracy: 0.9665 - val_precision: 0.0446 - val_recall: 0.9103 - val_auc: 0.9706 - val_prc: 0.7461
Epoch 15/1000
20/20 [==============================] - 1s 27ms/step - loss: 0.2196 - tp: 19217.0000 - fp: 1740.0000 - tn: 18646.0000 - fn: 1357.0000 - accuracy: 0.9244 - precision: 0.9170 - recall: 0.9340 - auc: 0.9763 - prc: 0.9816 - val_loss: 0.2352 - val_tp: 71.0000 - val_fp: 1395.0000 - val_tn: 44096.0000 - val_fn: 7.0000 - val_accuracy: 0.9692 - val_precision: 0.0484 - val_recall: 0.9103 - val_auc: 0.9713 - val_prc: 0.7466
Epoch 16/1000
20/20 [==============================] - 1s 26ms/step - loss: 0.2156 - tp: 19112.0000 - fp: 1690.0000 - tn: 18852.0000 - fn: 1306.0000 - accuracy: 0.9269 - precision: 0.9188 - recall: 0.9360 - auc: 0.9774 - prc: 0.9819 - val_loss: 0.2171 - val_tp: 71.0000 - val_fp: 1289.0000 - val_tn: 44202.0000 - val_fn: 7.0000 - val_accuracy: 0.9716 - val_precision: 0.0522 - val_recall: 0.9103 - val_auc: 0.9719 - val_prc: 0.7580
Epoch 17/1000
20/20 [==============================] - 0s 25ms/step - loss: 0.2056 - tp: 18850.0000 - fp: 1416.0000 - tn: 19353.0000 - fn: 1341.0000 - accuracy: 0.9327 - precision: 0.9301 - recall: 0.9336 - auc: 0.9787 - prc: 0.9826 - val_loss: 0.2012 - val_tp: 71.0000 - val_fp: 1207.0000 - val_tn: 44284.0000 - val_fn: 7.0000 - val_accuracy: 0.9734 - val_precision: 0.0556 - val_recall: 0.9103 - val_auc: 0.9717 - val_prc: 0.7593
Epoch 18/1000
20/20 [==============================] - 1s 26ms/step - loss: 0.1967 - tp: 19276.0000 - fp: 1330.0000 - tn: 19018.0000 - fn: 1336.0000 - accuracy: 0.9349 - precision: 0.9355 - recall: 0.9352 - auc: 0.9796 - prc: 0.9840 - val_loss: 0.1890 - val_tp: 71.0000 - val_fp: 1174.0000 - val_tn: 44317.0000 - val_fn: 7.0000 - val_accuracy: 0.9741 - val_precision: 0.0570 - val_recall: 0.9103 - val_auc: 0.9718 - val_prc: 0.7607
Epoch 19/1000
20/20 [==============================] - 1s 27ms/step - loss: 0.1895 - tp: 19395.0000 - fp: 1294.0000 - tn: 18968.0000 - fn: 1303.0000 - accuracy: 0.9366 - precision: 0.9375 - recall: 0.9370 - auc: 0.9810 - prc: 0.9850 - val_loss: 0.1789 - val_tp: 71.0000 - val_fp: 1164.0000 - val_tn: 44327.0000 - val_fn: 7.0000 - val_accuracy: 0.9743 - val_precision: 0.0575 - val_recall: 0.9103 - val_auc: 0.9725 - val_prc: 0.7632
Epoch 20/1000
20/20 [==============================] - 1s 26ms/step - loss: 0.1887 - tp: 19314.0000 - fp: 1251.0000 - tn: 19127.0000 - fn: 1268.0000 - accuracy: 0.9385 - precision: 0.9392 - recall: 0.9384 - auc: 0.9810 - prc: 0.9848 - val_loss: 0.1683 - val_tp: 71.0000 - val_fp: 1098.0000 - val_tn: 44393.0000 - val_fn: 7.0000 - val_accuracy: 0.9758 - val_precision: 0.0607 - val_recall: 0.9103 - val_auc: 0.9725 - val_prc: 0.7634
Epoch 21/1000
20/20 [==============================] - 0s 25ms/step - loss: 0.1808 - tp: 19232.0000 - fp: 1134.0000 - tn: 19326.0000 - fn: 1268.0000 - accuracy: 0.9414 - precision: 0.9443 - recall: 0.9381 - auc: 0.9827 - prc: 0.9860 - val_loss: 0.1600 - val_tp: 70.0000 - val_fp: 1081.0000 - val_tn: 44410.0000 - val_fn: 8.0000 - val_accuracy: 0.9761 - val_precision: 0.0608 - val_recall: 0.8974 - val_auc: 0.9729 - val_prc: 0.7634
Epoch 22/1000
20/20 [==============================] - 0s 25ms/step - loss: 0.1745 - tp: 19270.0000 - fp: 1069.0000 - tn: 19396.0000 - fn: 1225.0000 - accuracy: 0.9440 - precision: 0.9474 - recall: 0.9402 - auc: 0.9839 - prc: 0.9869 - val_loss: 0.1527 - val_tp: 70.0000 - val_fp: 1081.0000 - val_tn: 44410.0000 - val_fn: 8.0000 - val_accuracy: 0.9761 - val_precision: 0.0608 - val_recall: 0.8974 - val_auc: 0.9728 - val_prc: 0.7540
Epoch 23/1000
20/20 [==============================] - 1s 26ms/step - loss: 0.1729 - tp: 19304.0000 - fp: 1082.0000 - tn: 19360.0000 - fn: 1214.0000 - accuracy: 0.9439 - precision: 0.9469 - recall: 0.9408 - auc: 0.9842 - prc: 0.9869 - val_loss: 0.1453 - val_tp: 70.0000 - val_fp: 1059.0000 - val_tn: 44432.0000 - val_fn: 8.0000 - val_accuracy: 0.9766 - val_precision: 0.0620 - val_recall: 0.8974 - val_auc: 0.9728 - val_prc: 0.7540
Epoch 24/1000
20/20 [==============================] - 1s 29ms/step - loss: 0.1705 - tp: 19168.0000 - fp: 1075.0000 - tn: 19550.0000 - fn: 1167.0000 - accuracy: 0.9453 - precision: 0.9469 - recall: 0.9426 - auc: 0.9849 - prc: 0.9870 - val_loss: 0.1380 - val_tp: 70.0000 - val_fp: 1012.0000 - val_tn: 44479.0000 - val_fn: 8.0000 - val_accuracy: 0.9776 - val_precision: 0.0647 - val_recall: 0.8974 - val_auc: 0.9723 - val_prc: 0.7537
Epoch 25/1000
20/20 [==============================] - 1s 26ms/step - loss: 0.1631 - tp: 19352.0000 - fp: 989.0000 - tn: 19448.0000 - fn: 1171.0000 - accuracy: 0.9473 - precision: 0.9514 - recall: 0.9429 - auc: 0.9855 - prc: 0.9880 - val_loss: 0.1327 - val_tp: 70.0000 - val_fp: 997.0000 - val_tn: 44494.0000 - val_fn: 8.0000 - val_accuracy: 0.9779 - val_precision: 0.0656 - val_recall: 0.8974 - val_auc: 0.9724 - val_prc: 0.7534
Epoch 26/1000
20/20 [==============================] - 0s 26ms/step - loss: 0.1620 - tp: 19327.0000 - fp: 999.0000 - tn: 19502.0000 - fn: 1132.0000 - accuracy: 0.9480 - precision: 0.9509 - recall: 0.9447 - auc: 0.9862 - prc: 0.9881 - val_loss: 0.1269 - val_tp: 70.0000 - val_fp: 974.0000 - val_tn: 44517.0000 - val_fn: 8.0000 - val_accuracy: 0.9785 - val_precision: 0.0670 - val_recall: 0.8974 - val_auc: 0.9724 - val_prc: 0.7539
Epoch 27/1000
20/20 [==============================] - 0s 26ms/step - loss: 0.1575 - tp: 19461.0000 - fp: 935.0000 - tn: 19421.0000 - fn: 1143.0000 - accuracy: 0.9493 - precision: 0.9542 - recall: 0.9445 - auc: 0.9867 - prc: 0.9888 - val_loss: 0.1222 - val_tp: 70.0000 - val_fp: 955.0000 - val_tn: 44536.0000 - val_fn: 8.0000 - val_accuracy: 0.9789 - val_precision: 0.0683 - val_recall: 0.8974 - val_auc: 0.9724 - val_prc: 0.7544
Epoch 28/1000
20/20 [==============================] - 1s 27ms/step - loss: 0.1521 - tp: 19288.0000 - fp: 858.0000 - tn: 19712.0000 - fn: 1102.0000 - accuracy: 0.9521 - precision: 0.9574 - recall: 0.9460 - auc: 0.9875 - prc: 0.9895 - val_loss: 0.1181 - val_tp: 70.0000 - val_fp: 954.0000 - val_tn: 44537.0000 - val_fn: 8.0000 - val_accuracy: 0.9789 - val_precision: 0.0684 - val_recall: 0.8974 - val_auc: 0.9722 - val_prc: 0.7543
Epoch 29/1000
20/20 [==============================] - 0s 26ms/step - loss: 0.1557 - tp: 19299.0000 - fp: 925.0000 - tn: 19639.0000 - fn: 1097.0000 - accuracy: 0.9506 - precision: 0.9543 - recall: 0.9462 - auc: 0.9869 - prc: 0.9887 - val_loss: 0.1134 - val_tp: 70.0000 - val_fp: 916.0000 - val_tn: 44575.0000 - val_fn: 8.0000 - val_accuracy: 0.9797 - val_precision: 0.0710 - val_recall: 0.8974 - val_auc: 0.9720 - val_prc: 0.7556
Epoch 30/1000
20/20 [==============================] - 1s 26ms/step - loss: 0.1505 - tp: 19378.0000 - fp: 869.0000 - tn: 19616.0000 - fn: 1097.0000 - accuracy: 0.9520 - precision: 0.9571 - recall: 0.9464 - auc: 0.9878 - prc: 0.9896 - val_loss: 0.1083 - val_tp: 70.0000 - val_fp: 886.0000 - val_tn: 44605.0000 - val_fn: 8.0000 - val_accuracy: 0.9804 - val_precision: 0.0732 - val_recall: 0.8974 - val_auc: 0.9717 - val_prc: 0.7474
Epoch 31/1000
20/20 [==============================] - ETA: 0s - loss: 0.1465 - tp: 19316.0000 - fp: 823.0000 - tn: 19730.0000 - fn: 1091.0000 - accuracy: 0.9533 - precision: 0.9591 - recall: 0.9465 - auc: 0.9885 - prc: 0.9901Restoring model weights from the end of the best epoch: 21.
20/20 [==============================] - 1s 26ms/step - loss: 0.1465 - tp: 19316.0000 - fp: 823.0000 - tn: 19730.0000 - fn: 1091.0000 - accuracy: 0.9533 - precision: 0.9591 - recall: 0.9465 - auc: 0.9885 - prc: 0.9901 - val_loss: 0.1048 - val_tp: 70.0000 - val_fp: 879.0000 - val_tn: 44612.0000 - val_fn: 8.0000 - val_accuracy: 0.9805 - val_precision: 0.0738 - val_recall: 0.8974 - val_auc: 0.9721 - val_prc: 0.7470
Epoch 31: 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)
90/90 [==============================] - 0s 1ms/step
28/28 [==============================] - 0s 1ms/step
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_resampled)
loss :  0.15973372757434845
tp :  84.0
fp :  1356.0
tn :  55508.0
fn :  14.0
accuracy :  0.9759488701820374
precision :  0.05833333358168602
recall :  0.8571428656578064
auc :  0.9540590047836304
prc :  0.6833170056343079

Legitimate Transactions Detected (True Negatives):  55508
Legitimate Transactions Incorrectly Detected (False Positives):  1356
Fraudulent Transactions Missed (False Negatives):  14
Fraudulent Transactions Detected (True Positives):  84
Total Fraudulent Transactions:  98

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');

png

Plot the AUPRC

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

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

plot_prc("Train Resampled", train_labels, train_predictions_resampled, color=colors[2])
plot_prc("Test Resampled", test_labels, test_predictions_resampled, color=colors[2], linestyle='--')
plt.legend(loc='lower right');

png

Applying this tutorial to your problem

Imbalanced data classification is an inherently 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.