Classification on imbalanced data

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

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

This tutorial contains complete code to:

  • Load a CSV file using Pandas.
  • Create train, validation, and test sets.
  • Define and train a model using Keras (including setting class weights).
  • Evaluate the model using various metrics (including precision and recall).
  • Try common 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
mpl.rcParams['figure.figsize'] = (12, 10)
colors = plt.rcParams['axes.prop_cycle'].by_key()['color']

Data processing and exploration

Download the Kaggle Credit Card Fraud data set

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

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

Examine the class label imbalance

Let's look at the dataset imbalance:

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


This shows the small fraction of positive samples.

Clean, split and normalize the data

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

cleaned_df = raw_df.copy()

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

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

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

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

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

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

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

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

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

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


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

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

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

Look at the data distribution

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

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

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

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

png

png

Define the model and metrics

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

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

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

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

  return model

Understanding useful metrics

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

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

Read more:

Baseline model

Build the model

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

EPOCHS = 100
BATCH_SIZE = 2048

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

Test run the model:

model.predict(train_features[:10])
array([[0.08342141],
       [0.45535946],
       [0.2788669 ],
       [0.34513476],
       [0.29530644],
       [0.1911721 ],
       [0.44964454],
       [0.6827736 ],
       [0.77349436],
       [0.28305683]], dtype=float32)

Optional: Set the correct initial bias.

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

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

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

The correct bias to set can be derived from:

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

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

It should be near: pos/total = 0.0018

model = make_model(output_bias = initial_bias)
model.predict(train_features[:10])
array([[0.00998453],
       [0.00131771],
       [0.00725579],
       [0.00147547],
       [0.0143338 ],
       [0.00557884],
       [0.00177143],
       [0.000911  ],
       [0.01347023],
       [0.00292194]], 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.0130

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

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

Checkpoint the initial weights

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

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

Confirm that the bias fix helps

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

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

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

png

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

Train the model

model = make_model()
model.load_weights(initial_weights)
baseline_history = model.fit(
    train_features,
    train_labels,
    batch_size=BATCH_SIZE,
    epochs=EPOCHS,
    callbacks = [early_stopping],
    validation_data=(val_features, val_labels))
Epoch 1/100
90/90 [==============================] - 1s 12ms/step - loss: 0.0126 - tp: 103.0000 - fp: 91.0000 - tn: 227357.0000 - fn: 294.0000 - accuracy: 0.9983 - precision: 0.5309 - recall: 0.2594 - auc: 0.8073 - val_loss: 0.0058 - val_tp: 20.0000 - val_fp: 3.0000 - val_tn: 45484.0000 - val_fn: 62.0000 - val_accuracy: 0.9986 - val_precision: 0.8696 - val_recall: 0.2439 - val_auc: 0.9252
Epoch 2/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0083 - tp: 93.0000 - fp: 24.0000 - tn: 181937.0000 - fn: 222.0000 - accuracy: 0.9987 - precision: 0.7949 - recall: 0.2952 - auc: 0.8471 - val_loss: 0.0046 - val_tp: 43.0000 - val_fp: 7.0000 - val_tn: 45480.0000 - val_fn: 39.0000 - val_accuracy: 0.9990 - val_precision: 0.8600 - val_recall: 0.5244 - val_auc: 0.9263
Epoch 3/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0062 - tp: 152.0000 - fp: 22.0000 - tn: 181939.0000 - fn: 163.0000 - accuracy: 0.9990 - precision: 0.8736 - recall: 0.4825 - auc: 0.8925 - val_loss: 0.0041 - val_tp: 47.0000 - val_fp: 7.0000 - val_tn: 45480.0000 - val_fn: 35.0000 - val_accuracy: 0.9991 - val_precision: 0.8704 - val_recall: 0.5732 - val_auc: 0.9325
Epoch 4/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0058 - tp: 160.0000 - fp: 22.0000 - tn: 181939.0000 - fn: 155.0000 - accuracy: 0.9990 - precision: 0.8791 - recall: 0.5079 - auc: 0.8865 - val_loss: 0.0039 - val_tp: 50.0000 - val_fp: 7.0000 - val_tn: 45480.0000 - val_fn: 32.0000 - val_accuracy: 0.9991 - val_precision: 0.8772 - val_recall: 0.6098 - val_auc: 0.9326
Epoch 5/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0057 - tp: 154.0000 - fp: 27.0000 - tn: 181934.0000 - fn: 161.0000 - accuracy: 0.9990 - precision: 0.8508 - recall: 0.4889 - auc: 0.9084 - val_loss: 0.0037 - val_tp: 52.0000 - val_fp: 7.0000 - val_tn: 45480.0000 - val_fn: 30.0000 - val_accuracy: 0.9992 - val_precision: 0.8814 - val_recall: 0.6341 - val_auc: 0.9448
Epoch 6/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0057 - tp: 168.0000 - fp: 24.0000 - tn: 181937.0000 - fn: 147.0000 - accuracy: 0.9991 - precision: 0.8750 - recall: 0.5333 - auc: 0.8849 - val_loss: 0.0036 - val_tp: 52.0000 - val_fp: 7.0000 - val_tn: 45480.0000 - val_fn: 30.0000 - val_accuracy: 0.9992 - val_precision: 0.8814 - val_recall: 0.6341 - val_auc: 0.9449
Epoch 7/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0053 - tp: 161.0000 - fp: 27.0000 - tn: 181934.0000 - fn: 154.0000 - accuracy: 0.9990 - precision: 0.8564 - recall: 0.5111 - auc: 0.8856 - val_loss: 0.0034 - val_tp: 55.0000 - val_fp: 7.0000 - val_tn: 45480.0000 - val_fn: 27.0000 - val_accuracy: 0.9993 - val_precision: 0.8871 - val_recall: 0.6707 - val_auc: 0.9448
Epoch 8/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0054 - tp: 156.0000 - fp: 34.0000 - tn: 181927.0000 - fn: 159.0000 - accuracy: 0.9989 - precision: 0.8211 - recall: 0.4952 - auc: 0.9032 - val_loss: 0.0033 - val_tp: 55.0000 - val_fp: 7.0000 - val_tn: 45480.0000 - val_fn: 27.0000 - val_accuracy: 0.9993 - val_precision: 0.8871 - val_recall: 0.6707 - val_auc: 0.9448
Epoch 9/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0054 - tp: 161.0000 - fp: 26.0000 - tn: 181935.0000 - fn: 154.0000 - accuracy: 0.9990 - precision: 0.8610 - recall: 0.5111 - auc: 0.9033 - val_loss: 0.0033 - val_tp: 57.0000 - val_fp: 7.0000 - val_tn: 45480.0000 - val_fn: 25.0000 - val_accuracy: 0.9993 - val_precision: 0.8906 - val_recall: 0.6951 - val_auc: 0.9509
Epoch 10/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0051 - tp: 163.0000 - fp: 29.0000 - tn: 181932.0000 - fn: 152.0000 - accuracy: 0.9990 - precision: 0.8490 - recall: 0.5175 - auc: 0.9033 - val_loss: 0.0032 - val_tp: 58.0000 - val_fp: 7.0000 - val_tn: 45480.0000 - val_fn: 24.0000 - val_accuracy: 0.9993 - val_precision: 0.8923 - val_recall: 0.7073 - val_auc: 0.9508
Epoch 11/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0054 - tp: 155.0000 - fp: 32.0000 - tn: 181929.0000 - fn: 160.0000 - accuracy: 0.9989 - precision: 0.8289 - recall: 0.4921 - auc: 0.8811 - val_loss: 0.0031 - val_tp: 58.0000 - val_fp: 7.0000 - val_tn: 45480.0000 - val_fn: 24.0000 - val_accuracy: 0.9993 - val_precision: 0.8923 - val_recall: 0.7073 - val_auc: 0.9509
Epoch 12/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0054 - tp: 160.0000 - fp: 30.0000 - tn: 181931.0000 - fn: 155.0000 - accuracy: 0.9990 - precision: 0.8421 - recall: 0.5079 - auc: 0.8972 - val_loss: 0.0031 - val_tp: 56.0000 - val_fp: 7.0000 - val_tn: 45480.0000 - val_fn: 26.0000 - val_accuracy: 0.9993 - val_precision: 0.8889 - val_recall: 0.6829 - val_auc: 0.9509
Epoch 13/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0047 - tp: 174.0000 - fp: 29.0000 - tn: 181932.0000 - fn: 141.0000 - accuracy: 0.9991 - precision: 0.8571 - recall: 0.5524 - auc: 0.8988 - val_loss: 0.0030 - val_tp: 59.0000 - val_fp: 7.0000 - val_tn: 45480.0000 - val_fn: 23.0000 - val_accuracy: 0.9993 - val_precision: 0.8939 - val_recall: 0.7195 - val_auc: 0.9509
Epoch 14/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0051 - tp: 156.0000 - fp: 25.0000 - tn: 181936.0000 - fn: 159.0000 - accuracy: 0.9990 - precision: 0.8619 - recall: 0.4952 - auc: 0.8876 - val_loss: 0.0029 - val_tp: 62.0000 - val_fp: 7.0000 - val_tn: 45480.0000 - val_fn: 20.0000 - val_accuracy: 0.9994 - val_precision: 0.8986 - val_recall: 0.7561 - val_auc: 0.9509
Epoch 15/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0049 - tp: 178.0000 - fp: 40.0000 - tn: 181921.0000 - fn: 137.0000 - accuracy: 0.9990 - precision: 0.8165 - recall: 0.5651 - auc: 0.9052 - val_loss: 0.0030 - val_tp: 56.0000 - val_fp: 7.0000 - val_tn: 45480.0000 - val_fn: 26.0000 - val_accuracy: 0.9993 - val_precision: 0.8889 - val_recall: 0.6829 - val_auc: 0.9509
Epoch 16/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0046 - tp: 161.0000 - fp: 19.0000 - tn: 181942.0000 - fn: 154.0000 - accuracy: 0.9991 - precision: 0.8944 - recall: 0.5111 - auc: 0.9035 - val_loss: 0.0028 - val_tp: 60.0000 - val_fp: 7.0000 - val_tn: 45480.0000 - val_fn: 22.0000 - val_accuracy: 0.9994 - val_precision: 0.8955 - val_recall: 0.7317 - val_auc: 0.9509
Epoch 17/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0045 - tp: 176.0000 - fp: 30.0000 - tn: 181931.0000 - fn: 139.0000 - accuracy: 0.9991 - precision: 0.8544 - recall: 0.5587 - auc: 0.9021 - val_loss: 0.0028 - val_tp: 62.0000 - val_fp: 7.0000 - val_tn: 45480.0000 - val_fn: 20.0000 - val_accuracy: 0.9994 - val_precision: 0.8986 - val_recall: 0.7561 - val_auc: 0.9449
Epoch 18/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0047 - tp: 171.0000 - fp: 32.0000 - tn: 181929.0000 - fn: 144.0000 - accuracy: 0.9990 - precision: 0.8424 - recall: 0.5429 - auc: 0.9021 - val_loss: 0.0028 - val_tp: 59.0000 - val_fp: 5.0000 - val_tn: 45482.0000 - val_fn: 23.0000 - val_accuracy: 0.9994 - val_precision: 0.9219 - val_recall: 0.7195 - val_auc: 0.9509
Epoch 19/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0045 - tp: 172.0000 - fp: 25.0000 - tn: 181936.0000 - fn: 143.0000 - accuracy: 0.9991 - precision: 0.8731 - recall: 0.5460 - auc: 0.9100 - val_loss: 0.0028 - val_tp: 64.0000 - val_fp: 8.0000 - val_tn: 45479.0000 - val_fn: 18.0000 - val_accuracy: 0.9994 - val_precision: 0.8889 - val_recall: 0.7805 - val_auc: 0.9509
Epoch 20/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0045 - tp: 170.0000 - fp: 30.0000 - tn: 181931.0000 - fn: 145.0000 - accuracy: 0.9990 - precision: 0.8500 - recall: 0.5397 - auc: 0.9131 - val_loss: 0.0027 - val_tp: 64.0000 - val_fp: 8.0000 - val_tn: 45479.0000 - val_fn: 18.0000 - val_accuracy: 0.9994 - val_precision: 0.8889 - val_recall: 0.7805 - val_auc: 0.9569
Epoch 21/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0048 - tp: 158.0000 - fp: 30.0000 - tn: 181931.0000 - fn: 157.0000 - accuracy: 0.9990 - precision: 0.8404 - recall: 0.5016 - auc: 0.8957 - val_loss: 0.0028 - val_tp: 63.0000 - val_fp: 8.0000 - val_tn: 45479.0000 - val_fn: 19.0000 - val_accuracy: 0.9994 - val_precision: 0.8873 - val_recall: 0.7683 - val_auc: 0.9570
Epoch 22/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0050 - tp: 164.0000 - fp: 35.0000 - tn: 181926.0000 - fn: 151.0000 - accuracy: 0.9990 - precision: 0.8241 - recall: 0.5206 - auc: 0.8988 - val_loss: 0.0028 - val_tp: 64.0000 - val_fp: 8.0000 - val_tn: 45479.0000 - val_fn: 18.0000 - val_accuracy: 0.9994 - val_precision: 0.8889 - val_recall: 0.7805 - val_auc: 0.9570
Epoch 23/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0047 - tp: 175.0000 - fp: 23.0000 - tn: 181938.0000 - fn: 140.0000 - accuracy: 0.9991 - precision: 0.8838 - recall: 0.5556 - auc: 0.8940 - val_loss: 0.0027 - val_tp: 64.0000 - val_fp: 8.0000 - val_tn: 45479.0000 - val_fn: 18.0000 - val_accuracy: 0.9994 - val_precision: 0.8889 - val_recall: 0.7805 - val_auc: 0.9569
Epoch 24/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0045 - tp: 170.0000 - fp: 34.0000 - tn: 181927.0000 - fn: 145.0000 - accuracy: 0.9990 - precision: 0.8333 - recall: 0.5397 - auc: 0.8989 - val_loss: 0.0027 - val_tp: 63.0000 - val_fp: 8.0000 - val_tn: 45479.0000 - val_fn: 19.0000 - val_accuracy: 0.9994 - val_precision: 0.8873 - val_recall: 0.7683 - val_auc: 0.9570
Epoch 25/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0044 - tp: 176.0000 - fp: 29.0000 - tn: 181932.0000 - fn: 139.0000 - accuracy: 0.9991 - precision: 0.8585 - recall: 0.5587 - auc: 0.9037 - val_loss: 0.0027 - val_tp: 63.0000 - val_fp: 8.0000 - val_tn: 45479.0000 - val_fn: 19.0000 - val_accuracy: 0.9994 - val_precision: 0.8873 - val_recall: 0.7683 - val_auc: 0.9570
Epoch 26/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0044 - tp: 177.0000 - fp: 31.0000 - tn: 181930.0000 - fn: 138.0000 - accuracy: 0.9991 - precision: 0.8510 - recall: 0.5619 - auc: 0.9164 - val_loss: 0.0027 - val_tp: 63.0000 - val_fp: 6.0000 - val_tn: 45481.0000 - val_fn: 19.0000 - val_accuracy: 0.9995 - val_precision: 0.9130 - val_recall: 0.7683 - val_auc: 0.9569
Epoch 27/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0044 - tp: 179.0000 - fp: 35.0000 - tn: 181926.0000 - fn: 136.0000 - accuracy: 0.9991 - precision: 0.8364 - recall: 0.5683 - auc: 0.9227 - val_loss: 0.0027 - val_tp: 64.0000 - val_fp: 8.0000 - val_tn: 45479.0000 - val_fn: 18.0000 - val_accuracy: 0.9994 - val_precision: 0.8889 - val_recall: 0.7805 - val_auc: 0.9570
Epoch 28/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0041 - tp: 187.0000 - fp: 25.0000 - tn: 181936.0000 - fn: 128.0000 - accuracy: 0.9992 - precision: 0.8821 - recall: 0.5937 - auc: 0.9243 - val_loss: 0.0026 - val_tp: 64.0000 - val_fp: 8.0000 - val_tn: 45479.0000 - val_fn: 18.0000 - val_accuracy: 0.9994 - val_precision: 0.8889 - val_recall: 0.7805 - val_auc: 0.9569
Epoch 29/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0044 - tp: 180.0000 - fp: 32.0000 - tn: 181929.0000 - fn: 135.0000 - accuracy: 0.9991 - precision: 0.8491 - recall: 0.5714 - auc: 0.9195 - val_loss: 0.0027 - val_tp: 64.0000 - val_fp: 8.0000 - val_tn: 45479.0000 - val_fn: 18.0000 - val_accuracy: 0.9994 - val_precision: 0.8889 - val_recall: 0.7805 - val_auc: 0.9569
Epoch 30/100
90/90 [==============================] - 0s 5ms/step - loss: 0.0044 - tp: 198.0000 - fp: 43.0000 - tn: 181918.0000 - fn: 117.0000 - accuracy: 0.9991 - precision: 0.8216 - recall: 0.6286 - auc: 0.9227 - val_loss: 0.0027 - val_tp: 65.0000 - val_fp: 8.0000 - val_tn: 45479.0000 - val_fn: 17.0000 - val_accuracy: 0.9995 - val_precision: 0.8904 - val_recall: 0.7927 - val_auc: 0.9569
Epoch 31/100
79/90 [=========================>....] - ETA: 0s - loss: 0.0047 - tp: 162.0000 - fp: 31.0000 - tn: 161480.0000 - fn: 119.0000 - accuracy: 0.9991 - precision: 0.8394 - recall: 0.5765 - auc: 0.9117Restoring model weights from the end of the best epoch.
90/90 [==============================] - 0s 5ms/step - loss: 0.0045 - tp: 180.0000 - fp: 33.0000 - tn: 181928.0000 - fn: 135.0000 - accuracy: 0.9991 - precision: 0.8451 - recall: 0.5714 - auc: 0.9132 - val_loss: 0.0027 - val_tp: 65.0000 - val_fp: 8.0000 - val_tn: 45479.0000 - val_fn: 17.0000 - val_accuracy: 0.9995 - val_precision: 0.8904 - val_recall: 0.7927 - val_auc: 0.9569
Epoch 00031: early stopping

Check training history

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

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

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

    plt.legend()

plot_metrics(baseline_history)

png

Evaluate metrics

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

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

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

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

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

plot_cm(test_labels, test_predictions_baseline)
loss :  0.0027596293948590755
tp :  75.0
fp :  8.0
tn :  56859.0
fn :  20.0
accuracy :  0.9995084404945374
precision :  0.9036144614219666
recall :  0.7894737124443054
auc :  0.9312645196914673

Legitimate Transactions Detected (True Negatives):  56859
Legitimate Transactions Incorrectly Detected (False Positives):  8
Fraudulent Transactions Missed (False Negatives):  20
Fraudulent Transactions Detected (True Positives):  75
Total Fraudulent Transactions:  95

png

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

Plot the ROC

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

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

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

png

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

Class weights

Calculate class weights

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

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

class_weight = {0: weight_for_0, 1: weight_for_1}

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

Train a model with class weights

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

weighted_model = make_model()
weighted_model.load_weights(initial_weights)

weighted_history = weighted_model.fit(
    train_features,
    train_labels,
    batch_size=BATCH_SIZE,
    epochs=EPOCHS,
    callbacks = [early_stopping],
    validation_data=(val_features, val_labels),
    # The class weights go here
    class_weight=class_weight) 
Epoch 1/100
90/90 [==============================] - 1s 13ms/step - loss: 1.6413 - tp: 167.0000 - fp: 427.0000 - tn: 238401.0000 - fn: 243.0000 - accuracy: 0.9972 - precision: 0.2811 - recall: 0.4073 - auc: 0.8362 - val_loss: 0.0087 - val_tp: 52.0000 - val_fp: 10.0000 - val_tn: 45477.0000 - val_fn: 30.0000 - val_accuracy: 0.9991 - val_precision: 0.8387 - val_recall: 0.6341 - val_auc: 0.9756
Epoch 2/100
90/90 [==============================] - 0s 5ms/step - loss: 0.8100 - tp: 182.0000 - fp: 1039.0000 - tn: 180922.0000 - fn: 133.0000 - accuracy: 0.9936 - precision: 0.1491 - recall: 0.5778 - auc: 0.8955 - val_loss: 0.0128 - val_tp: 65.0000 - val_fp: 22.0000 - val_tn: 45465.0000 - val_fn: 17.0000 - val_accuracy: 0.9991 - val_precision: 0.7471 - val_recall: 0.7927 - val_auc: 0.9786
Epoch 3/100
90/90 [==============================] - 0s 5ms/step - loss: 0.5497 - tp: 222.0000 - fp: 1925.0000 - tn: 180036.0000 - fn: 93.0000 - accuracy: 0.9889 - precision: 0.1034 - recall: 0.7048 - auc: 0.9317 - val_loss: 0.0190 - val_tp: 67.0000 - val_fp: 68.0000 - val_tn: 45419.0000 - val_fn: 15.0000 - val_accuracy: 0.9982 - val_precision: 0.4963 - val_recall: 0.8171 - val_auc: 0.9802
Epoch 4/100
90/90 [==============================] - 0s 5ms/step - loss: 0.4706 - tp: 237.0000 - fp: 3096.0000 - tn: 178865.0000 - fn: 78.0000 - accuracy: 0.9826 - precision: 0.0711 - recall: 0.7524 - auc: 0.9345 - val_loss: 0.0270 - val_tp: 68.0000 - val_fp: 147.0000 - val_tn: 45340.0000 - val_fn: 14.0000 - val_accuracy: 0.9965 - val_precision: 0.3163 - val_recall: 0.8293 - val_auc: 0.9854
Epoch 5/100
90/90 [==============================] - 0s 5ms/step - loss: 0.3786 - tp: 252.0000 - fp: 4186.0000 - tn: 177775.0000 - fn: 63.0000 - accuracy: 0.9767 - precision: 0.0568 - recall: 0.8000 - auc: 0.9410 - val_loss: 0.0356 - val_tp: 72.0000 - val_fp: 274.0000 - val_tn: 45213.0000 - val_fn: 10.0000 - val_accuracy: 0.9938 - val_precision: 0.2081 - val_recall: 0.8780 - val_auc: 0.9883
Epoch 6/100
90/90 [==============================] - 0s 5ms/step - loss: 0.3616 - tp: 261.0000 - fp: 5244.0000 - tn: 176717.0000 - fn: 54.0000 - accuracy: 0.9709 - precision: 0.0474 - recall: 0.8286 - auc: 0.9445 - val_loss: 0.0436 - val_tp: 73.0000 - val_fp: 441.0000 - val_tn: 45046.0000 - val_fn: 9.0000 - val_accuracy: 0.9901 - val_precision: 0.1420 - val_recall: 0.8902 - val_auc: 0.9895
Epoch 7/100
90/90 [==============================] - 0s 5ms/step - loss: 0.3383 - tp: 257.0000 - fp: 6060.0000 - tn: 175901.0000 - fn: 58.0000 - accuracy: 0.9664 - precision: 0.0407 - recall: 0.8159 - auc: 0.9480 - val_loss: 0.0519 - val_tp: 74.0000 - val_fp: 565.0000 - val_tn: 44922.0000 - val_fn: 8.0000 - val_accuracy: 0.9874 - val_precision: 0.1158 - val_recall: 0.9024 - val_auc: 0.9912
Epoch 8/100
90/90 [==============================] - 0s 5ms/step - loss: 0.3138 - tp: 267.0000 - fp: 6757.0000 - tn: 175204.0000 - fn: 48.0000 - accuracy: 0.9627 - precision: 0.0380 - recall: 0.8476 - auc: 0.9525 - val_loss: 0.0598 - val_tp: 75.0000 - val_fp: 661.0000 - val_tn: 44826.0000 - val_fn: 7.0000 - val_accuracy: 0.9853 - val_precision: 0.1019 - val_recall: 0.9146 - val_auc: 0.9921
Epoch 9/100
90/90 [==============================] - 0s 5ms/step - loss: 0.3275 - tp: 267.0000 - fp: 7236.0000 - tn: 174725.0000 - fn: 48.0000 - accuracy: 0.9600 - precision: 0.0356 - recall: 0.8476 - auc: 0.9472 - val_loss: 0.0647 - val_tp: 75.0000 - val_fp: 726.0000 - val_tn: 44761.0000 - val_fn: 7.0000 - val_accuracy: 0.9839 - val_precision: 0.0936 - val_recall: 0.9146 - val_auc: 0.9924
Epoch 10/100
90/90 [==============================] - 0s 5ms/step - loss: 0.2942 - tp: 270.0000 - fp: 7408.0000 - tn: 174553.0000 - fn: 45.0000 - accuracy: 0.9591 - precision: 0.0352 - recall: 0.8571 - auc: 0.9556 - val_loss: 0.0672 - val_tp: 75.0000 - val_fp: 748.0000 - val_tn: 44739.0000 - val_fn: 7.0000 - val_accuracy: 0.9834 - val_precision: 0.0911 - val_recall: 0.9146 - val_auc: 0.9927
Epoch 11/100
90/90 [==============================] - 0s 5ms/step - loss: 0.3036 - tp: 271.0000 - fp: 7517.0000 - tn: 174444.0000 - fn: 44.0000 - accuracy: 0.9585 - precision: 0.0348 - recall: 0.8603 - auc: 0.9524 - val_loss: 0.0698 - val_tp: 75.0000 - val_fp: 780.0000 - val_tn: 44707.0000 - val_fn: 7.0000 - val_accuracy: 0.9827 - val_precision: 0.0877 - val_recall: 0.9146 - val_auc: 0.9930
Epoch 12/100
90/90 [==============================] - 0s 5ms/step - loss: 0.3320 - tp: 267.0000 - fp: 7435.0000 - tn: 174526.0000 - fn: 48.0000 - accuracy: 0.9589 - precision: 0.0347 - recall: 0.8476 - auc: 0.9459 - val_loss: 0.0713 - val_tp: 76.0000 - val_fp: 793.0000 - val_tn: 44694.0000 - val_fn: 6.0000 - val_accuracy: 0.9825 - val_precision: 0.0875 - val_recall: 0.9268 - val_auc: 0.9929
Epoch 13/100
90/90 [==============================] - 0s 5ms/step - loss: 0.3061 - tp: 272.0000 - fp: 7481.0000 - tn: 174480.0000 - fn: 43.0000 - accuracy: 0.9587 - precision: 0.0351 - recall: 0.8635 - auc: 0.9501 - val_loss: 0.0726 - val_tp: 76.0000 - val_fp: 808.0000 - val_tn: 44679.0000 - val_fn: 6.0000 - val_accuracy: 0.9821 - val_precision: 0.0860 - val_recall: 0.9268 - val_auc: 0.9930
Epoch 14/100
90/90 [==============================] - 0s 5ms/step - loss: 0.2741 - tp: 276.0000 - fp: 7466.0000 - tn: 174495.0000 - fn: 39.0000 - accuracy: 0.9588 - precision: 0.0356 - recall: 0.8762 - auc: 0.9564 - val_loss: 0.0717 - val_tp: 76.0000 - val_fp: 777.0000 - val_tn: 44710.0000 - val_fn: 6.0000 - val_accuracy: 0.9828 - val_precision: 0.0891 - val_recall: 0.9268 - val_auc: 0.9932
Epoch 15/100
90/90 [==============================] - 0s 5ms/step - loss: 0.2588 - tp: 275.0000 - fp: 7284.0000 - tn: 174677.0000 - fn: 40.0000 - accuracy: 0.9598 - precision: 0.0364 - recall: 0.8730 - auc: 0.9637 - val_loss: 0.0745 - val_tp: 77.0000 - val_fp: 817.0000 - val_tn: 44670.0000 - val_fn: 5.0000 - val_accuracy: 0.9820 - val_precision: 0.0861 - val_recall: 0.9390 - val_auc: 0.9932
Epoch 16/100
90/90 [==============================] - 0s 5ms/step - loss: 0.2463 - tp: 277.0000 - fp: 7458.0000 - tn: 174503.0000 - fn: 38.0000 - accuracy: 0.9589 - precision: 0.0358 - recall: 0.8794 - auc: 0.9653 - val_loss: 0.0774 - val_tp: 77.0000 - val_fp: 833.0000 - val_tn: 44654.0000 - val_fn: 5.0000 - val_accuracy: 0.9816 - val_precision: 0.0846 - val_recall: 0.9390 - val_auc: 0.9934
Epoch 17/100
90/90 [==============================] - 0s 5ms/step - loss: 0.2564 - tp: 278.0000 - fp: 7354.0000 - tn: 174607.0000 - fn: 37.0000 - accuracy: 0.9595 - precision: 0.0364 - recall: 0.8825 - auc: 0.9604 - val_loss: 0.0747 - val_tp: 77.0000 - val_fp: 798.0000 - val_tn: 44689.0000 - val_fn: 5.0000 - val_accuracy: 0.9824 - val_precision: 0.0880 - val_recall: 0.9390 - val_auc: 0.9936
Epoch 18/100
90/90 [==============================] - 0s 5ms/step - loss: 0.2578 - tp: 275.0000 - fp: 7361.0000 - tn: 174600.0000 - fn: 40.0000 - accuracy: 0.9594 - precision: 0.0360 - recall: 0.8730 - auc: 0.9621 - val_loss: 0.0791 - val_tp: 77.0000 - val_fp: 836.0000 - val_tn: 44651.0000 - val_fn: 5.0000 - val_accuracy: 0.9815 - val_precision: 0.0843 - val_recall: 0.9390 - val_auc: 0.9934
Epoch 19/100
90/90 [==============================] - 0s 5ms/step - loss: 0.2597 - tp: 274.0000 - fp: 7694.0000 - tn: 174267.0000 - fn: 41.0000 - accuracy: 0.9576 - precision: 0.0344 - recall: 0.8698 - auc: 0.9614 - val_loss: 0.0826 - val_tp: 78.0000 - val_fp: 881.0000 - val_tn: 44606.0000 - val_fn: 4.0000 - val_accuracy: 0.9806 - val_precision: 0.0813 - val_recall: 0.9512 - val_auc: 0.9935
Epoch 20/100
90/90 [==============================] - 0s 5ms/step - loss: 0.2223 - tp: 281.0000 - fp: 7696.0000 - tn: 174265.0000 - fn: 34.0000 - accuracy: 0.9576 - precision: 0.0352 - recall: 0.8921 - auc: 0.9720 - val_loss: 0.0844 - val_tp: 78.0000 - val_fp: 903.0000 - val_tn: 44584.0000 - val_fn: 4.0000 - val_accuracy: 0.9801 - val_precision: 0.0795 - val_recall: 0.9512 - val_auc: 0.9933
Epoch 21/100
90/90 [==============================] - 0s 5ms/step - loss: 0.2572 - tp: 280.0000 - fp: 7744.0000 - tn: 174217.0000 - fn: 35.0000 - accuracy: 0.9573 - precision: 0.0349 - recall: 0.8889 - auc: 0.9599 - val_loss: 0.0825 - val_tp: 78.0000 - val_fp: 876.0000 - val_tn: 44611.0000 - val_fn: 4.0000 - val_accuracy: 0.9807 - val_precision: 0.0818 - val_recall: 0.9512 - val_auc: 0.9934
Epoch 22/100
90/90 [==============================] - 0s 5ms/step - loss: 0.2379 - tp: 276.0000 - fp: 7504.0000 - tn: 174457.0000 - fn: 39.0000 - accuracy: 0.9586 - precision: 0.0355 - recall: 0.8762 - auc: 0.9660 - val_loss: 0.0832 - val_tp: 78.0000 - val_fp: 882.0000 - val_tn: 44605.0000 - val_fn: 4.0000 - val_accuracy: 0.9806 - val_precision: 0.0812 - val_recall: 0.9512 - val_auc: 0.9932
Epoch 23/100
90/90 [==============================] - 0s 5ms/step - loss: 0.2448 - tp: 277.0000 - fp: 7444.0000 - tn: 174517.0000 - fn: 38.0000 - accuracy: 0.9590 - precision: 0.0359 - recall: 0.8794 - auc: 0.9667 - val_loss: 0.0835 - val_tp: 78.0000 - val_fp: 891.0000 - val_tn: 44596.0000 - val_fn: 4.0000 - val_accuracy: 0.9804 - val_precision: 0.0805 - val_recall: 0.9512 - val_auc: 0.9931
Epoch 24/100
90/90 [==============================] - 0s 5ms/step - loss: 0.2186 - tp: 285.0000 - fp: 7096.0000 - tn: 174865.0000 - fn: 30.0000 - accuracy: 0.9609 - precision: 0.0386 - recall: 0.9048 - auc: 0.9696 - val_loss: 0.0792 - val_tp: 77.0000 - val_fp: 838.0000 - val_tn: 44649.0000 - val_fn: 5.0000 - val_accuracy: 0.9815 - val_precision: 0.0842 - val_recall: 0.9390 - val_auc: 0.9930
Epoch 25/100
90/90 [==============================] - 0s 5ms/step - loss: 0.2718 - tp: 278.0000 - fp: 6685.0000 - tn: 175276.0000 - fn: 37.0000 - accuracy: 0.9631 - precision: 0.0399 - recall: 0.8825 - auc: 0.9541 - val_loss: 0.0758 - val_tp: 77.0000 - val_fp: 779.0000 - val_tn: 44708.0000 - val_fn: 5.0000 - val_accuracy: 0.9828 - val_precision: 0.0900 - val_recall: 0.9390 - val_auc: 0.9931
Epoch 26/100
90/90 [==============================] - 0s 5ms/step - loss: 0.1886 - tp: 284.0000 - fp: 6707.0000 - tn: 175254.0000 - fn: 31.0000 - accuracy: 0.9630 - precision: 0.0406 - recall: 0.9016 - auc: 0.9781 - val_loss: 0.0747 - val_tp: 77.0000 - val_fp: 767.0000 - val_tn: 44720.0000 - val_fn: 5.0000 - val_accuracy: 0.9831 - val_precision: 0.0912 - val_recall: 0.9390 - val_auc: 0.9931
Epoch 27/100
87/90 [============================>.] - ETA: 0s - loss: 0.2091 - tp: 270.0000 - fp: 6141.0000 - tn: 171731.0000 - fn: 34.0000 - accuracy: 0.9653 - precision: 0.0421 - recall: 0.8882 - auc: 0.9727Restoring model weights from the end of the best epoch.
90/90 [==============================] - 0s 5ms/step - loss: 0.2227 - tp: 277.0000 - fp: 6309.0000 - tn: 175652.0000 - fn: 38.0000 - accuracy: 0.9652 - precision: 0.0421 - recall: 0.8794 - auc: 0.9700 - val_loss: 0.0752 - val_tp: 77.0000 - val_fp: 767.0000 - val_tn: 44720.0000 - val_fn: 5.0000 - val_accuracy: 0.9831 - val_precision: 0.0912 - val_recall: 0.9390 - val_auc: 0.9929
Epoch 00027: early stopping

Check training history

plot_metrics(weighted_history)

png

Evaluate metrics

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

plot_cm(test_labels, test_predictions_weighted)
loss :  0.07350748032331467
tp :  84.0
fp :  959.0
tn :  55908.0
fn :  11.0
accuracy :  0.9829711318016052
precision :  0.08053690940141678
recall :  0.8842105269432068
auc :  0.9736334681510925

Legitimate Transactions Detected (True Negatives):  55908
Legitimate Transactions Incorrectly Detected (False Positives):  959
Fraudulent Transactions Missed (False Negatives):  11
Fraudulent Transactions Detected (True Positives):  84
Total Fraudulent Transactions:  95

png

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

Plot the ROC

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

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


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

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
(181961, 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
(363922, 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:
 [-5.          3.92538883 -5.          5.         -5.         -2.11052161
 -5.          5.         -5.         -5.          5.         -5.
  1.47810212 -5.          1.084462   -5.         -5.         -5.
  4.08305088 -0.78812737  3.69750536 -0.0661625  -1.30980562  1.03408767
  0.23934831  0.36944918 -2.00690478 -1.55433153  0.33441295]

Label:  1

Merge the two together using experimental.sample_from_datasets:

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

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 [==============================] - 5s 19ms/step - loss: 0.4222 - tp: 243899.0000 - fp: 79000.0000 - tn: 262877.0000 - fn: 40530.0000 - accuracy: 0.8092 - precision: 0.7553 - recall: 0.8575 - auc: 0.9150 - val_loss: 0.2321 - val_tp: 77.0000 - val_fp: 1292.0000 - val_tn: 44195.0000 - val_fn: 5.0000 - val_accuracy: 0.9715 - val_precision: 0.0562 - val_recall: 0.9390 - val_auc: 0.9902
Epoch 2/100
278/278 [==============================] - 5s 17ms/step - loss: 0.2105 - tp: 255777.0000 - fp: 16811.0000 - tn: 267449.0000 - fn: 29307.0000 - accuracy: 0.9190 - precision: 0.9383 - recall: 0.8972 - auc: 0.9710 - val_loss: 0.1270 - val_tp: 77.0000 - val_fp: 1010.0000 - val_tn: 44477.0000 - val_fn: 5.0000 - val_accuracy: 0.9777 - val_precision: 0.0708 - val_recall: 0.9390 - val_auc: 0.9926
Epoch 3/100
278/278 [==============================] - 5s 17ms/step - loss: 0.1640 - tp: 260392.0000 - fp: 11930.0000 - tn: 272600.0000 - fn: 24422.0000 - accuracy: 0.9362 - precision: 0.9562 - recall: 0.9143 - auc: 0.9829 - val_loss: 0.0936 - val_tp: 78.0000 - val_fp: 934.0000 - val_tn: 44553.0000 - val_fn: 4.0000 - val_accuracy: 0.9794 - val_precision: 0.0771 - val_recall: 0.9512 - val_auc: 0.9934
Epoch 4/100
278/278 [==============================] - 5s 17ms/step - loss: 0.1421 - tp: 262909.0000 - fp: 10423.0000 - tn: 274668.0000 - fn: 21344.0000 - accuracy: 0.9442 - precision: 0.9619 - recall: 0.9249 - auc: 0.9876 - val_loss: 0.0797 - val_tp: 78.0000 - val_fp: 888.0000 - val_tn: 44599.0000 - val_fn: 4.0000 - val_accuracy: 0.9804 - val_precision: 0.0807 - val_recall: 0.9512 - val_auc: 0.9939
Epoch 5/100
278/278 [==============================] - 5s 17ms/step - loss: 0.1291 - tp: 264584.0000 - fp: 9645.0000 - tn: 275165.0000 - fn: 19950.0000 - accuracy: 0.9480 - precision: 0.9648 - recall: 0.9299 - auc: 0.9901 - val_loss: 0.0727 - val_tp: 78.0000 - val_fp: 898.0000 - val_tn: 44589.0000 - val_fn: 4.0000 - val_accuracy: 0.9802 - val_precision: 0.0799 - val_recall: 0.9512 - val_auc: 0.9931
Epoch 6/100
278/278 [==============================] - 5s 17ms/step - loss: 0.1205 - tp: 266380.0000 - fp: 9474.0000 - tn: 274854.0000 - fn: 18636.0000 - accuracy: 0.9506 - precision: 0.9657 - recall: 0.9346 - auc: 0.9916 - val_loss: 0.0672 - val_tp: 77.0000 - val_fp: 866.0000 - val_tn: 44621.0000 - val_fn: 5.0000 - val_accuracy: 0.9809 - val_precision: 0.0817 - val_recall: 0.9390 - val_auc: 0.9921
Epoch 7/100
278/278 [==============================] - 5s 16ms/step - loss: 0.1142 - tp: 267383.0000 - fp: 9124.0000 - tn: 275051.0000 - fn: 17786.0000 - accuracy: 0.9527 - precision: 0.9670 - recall: 0.9376 - auc: 0.9926 - val_loss: 0.0621 - val_tp: 76.0000 - val_fp: 836.0000 - val_tn: 44651.0000 - val_fn: 6.0000 - val_accuracy: 0.9815 - val_precision: 0.0833 - val_recall: 0.9268 - val_auc: 0.9906
Epoch 8/100
278/278 [==============================] - 5s 17ms/step - loss: 0.1088 - tp: 267839.0000 - fp: 8965.0000 - tn: 276065.0000 - fn: 16475.0000 - accuracy: 0.9553 - precision: 0.9676 - recall: 0.9421 - auc: 0.9934 - val_loss: 0.0576 - val_tp: 76.0000 - val_fp: 775.0000 - val_tn: 44712.0000 - val_fn: 6.0000 - val_accuracy: 0.9829 - val_precision: 0.0893 - val_recall: 0.9268 - val_auc: 0.9891
Epoch 9/100
278/278 [==============================] - 5s 17ms/step - loss: 0.1025 - tp: 269506.0000 - fp: 8618.0000 - tn: 276360.0000 - fn: 14860.0000 - accuracy: 0.9588 - precision: 0.9690 - recall: 0.9477 - auc: 0.9944 - val_loss: 0.0543 - val_tp: 76.0000 - val_fp: 745.0000 - val_tn: 44742.0000 - val_fn: 6.0000 - val_accuracy: 0.9835 - val_precision: 0.0926 - val_recall: 0.9268 - val_auc: 0.9871
Epoch 10/100
278/278 [==============================] - 5s 17ms/step - loss: 0.0958 - tp: 271313.0000 - fp: 8272.0000 - tn: 276255.0000 - fn: 13504.0000 - accuracy: 0.9618 - precision: 0.9704 - recall: 0.9526 - auc: 0.9952 - val_loss: 0.0494 - val_tp: 76.0000 - val_fp: 693.0000 - val_tn: 44794.0000 - val_fn: 6.0000 - val_accuracy: 0.9847 - val_precision: 0.0988 - val_recall: 0.9268 - val_auc: 0.9769
Epoch 11/100
278/278 [==============================] - 5s 17ms/step - loss: 0.0913 - tp: 272306.0000 - fp: 7857.0000 - tn: 276608.0000 - fn: 12573.0000 - accuracy: 0.9641 - precision: 0.9720 - recall: 0.9559 - auc: 0.9957 - val_loss: 0.0467 - val_tp: 76.0000 - val_fp: 689.0000 - val_tn: 44798.0000 - val_fn: 6.0000 - val_accuracy: 0.9847 - val_precision: 0.0993 - val_recall: 0.9268 - val_auc: 0.9730
Epoch 12/100
278/278 [==============================] - 5s 17ms/step - loss: 0.0876 - tp: 272958.0000 - fp: 7828.0000 - tn: 276796.0000 - fn: 11762.0000 - accuracy: 0.9656 - precision: 0.9721 - recall: 0.9587 - auc: 0.9960 - val_loss: 0.0419 - val_tp: 76.0000 - val_fp: 640.0000 - val_tn: 44847.0000 - val_fn: 6.0000 - val_accuracy: 0.9858 - val_precision: 0.1061 - val_recall: 0.9268 - val_auc: 0.9692
Epoch 13/100
278/278 [==============================] - 5s 17ms/step - loss: 0.0843 - tp: 272890.0000 - fp: 7631.0000 - tn: 277477.0000 - fn: 11346.0000 - accuracy: 0.9667 - precision: 0.9728 - recall: 0.9601 - auc: 0.9962 - val_loss: 0.0409 - val_tp: 76.0000 - val_fp: 644.0000 - val_tn: 44843.0000 - val_fn: 6.0000 - val_accuracy: 0.9857 - val_precision: 0.1056 - val_recall: 0.9268 - val_auc: 0.9698
Epoch 14/100
277/278 [============================>.] - ETA: 0s - loss: 0.0818 - tp: 272389.0000 - fp: 7531.0000 - tn: 276445.0000 - fn: 10931.0000 - accuracy: 0.9675 - precision: 0.9731 - recall: 0.9614 - auc: 0.9963Restoring model weights from the end of the best epoch.
278/278 [==============================] - 5s 17ms/step - loss: 0.0818 - tp: 273350.0000 - fp: 7564.0000 - tn: 277451.0000 - fn: 10979.0000 - accuracy: 0.9674 - precision: 0.9731 - recall: 0.9614 - auc: 0.9963 - val_loss: 0.0378 - val_tp: 76.0000 - val_fp: 597.0000 - val_tn: 44890.0000 - val_fn: 6.0000 - val_accuracy: 0.9868 - val_precision: 0.1129 - val_recall: 0.9268 - val_auc: 0.9704
Epoch 00014: early stopping

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

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

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

Check training history

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

plot_metrics(resampled_history )

png

Re-train

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

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

resampled_model = make_model()
resampled_model.load_weights(initial_weights)

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

resampled_history = resampled_model.fit(
    resampled_ds,
    # These are not real epochs
    steps_per_epoch = 20,
    epochs=10*EPOCHS,
    callbacks = [early_stopping],
    validation_data=(val_ds))
Epoch 1/1000
20/20 [==============================] - 1s 58ms/step - loss: 0.8942 - tp: 14505.0000 - fp: 12489.0000 - tn: 53432.0000 - fn: 6103.0000 - accuracy: 0.7851 - precision: 0.5373 - recall: 0.7039 - auc: 0.8776 - val_loss: 0.8572 - val_tp: 81.0000 - val_fp: 27953.0000 - val_tn: 17534.0000 - val_fn: 1.0000 - val_accuracy: 0.3866 - val_precision: 0.0029 - val_recall: 0.9878 - val_auc: 0.9631
Epoch 2/1000
20/20 [==============================] - 0s 22ms/step - loss: 0.6621 - tp: 16576.0000 - fp: 11019.0000 - tn: 9465.0000 - fn: 3900.0000 - accuracy: 0.6358 - precision: 0.6007 - recall: 0.8095 - auc: 0.7953 - val_loss: 0.7678 - val_tp: 81.0000 - val_fp: 23753.0000 - val_tn: 21734.0000 - val_fn: 1.0000 - val_accuracy: 0.4787 - val_precision: 0.0034 - val_recall: 0.9878 - val_auc: 0.9714
Epoch 3/1000
20/20 [==============================] - 0s 22ms/step - loss: 0.5560 - tp: 17290.0000 - fp: 9697.0000 - tn: 10822.0000 - fn: 3151.0000 - accuracy: 0.6863 - precision: 0.6407 - recall: 0.8458 - auc: 0.8487 - val_loss: 0.6725 - val_tp: 81.0000 - val_fp: 18647.0000 - val_tn: 26840.0000 - val_fn: 1.0000 - val_accuracy: 0.5908 - val_precision: 0.0043 - val_recall: 0.9878 - val_auc: 0.9758
Epoch 4/1000
20/20 [==============================] - 0s 21ms/step - loss: 0.4916 - tp: 17491.0000 - fp: 8342.0000 - tn: 12229.0000 - fn: 2898.0000 - accuracy: 0.7256 - precision: 0.6771 - recall: 0.8579 - auc: 0.8758 - val_loss: 0.5878 - val_tp: 81.0000 - val_fp: 13532.0000 - val_tn: 31955.0000 - val_fn: 1.0000 - val_accuracy: 0.7030 - val_precision: 0.0060 - val_recall: 0.9878 - val_auc: 0.9789
Epoch 5/1000
20/20 [==============================] - 0s 19ms/step - loss: 0.4428 - tp: 17613.0000 - fp: 7070.0000 - tn: 13537.0000 - fn: 2740.0000 - accuracy: 0.7605 - precision: 0.7136 - recall: 0.8654 - auc: 0.8950 - val_loss: 0.5164 - val_tp: 81.0000 - val_fp: 9487.0000 - val_tn: 36000.0000 - val_fn: 1.0000 - val_accuracy: 0.7918 - val_precision: 0.0085 - val_recall: 0.9878 - val_auc: 0.9808
Epoch 6/1000
20/20 [==============================] - 0s 19ms/step - loss: 0.4052 - tp: 17796.0000 - fp: 5876.0000 - tn: 14588.0000 - fn: 2700.0000 - accuracy: 0.7906 - precision: 0.7518 - recall: 0.8683 - auc: 0.9075 - val_loss: 0.4599 - val_tp: 78.0000 - val_fp: 6467.0000 - val_tn: 39020.0000 - val_fn: 4.0000 - val_accuracy: 0.8580 - val_precision: 0.0119 - val_recall: 0.9512 - val_auc: 0.9824
Epoch 7/1000
20/20 [==============================] - 0s 19ms/step - loss: 0.3801 - tp: 17813.0000 - fp: 5023.0000 - tn: 15427.0000 - fn: 2697.0000 - accuracy: 0.8115 - precision: 0.7800 - recall: 0.8685 - auc: 0.9153 - val_loss: 0.4136 - val_tp: 78.0000 - val_fp: 4597.0000 - val_tn: 40890.0000 - val_fn: 4.0000 - val_accuracy: 0.8990 - val_precision: 0.0167 - val_recall: 0.9512 - val_auc: 0.9842
Epoch 8/1000
20/20 [==============================] - 0s 19ms/step - loss: 0.3499 - tp: 17926.0000 - fp: 4218.0000 - tn: 16337.0000 - fn: 2479.0000 - accuracy: 0.8365 - precision: 0.8095 - recall: 0.8785 - auc: 0.9284 - val_loss: 0.3738 - val_tp: 77.0000 - val_fp: 3356.0000 - val_tn: 42131.0000 - val_fn: 5.0000 - val_accuracy: 0.9262 - val_precision: 0.0224 - val_recall: 0.9390 - val_auc: 0.9855
Epoch 9/1000
20/20 [==============================] - 0s 22ms/step - loss: 0.3287 - tp: 18052.0000 - fp: 3639.0000 - tn: 16771.0000 - fn: 2498.0000 - accuracy: 0.8502 - precision: 0.8322 - recall: 0.8784 - auc: 0.9345 - val_loss: 0.3407 - val_tp: 77.0000 - val_fp: 2569.0000 - val_tn: 42918.0000 - val_fn: 5.0000 - val_accuracy: 0.9435 - val_precision: 0.0291 - val_recall: 0.9390 - val_auc: 0.9863
Epoch 10/1000
20/20 [==============================] - 0s 21ms/step - loss: 0.3155 - tp: 17861.0000 - fp: 3067.0000 - tn: 17520.0000 - fn: 2512.0000 - accuracy: 0.8638 - precision: 0.8534 - recall: 0.8767 - auc: 0.9396 - val_loss: 0.3123 - val_tp: 77.0000 - val_fp: 2061.0000 - val_tn: 43426.0000 - val_fn: 5.0000 - val_accuracy: 0.9547 - val_precision: 0.0360 - val_recall: 0.9390 - val_auc: 0.9871
Epoch 11/1000
20/20 [==============================] - 0s 21ms/step - loss: 0.2950 - tp: 18155.0000 - fp: 2739.0000 - tn: 17701.0000 - fn: 2365.0000 - accuracy: 0.8754 - precision: 0.8689 - recall: 0.8847 - auc: 0.9463 - val_loss: 0.2887 - val_tp: 77.0000 - val_fp: 1757.0000 - val_tn: 43730.0000 - val_fn: 5.0000 - val_accuracy: 0.9613 - val_precision: 0.0420 - val_recall: 0.9390 - val_auc: 0.9880
Epoch 12/1000
20/20 [==============================] - 0s 22ms/step - loss: 0.2812 - tp: 18261.0000 - fp: 2368.0000 - tn: 18036.0000 - fn: 2295.0000 - accuracy: 0.8862 - precision: 0.8852 - recall: 0.8884 - auc: 0.9507 - val_loss: 0.2680 - val_tp: 77.0000 - val_fp: 1559.0000 - val_tn: 43928.0000 - val_fn: 5.0000 - val_accuracy: 0.9657 - val_precision: 0.0471 - val_recall: 0.9390 - val_auc: 0.9891
Epoch 13/1000
20/20 [==============================] - 0s 21ms/step - loss: 0.2663 - tp: 18160.0000 - fp: 2025.0000 - tn: 18578.0000 - fn: 2197.0000 - accuracy: 0.8969 - precision: 0.8997 - recall: 0.8921 - auc: 0.9554 - val_loss: 0.2494 - val_tp: 77.0000 - val_fp: 1415.0000 - val_tn: 44072.0000 - val_fn: 5.0000 - val_accuracy: 0.9688 - val_precision: 0.0516 - val_recall: 0.9390 - val_auc: 0.9900
Epoch 14/1000
20/20 [==============================] - 0s 19ms/step - loss: 0.2556 - tp: 18208.0000 - fp: 1871.0000 - tn: 18639.0000 - fn: 2242.0000 - accuracy: 0.8996 - precision: 0.9068 - recall: 0.8904 - auc: 0.9582 - val_loss: 0.2334 - val_tp: 77.0000 - val_fp: 1320.0000 - val_tn: 44167.0000 - val_fn: 5.0000 - val_accuracy: 0.9709 - val_precision: 0.0551 - val_recall: 0.9390 - val_auc: 0.9904
Epoch 15/1000
20/20 [==============================] - 0s 19ms/step - loss: 0.2461 - tp: 18402.0000 - fp: 1683.0000 - tn: 18641.0000 - fn: 2234.0000 - accuracy: 0.9044 - precision: 0.9162 - recall: 0.8917 - auc: 0.9609 - val_loss: 0.2188 - val_tp: 77.0000 - val_fp: 1238.0000 - val_tn: 44249.0000 - val_fn: 5.0000 - val_accuracy: 0.9727 - val_precision: 0.0586 - val_recall: 0.9390 - val_auc: 0.9909
Epoch 16/1000
20/20 [==============================] - 0s 19ms/step - loss: 0.2374 - tp: 18251.0000 - fp: 1564.0000 - tn: 18948.0000 - fn: 2197.0000 - accuracy: 0.9082 - precision: 0.9211 - recall: 0.8926 - auc: 0.9634 - val_loss: 0.2057 - val_tp: 77.0000 - val_fp: 1179.0000 - val_tn: 44308.0000 - val_fn: 5.0000 - val_accuracy: 0.9740 - val_precision: 0.0613 - val_recall: 0.9390 - val_auc: 0.9914
Epoch 17/1000
20/20 [==============================] - 0s 19ms/step - loss: 0.2338 - tp: 18378.0000 - fp: 1554.0000 - tn: 18887.0000 - fn: 2141.0000 - accuracy: 0.9098 - precision: 0.9220 - recall: 0.8957 - auc: 0.9643 - val_loss: 0.1944 - val_tp: 77.0000 - val_fp: 1131.0000 - val_tn: 44356.0000 - val_fn: 5.0000 - val_accuracy: 0.9751 - val_precision: 0.0637 - val_recall: 0.9390 - val_auc: 0.9918
Epoch 18/1000
20/20 [==============================] - 0s 22ms/step - loss: 0.2275 - tp: 18344.0000 - fp: 1425.0000 - tn: 19022.0000 - fn: 2169.0000 - accuracy: 0.9123 - precision: 0.9279 - recall: 0.8943 - auc: 0.9663 - val_loss: 0.1837 - val_tp: 77.0000 - val_fp: 1088.0000 - val_tn: 44399.0000 - val_fn: 5.0000 - val_accuracy: 0.9760 - val_precision: 0.0661 - val_recall: 0.9390 - val_auc: 0.9920
Epoch 19/1000
20/20 [==============================] - 0s 22ms/step - loss: 0.2212 - tp: 18404.0000 - fp: 1266.0000 - tn: 19086.0000 - fn: 2204.0000 - accuracy: 0.9153 - precision: 0.9356 - recall: 0.8931 - auc: 0.9675 - val_loss: 0.1755 - val_tp: 77.0000 - val_fp: 1068.0000 - val_tn: 44419.0000 - val_fn: 5.0000 - val_accuracy: 0.9765 - val_precision: 0.0672 - val_recall: 0.9390 - val_auc: 0.9924
Epoch 20/1000
20/20 [==============================] - 0s 22ms/step - loss: 0.2139 - tp: 18494.0000 - fp: 1252.0000 - tn: 19119.0000 - fn: 2095.0000 - accuracy: 0.9183 - precision: 0.9366 - recall: 0.8982 - auc: 0.9698 - val_loss: 0.1680 - val_tp: 77.0000 - val_fp: 1062.0000 - val_tn: 44425.0000 - val_fn: 5.0000 - val_accuracy: 0.9766 - val_precision: 0.0676 - val_recall: 0.9390 - val_auc: 0.9928
Epoch 21/1000
20/20 [==============================] - 0s 21ms/step - loss: 0.2069 - tp: 18481.0000 - fp: 1119.0000 - tn: 19264.0000 - fn: 2096.0000 - accuracy: 0.9215 - precision: 0.9429 - recall: 0.8981 - auc: 0.9718 - val_loss: 0.1611 - val_tp: 77.0000 - val_fp: 1056.0000 - val_tn: 44431.0000 - val_fn: 5.0000 - val_accuracy: 0.9767 - val_precision: 0.0680 - val_recall: 0.9390 - val_auc: 0.9927
Epoch 22/1000
20/20 [==============================] - 0s 21ms/step - loss: 0.2081 - tp: 18328.0000 - fp: 1195.0000 - tn: 19317.0000 - fn: 2120.0000 - accuracy: 0.9191 - precision: 0.9388 - recall: 0.8963 - auc: 0.9716 - val_loss: 0.1540 - val_tp: 77.0000 - val_fp: 1021.0000 - val_tn: 44466.0000 - val_fn: 5.0000 - val_accuracy: 0.9775 - val_precision: 0.0701 - val_recall: 0.9390 - val_auc: 0.9926
Epoch 23/1000
20/20 [==============================] - 0s 19ms/step - loss: 0.2016 - tp: 18344.0000 - fp: 1107.0000 - tn: 19437.0000 - fn: 2072.0000 - accuracy: 0.9224 - precision: 0.9431 - recall: 0.8985 - auc: 0.9730 - val_loss: 0.1481 - val_tp: 77.0000 - val_fp: 996.0000 - val_tn: 44491.0000 - val_fn: 5.0000 - val_accuracy: 0.9780 - val_precision: 0.0718 - val_recall: 0.9390 - val_auc: 0.9926
Epoch 24/1000
20/20 [==============================] - 0s 19ms/step - loss: 0.1978 - tp: 18398.0000 - fp: 989.0000 - tn: 19486.0000 - fn: 2087.0000 - accuracy: 0.9249 - precision: 0.9490 - recall: 0.8981 - auc: 0.9743 - val_loss: 0.1433 - val_tp: 77.0000 - val_fp: 996.0000 - val_tn: 44491.0000 - val_fn: 5.0000 - val_accuracy: 0.9780 - val_precision: 0.0718 - val_recall: 0.9390 - val_auc: 0.9927
Epoch 25/1000
20/20 [==============================] - 0s 19ms/step - loss: 0.1939 - tp: 18298.0000 - fp: 990.0000 - tn: 19607.0000 - fn: 2065.0000 - accuracy: 0.9254 - precision: 0.9487 - recall: 0.8986 - auc: 0.9756 - val_loss: 0.1387 - val_tp: 77.0000 - val_fp: 991.0000 - val_tn: 44496.0000 - val_fn: 5.0000 - val_accuracy: 0.9781 - val_precision: 0.0721 - val_recall: 0.9390 - val_auc: 0.9926
Epoch 26/1000
20/20 [==============================] - 0s 19ms/step - loss: 0.1903 - tp: 18470.0000 - fp: 982.0000 - tn: 19478.0000 - fn: 2030.0000 - accuracy: 0.9265 - precision: 0.9495 - recall: 0.9010 - auc: 0.9762 - val_loss: 0.1337 - val_tp: 77.0000 - val_fp: 976.0000 - val_tn: 44511.0000 - val_fn: 5.0000 - val_accuracy: 0.9785 - val_precision: 0.0731 - val_recall: 0.9390 - val_auc: 0.9926
Epoch 27/1000
20/20 [==============================] - 0s 23ms/step - loss: 0.1866 - tp: 18572.0000 - fp: 896.0000 - tn: 19478.0000 - fn: 2014.0000 - accuracy: 0.9290 - precision: 0.9540 - recall: 0.9022 - auc: 0.9771 - val_loss: 0.1303 - val_tp: 77.0000 - val_fp: 990.0000 - val_tn: 44497.0000 - val_fn: 5.0000 - val_accuracy: 0.9782 - val_precision: 0.0722 - val_recall: 0.9390 - val_auc: 0.9927
Epoch 28/1000
20/20 [==============================] - 0s 21ms/step - loss: 0.1838 - tp: 18498.0000 - fp: 902.0000 - tn: 19578.0000 - fn: 1982.0000 - accuracy: 0.9296 - precision: 0.9535 - recall: 0.9032 - auc: 0.9781 - val_loss: 0.1269 - val_tp: 77.0000 - val_fp: 997.0000 - val_tn: 44490.0000 - val_fn: 5.0000 - val_accuracy: 0.9780 - val_precision: 0.0717 - val_recall: 0.9390 - val_auc: 0.9926
Epoch 29/1000
20/20 [==============================] - 0s 21ms/step - loss: 0.1835 - tp: 18542.0000 - fp: 990.0000 - tn: 19449.0000 - fn: 1979.0000 - accuracy: 0.9275 - precision: 0.9493 - recall: 0.9036 - auc: 0.9782 - val_loss: 0.1233 - val_tp: 77.0000 - val_fp: 983.0000 - val_tn: 44504.0000 - val_fn: 5.0000 - val_accuracy: 0.9783 - val_precision: 0.0726 - val_recall: 0.9390 - val_auc: 0.9927
Epoch 30/1000
20/20 [==============================] - 0s 21ms/step - loss: 0.1802 - tp: 18453.0000 - fp: 933.0000 - tn: 19616.0000 - fn: 1958.0000 - accuracy: 0.9294 - precision: 0.9519 - recall: 0.9041 - auc: 0.9790 - val_loss: 0.1202 - val_tp: 77.0000 - val_fp: 977.0000 - val_tn: 44510.0000 - val_fn: 5.0000 - val_accuracy: 0.9785 - val_precision: 0.0731 - val_recall: 0.9390 - val_auc: 0.9928
Epoch 31/1000
20/20 [==============================] - 0s 21ms/step - loss: 0.1772 - tp: 18554.0000 - fp: 918.0000 - tn: 19549.0000 - fn: 1939.0000 - accuracy: 0.9302 - precision: 0.9529 - recall: 0.9054 - auc: 0.9797 - val_loss: 0.1164 - val_tp: 78.0000 - val_fp: 951.0000 - val_tn: 44536.0000 - val_fn: 4.0000 - val_accuracy: 0.9790 - val_precision: 0.0758 - val_recall: 0.9512 - val_auc: 0.9929
Epoch 32/1000
20/20 [==============================] - 0s 19ms/step - loss: 0.1726 - tp: 18628.0000 - fp: 865.0000 - tn: 19586.0000 - fn: 1881.0000 - accuracy: 0.9330 - precision: 0.9556 - recall: 0.9083 - auc: 0.9807 - val_loss: 0.1142 - val_tp: 78.0000 - val_fp: 965.0000 - val_tn: 44522.0000 - val_fn: 4.0000 - val_accuracy: 0.9787 - val_precision: 0.0748 - val_recall: 0.9512 - val_auc: 0.9930
Epoch 33/1000
20/20 [==============================] - 0s 19ms/step - loss: 0.1707 - tp: 18577.0000 - fp: 863.0000 - tn: 19632.0000 - fn: 1888.0000 - accuracy: 0.9328 - precision: 0.9556 - recall: 0.9077 - auc: 0.9812 - val_loss: 0.1117 - val_tp: 78.0000 - val_fp: 966.0000 - val_tn: 44521.0000 - val_fn: 4.0000 - val_accuracy: 0.9787 - val_precision: 0.0747 - val_recall: 0.9512 - val_auc: 0.9932
Epoch 34/1000
20/20 [==============================] - 0s 20ms/step - loss: 0.1697 - tp: 18608.0000 - fp: 869.0000 - tn: 19631.0000 - fn: 1852.0000 - accuracy: 0.9336 - precision: 0.9554 - recall: 0.9095 - auc: 0.9816 - val_loss: 0.1093 - val_tp: 77.0000 - val_fp: 955.0000 - val_tn: 44532.0000 - val_fn: 5.0000 - val_accuracy: 0.9789 - val_precision: 0.0746 - val_recall: 0.9390 - val_auc: 0.9932
Epoch 35/1000
20/20 [==============================] - 0s 19ms/step - loss: 0.1618 - tp: 18912.0000 - fp: 837.0000 - tn: 19443.0000 - fn: 1768.0000 - accuracy: 0.9364 - precision: 0.9576 - recall: 0.9145 - auc: 0.9832 - val_loss: 0.1077 - val_tp: 77.0000 - val_fp: 973.0000 - val_tn: 44514.0000 - val_fn: 5.0000 - val_accuracy: 0.9785 - val_precision: 0.0733 - val_recall: 0.9390 - val_auc: 0.9932
Epoch 36/1000
20/20 [==============================] - 0s 23ms/step - loss: 0.1653 - tp: 18636.0000 - fp: 847.0000 - tn: 19732.0000 - fn: 1745.0000 - accuracy: 0.9367 - precision: 0.9565 - recall: 0.9144 - auc: 0.9823 - val_loss: 0.1058 - val_tp: 77.0000 - val_fp: 980.0000 - val_tn: 44507.0000 - val_fn: 5.0000 - val_accuracy: 0.9784 - val_precision: 0.0728 - val_recall: 0.9390 - val_auc: 0.9933
Epoch 37/1000
20/20 [==============================] - 0s 21ms/step - loss: 0.1625 - tp: 18695.0000 - fp: 808.0000 - tn: 19735.0000 - fn: 1722.0000 - accuracy: 0.9382 - precision: 0.9586 - recall: 0.9157 - auc: 0.9830 - val_loss: 0.1033 - val_tp: 77.0000 - val_fp: 962.0000 - val_tn: 44525.0000 - val_fn: 5.0000 - val_accuracy: 0.9788 - val_precision: 0.0741 - val_recall: 0.9390 - val_auc: 0.9934
Epoch 38/1000
20/20 [==============================] - 0s 22ms/step - loss: 0.1588 - tp: 18720.0000 - fp: 819.0000 - tn: 19707.0000 - fn: 1714.0000 - accuracy: 0.9382 - precision: 0.9581 - recall: 0.9161 - auc: 0.9840 - val_loss: 0.1019 - val_tp: 78.0000 - val_fp: 962.0000 - val_tn: 44525.0000 - val_fn: 4.0000 - val_accuracy: 0.9788 - val_precision: 0.0750 - val_recall: 0.9512 - val_auc: 0.9934
Epoch 39/1000
20/20 [==============================] - 0s 21ms/step - loss: 0.1569 - tp: 18880.0000 - fp: 838.0000 - tn: 19537.0000 - fn: 1705.0000 - accuracy: 0.9379 - precision: 0.9575 - recall: 0.9172 - auc: 0.9841 - val_loss: 0.1005 - val_tp: 78.0000 - val_fp: 972.0000 - val_tn: 44515.0000 - val_fn: 4.0000 - val_accuracy: 0.9786 - val_precision: 0.0743 - val_recall: 0.9512 - val_auc: 0.9936
Epoch 40/1000
20/20 [==============================] - 0s 20ms/step - loss: 0.1555 - tp: 18706.0000 - fp: 799.0000 - tn: 19746.0000 - fn: 1709.0000 - accuracy: 0.9388 - precision: 0.9590 - recall: 0.9163 - auc: 0.9847 - val_loss: 0.0987 - val_tp: 78.0000 - val_fp: 979.0000 - val_tn: 44508.0000 - val_fn: 4.0000 - val_accuracy: 0.9784 - val_precision: 0.0738 - val_recall: 0.9512 - val_auc: 0.9936
Epoch 41/1000
20/20 [==============================] - 0s 19ms/step - loss: 0.1528 - tp: 18883.0000 - fp: 784.0000 - tn: 19664.0000 - fn: 1629.0000 - accuracy: 0.9411 - precision: 0.9601 - recall: 0.9206 - auc: 0.9853 - val_loss: 0.0976 - val_tp: 78.0000 - val_fp: 991.0000 - val_tn: 44496.0000 - val_fn: 4.0000 - val_accuracy: 0.9782 - val_precision: 0.0730 - val_recall: 0.9512 - val_auc: 0.9937
Epoch 42/1000
20/20 [==============================] - 0s 19ms/step - loss: 0.1557 - tp: 18727.0000 - fp: 858.0000 - tn: 19743.0000 - fn: 1632.0000 - accuracy: 0.9392 - precision: 0.9562 - recall: 0.9198 - auc: 0.9845 - val_loss: 0.0954 - val_tp: 78.0000 - val_fp: 963.0000 - val_tn: 44524.0000 - val_fn: 4.0000 - val_accuracy: 0.9788 - val_precision: 0.0749 - val_recall: 0.9512 - val_auc: 0.9937
Epoch 43/1000
20/20 [==============================] - 0s 19ms/step - loss: 0.1505 - tp: 19082.0000 - fp: 774.0000 - tn: 19469.0000 - fn: 1635.0000 - accuracy: 0.9412 - precision: 0.9610 - recall: 0.9211 - auc: 0.9857 - val_loss: 0.0942 - val_tp: 78.0000 - val_fp: 962.0000 - val_tn: 44525.0000 - val_fn: 4.0000 - val_accuracy: 0.9788 - val_precision: 0.0750 - val_recall: 0.9512 - val_auc: 0.9937
Epoch 44/1000
20/20 [==============================] - 0s 19ms/step - loss: 0.1487 - tp: 18788.0000 - fp: 812.0000 - tn: 19767.0000 - fn: 1593.0000 - accuracy: 0.9413 - precision: 0.9586 - recall: 0.9218 - auc: 0.9859 - val_loss: 0.0927 - val_tp: 78.0000 - val_fp: 946.0000 - val_tn: 44541.0000 - val_fn: 4.0000 - val_accuracy: 0.9792 - val_precision: 0.0762 - val_recall: 0.9512 - val_auc: 0.9939
Epoch 45/1000
20/20 [==============================] - 0s 23ms/step - loss: 0.1493 - tp: 18902.0000 - fp: 816.0000 - tn: 19686.0000 - fn: 1556.0000 - accuracy: 0.9421 - precision: 0.9586 - recall: 0.9239 - auc: 0.9861 - val_loss: 0.0908 - val_tp: 78.0000 - val_fp: 921.0000 - val_tn: 44566.0000 - val_fn: 4.0000 - val_accuracy: 0.9797 - val_precision: 0.0781 - val_recall: 0.9512 - val_auc: 0.9939
Epoch 46/1000
20/20 [==============================] - 0s 22ms/step - loss: 0.1478 - tp: 18692.0000 - fp: 768.0000 - tn: 19872.0000 - fn: 1628.0000 - accuracy: 0.9415 - precision: 0.9605 - recall: 0.9199 - auc: 0.9863 - val_loss: 0.0893 - val_tp: 78.0000 - val_fp: 900.0000 - val_tn: 44587.0000 - val_fn: 4.0000 - val_accuracy: 0.9802 - val_precision: 0.0798 - val_recall: 0.9512 - val_auc: 0.9940
Epoch 47/1000
20/20 [==============================] - 0s 21ms/step - loss: 0.1446 - tp: 18772.0000 - fp: 710.0000 - tn: 19918.0000 - fn: 1560.0000 - accuracy: 0.9446 - precision: 0.9636 - recall: 0.9233 - auc: 0.9872 - val_loss: 0.0882 - val_tp: 78.0000 - val_fp: 906.0000 - val_tn: 44581.0000 - val_fn: 4.0000 - val_accuracy: 0.9800 - val_precision: 0.0793 - val_recall: 0.9512 - val_auc: 0.9941
Epoch 48/1000
20/20 [==============================] - 0s 22ms/step - loss: 0.1469 - tp: 18903.0000 - fp: 761.0000 - tn: 19683.0000 - fn: 1613.0000 - accuracy: 0.9420 - precision: 0.9613 - recall: 0.9214 - auc: 0.9869 - val_loss: 0.0878 - val_tp: 78.0000 - val_fp: 912.0000 - val_tn: 44575.0000 - val_fn: 4.0000 - val_accuracy: 0.9799 - val_precision: 0.0788 - val_recall: 0.9512 - val_auc: 0.9941
Epoch 49/1000
20/20 [==============================] - 0s 20ms/step - loss: 0.1424 - tp: 18880.0000 - fp: 779.0000 - tn: 19729.0000 - fn: 1572.0000 - accuracy: 0.9426 - precision: 0.9604 - recall: 0.9231 - auc: 0.9875 - val_loss: 0.0871 - val_tp: 78.0000 - val_fp: 913.0000 - val_tn: 44574.0000 - val_fn: 4.0000 - val_accuracy: 0.9799 - val_precision: 0.0787 - val_recall: 0.9512 - val_auc: 0.9942
Epoch 50/1000
20/20 [==============================] - 0s 19ms/step - loss: 0.1402 - tp: 19031.0000 - fp: 732.0000 - tn: 19701.0000 - fn: 1496.0000 - accuracy: 0.9456 - precision: 0.9630 - recall: 0.9271 - auc: 0.9878 - val_loss: 0.0862 - val_tp: 78.0000 - val_fp: 914.0000 - val_tn: 44573.0000 - val_fn: 4.0000 - val_accuracy: 0.9799 - val_precision: 0.0786 - val_recall: 0.9512 - val_auc: 0.9943
Epoch 51/1000
20/20 [==============================] - 0s 19ms/step - loss: 0.1396 - tp: 18937.0000 - fp: 773.0000 - tn: 19706.0000 - fn: 1544.0000 - accuracy: 0.9434 - precision: 0.9608 - recall: 0.9246 - auc: 0.9881 - val_loss: 0.0850 - val_tp: 78.0000 - val_fp: 908.0000 - val_tn: 44579.0000 - val_fn: 4.0000 - val_accuracy: 0.9800 - val_precision: 0.0791 - val_recall: 0.9512 - val_auc: 0.9944
Epoch 52/1000
20/20 [==============================] - 0s 19ms/step - loss: 0.1406 - tp: 18936.0000 - fp: 779.0000 - tn: 19690.0000 - fn: 1555.0000 - accuracy: 0.9430 - precision: 0.9605 - recall: 0.9241 - auc: 0.9879 - val_loss: 0.0835 - val_tp: 78.0000 - val_fp: 888.0000 - val_tn: 44599.0000 - val_fn: 4.0000 - val_accuracy: 0.9804 - val_precision: 0.0807 - val_recall: 0.9512 - val_auc: 0.9943
Epoch 53/1000
20/20 [==============================] - 0s 19ms/step - loss: 0.1384 - tp: 18943.0000 - fp: 733.0000 - tn: 19769.0000 - fn: 1515.0000 - accuracy: 0.9451 - precision: 0.9627 - recall: 0.9259 - auc: 0.9882 - val_loss: 0.0825 - val_tp: 78.0000 - val_fp: 879.0000 - val_tn: 44608.0000 - val_fn: 4.0000 - val_accuracy: 0.9806 - val_precision: 0.0815 - val_recall: 0.9512 - val_auc: 0.9943
Epoch 54/1000
20/20 [==============================] - 0s 23ms/step - loss: 0.1358 - tp: 18901.0000 - fp: 717.0000 - tn: 19844.0000 - fn: 1498.0000 - accuracy: 0.9459 - precision: 0.9635 - recall: 0.9266 - auc: 0.9888 - val_loss: 0.0816 - val_tp: 78.0000 - val_fp: 879.0000 - val_tn: 44608.0000 - val_fn: 4.0000 - val_accuracy: 0.9806 - val_precision: 0.0815 - val_recall: 0.9512 - val_auc: 0.9943
Epoch 55/1000
20/20 [==============================] - 0s 22ms/step - loss: 0.1388 - tp: 18983.0000 - fp: 717.0000 - tn: 19766.0000 - fn: 1494.0000 - accuracy: 0.9460 - precision: 0.9636 - recall: 0.9270 - auc: 0.9884 - val_loss: 0.0813 - val_tp: 78.0000 - val_fp: 902.0000 - val_tn: 44585.0000 - val_fn: 4.0000 - val_accuracy: 0.9801 - val_precision: 0.0796 - val_recall: 0.9512 - val_auc: 0.9942
Epoch 56/1000
20/20 [==============================] - 0s 21ms/step - loss: 0.1362 - tp: 18931.0000 - fp: 739.0000 - tn: 19795.0000 - fn: 1495.0000 - accuracy: 0.9455 - precision: 0.9624 - recall: 0.9268 - auc: 0.9887 - val_loss: 0.0807 - val_tp: 78.0000 - val_fp: 901.0000 - val_tn: 44586.0000 - val_fn: 4.0000 - val_accuracy: 0.9801 - val_precision: 0.0797 - val_recall: 0.9512 - val_auc: 0.9942
Epoch 57/1000
20/20 [==============================] - 0s 22ms/step - loss: 0.1343 - tp: 18932.0000 - fp: 707.0000 - tn: 19830.0000 - fn: 1491.0000 - accuracy: 0.9463 - precision: 0.9640 - recall: 0.9270 - auc: 0.9890 - val_loss: 0.0791 - val_tp: 78.0000 - val_fp: 878.0000 - val_tn: 44609.0000 - val_fn: 4.0000 - val_accuracy: 0.9806 - val_precision: 0.0816 - val_recall: 0.9512 - val_auc: 0.9942
Epoch 58/1000
20/20 [==============================] - 0s 19ms/step - loss: 0.1322 - tp: 19057.0000 - fp: 686.0000 - tn: 19721.0000 - fn: 1496.0000 - accuracy: 0.9467 - precision: 0.9653 - recall: 0.9272 - auc: 0.9893 - val_loss: 0.0792 - val_tp: 78.0000 - val_fp: 900.0000 - val_tn: 44587.0000 - val_fn: 4.0000 - val_accuracy: 0.9802 - val_precision: 0.0798 - val_recall: 0.9512 - val_auc: 0.9941
Epoch 59/1000
20/20 [==============================] - 0s 19ms/step - loss: 0.1316 - tp: 19003.0000 - fp: 701.0000 - tn: 19796.0000 - fn: 1460.0000 - accuracy: 0.9472 - precision: 0.9644 - recall: 0.9287 - auc: 0.9898 - val_loss: 0.0780 - val_tp: 78.0000 - val_fp: 882.0000 - val_tn: 44605.0000 - val_fn: 4.0000 - val_accuracy: 0.9806 - val_precision: 0.0812 - val_recall: 0.9512 - val_auc: 0.9940
Epoch 60/1000
20/20 [==============================] - 0s 22ms/step - loss: 0.1304 - tp: 18999.0000 - fp: 680.0000 - tn: 19802.0000 - fn: 1479.0000 - accuracy: 0.9473 - precision: 0.9654 - recall: 0.9278 - auc: 0.9897 - val_loss: 0.0776 - val_tp: 78.0000 - val_fp: 884.0000 - val_tn: 44603.0000 - val_fn: 4.0000 - val_accuracy: 0.9805 - val_precision: 0.0811 - val_recall: 0.9512 - val_auc: 0.9939
Epoch 61/1000
18/20 [==========================>...] - ETA: 0s - loss: 0.1330 - tp: 17361.0000 - fp: 608.0000 - tn: 17556.0000 - fn: 1339.0000 - accuracy: 0.9472 - precision: 0.9662 - recall: 0.9284 - auc: 0.9897Restoring model weights from the end of the best epoch.
20/20 [==============================] - 0s 19ms/step - loss: 0.1333 - tp: 19225.0000 - fp: 683.0000 - tn: 19563.0000 - fn: 1489.0000 - accuracy: 0.9470 - precision: 0.9657 - recall: 0.9281 - auc: 0.9896 - val_loss: 0.0776 - val_tp: 78.0000 - val_fp: 893.0000 - val_tn: 44594.0000 - val_fn: 4.0000 - val_accuracy: 0.9803 - val_precision: 0.0803 - val_recall: 0.9512 - val_auc: 0.9938
Epoch 00061: early stopping

Re-check training history

plot_metrics(resampled_history)

png

Evaluate metrics

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

plot_cm(test_labels, test_predictions_resampled)
loss :  0.08373639732599258
tp :  87.0
fp :  1122.0
tn :  55745.0
fn :  8.0
accuracy :  0.9801622033119202
precision :  0.07196030020713806
recall :  0.9157894849777222
auc :  0.9741244316101074

Legitimate Transactions Detected (True Negatives):  55745
Legitimate Transactions Incorrectly Detected (False Positives):  1122
Fraudulent Transactions Missed (False Negatives):  8
Fraudulent Transactions Detected (True Positives):  87
Total Fraudulent Transactions:  95

png

Plot the ROC

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

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

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

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.