![]() |
![]() |
![]() |
![]() |
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-12-14 02:48:09.410091: 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-12-14 02:48:09.410192: 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-12-14 02:48:09.410202: 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.
- Yes. You've normalized the input and these are mostly concentrated in the
- 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")
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:
- True vs. False and Positive vs. Negative
- Accuracy
- Precision and Recall
- ROC-AUC
- Relationship between Precision-Recall and ROC Curves
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 427ms/step array([[0.12086625], [0.37875205], [0.14548904], [0.1107088 ], [0.11098117], [0.13489977], [0.09975273], [0.17781098], [0.09235804], [0.176774 ]], 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: 0.1652
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 50ms/step array([[0.00110731], [0.00280909], [0.00413586], [0.00342205], [0.00166295], [0.00368322], [0.00099343], [0.00190527], [0.00081247], [0.00266587]], 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.0101
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)
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 11ms/step - loss: 0.0108 - tp: 156.0000 - fp: 138.0000 - tn: 227312.0000 - fn: 239.0000 - accuracy: 0.9983 - precision: 0.5306 - recall: 0.3949 - auc: 0.8656 - prc: 0.3547 - val_loss: 0.0055 - val_tp: 44.0000 - val_fp: 8.0000 - val_tn: 45480.0000 - val_fn: 37.0000 - val_accuracy: 0.9990 - val_precision: 0.8462 - val_recall: 0.5432 - val_auc: 0.9051 - val_prc: 0.6984 Epoch 2/100 90/90 [==============================] - 1s 6ms/step - loss: 0.0067 - tp: 153.0000 - fp: 34.0000 - tn: 181928.0000 - fn: 161.0000 - accuracy: 0.9989 - precision: 0.8182 - recall: 0.4873 - auc: 0.9011 - prc: 0.5735 - val_loss: 0.0048 - val_tp: 49.0000 - val_fp: 10.0000 - val_tn: 45478.0000 - val_fn: 32.0000 - val_accuracy: 0.9991 - val_precision: 0.8305 - val_recall: 0.6049 - val_auc: 0.9131 - val_prc: 0.7129 Epoch 3/100 90/90 [==============================] - 1s 6ms/step - loss: 0.0058 - tp: 163.0000 - fp: 28.0000 - tn: 181934.0000 - fn: 151.0000 - accuracy: 0.9990 - precision: 0.8534 - recall: 0.5191 - auc: 0.9057 - prc: 0.6172 - val_loss: 0.0045 - val_tp: 50.0000 - val_fp: 10.0000 - val_tn: 45478.0000 - val_fn: 31.0000 - val_accuracy: 0.9991 - val_precision: 0.8333 - val_recall: 0.6173 - val_auc: 0.9132 - val_prc: 0.7091 Epoch 4/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0053 - tp: 166.0000 - fp: 31.0000 - tn: 181931.0000 - fn: 148.0000 - accuracy: 0.9990 - precision: 0.8426 - recall: 0.5287 - auc: 0.9187 - prc: 0.6479 - val_loss: 0.0043 - val_tp: 54.0000 - val_fp: 10.0000 - val_tn: 45478.0000 - val_fn: 27.0000 - val_accuracy: 0.9992 - val_precision: 0.8438 - val_recall: 0.6667 - val_auc: 0.9195 - val_prc: 0.7355 Epoch 5/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0048 - tp: 177.0000 - fp: 28.0000 - tn: 181934.0000 - fn: 137.0000 - accuracy: 0.9991 - precision: 0.8634 - recall: 0.5637 - auc: 0.9230 - prc: 0.7090 - val_loss: 0.0042 - val_tp: 56.0000 - val_fp: 11.0000 - val_tn: 45477.0000 - val_fn: 25.0000 - val_accuracy: 0.9992 - val_precision: 0.8358 - val_recall: 0.6914 - val_auc: 0.9256 - val_prc: 0.7012 Epoch 6/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0049 - tp: 186.0000 - fp: 33.0000 - tn: 181929.0000 - fn: 128.0000 - accuracy: 0.9991 - precision: 0.8493 - recall: 0.5924 - auc: 0.9187 - prc: 0.6840 - val_loss: 0.0040 - val_tp: 55.0000 - val_fp: 11.0000 - val_tn: 45477.0000 - val_fn: 26.0000 - val_accuracy: 0.9992 - val_precision: 0.8333 - val_recall: 0.6790 - val_auc: 0.9257 - val_prc: 0.7520 Epoch 7/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0043 - tp: 186.0000 - fp: 26.0000 - tn: 181936.0000 - fn: 128.0000 - accuracy: 0.9992 - precision: 0.8774 - recall: 0.5924 - auc: 0.9222 - prc: 0.7255 - val_loss: 0.0040 - val_tp: 58.0000 - val_fp: 11.0000 - val_tn: 45477.0000 - val_fn: 23.0000 - val_accuracy: 0.9993 - val_precision: 0.8406 - val_recall: 0.7160 - val_auc: 0.9257 - val_prc: 0.7251 Epoch 8/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0044 - tp: 195.0000 - fp: 25.0000 - tn: 181937.0000 - fn: 119.0000 - accuracy: 0.9992 - precision: 0.8864 - recall: 0.6210 - auc: 0.9143 - prc: 0.7114 - val_loss: 0.0038 - val_tp: 56.0000 - val_fp: 11.0000 - val_tn: 45477.0000 - val_fn: 25.0000 - val_accuracy: 0.9992 - val_precision: 0.8358 - val_recall: 0.6914 - val_auc: 0.9195 - val_prc: 0.7571 Epoch 9/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0043 - tp: 198.0000 - fp: 31.0000 - tn: 181931.0000 - fn: 116.0000 - accuracy: 0.9992 - precision: 0.8646 - recall: 0.6306 - auc: 0.9209 - prc: 0.7156 - val_loss: 0.0037 - val_tp: 55.0000 - val_fp: 11.0000 - val_tn: 45477.0000 - val_fn: 26.0000 - val_accuracy: 0.9992 - val_precision: 0.8333 - val_recall: 0.6790 - val_auc: 0.9195 - val_prc: 0.7596 Epoch 10/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0042 - tp: 189.0000 - fp: 24.0000 - tn: 181938.0000 - fn: 125.0000 - accuracy: 0.9992 - precision: 0.8873 - recall: 0.6019 - auc: 0.9209 - prc: 0.7199 - val_loss: 0.0037 - val_tp: 61.0000 - val_fp: 11.0000 - val_tn: 45477.0000 - val_fn: 20.0000 - val_accuracy: 0.9993 - val_precision: 0.8472 - val_recall: 0.7531 - val_auc: 0.9257 - val_prc: 0.7715 Epoch 11/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0043 - tp: 199.0000 - fp: 24.0000 - tn: 181938.0000 - fn: 115.0000 - accuracy: 0.9992 - precision: 0.8924 - recall: 0.6338 - auc: 0.9112 - prc: 0.6912 - val_loss: 0.0036 - val_tp: 59.0000 - val_fp: 11.0000 - val_tn: 45477.0000 - val_fn: 22.0000 - val_accuracy: 0.9993 - val_precision: 0.8429 - val_recall: 0.7284 - val_auc: 0.9257 - val_prc: 0.7707 Epoch 12/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0042 - tp: 201.0000 - fp: 29.0000 - tn: 181933.0000 - fn: 113.0000 - accuracy: 0.9992 - precision: 0.8739 - recall: 0.6401 - auc: 0.9177 - prc: 0.7129 - val_loss: 0.0035 - val_tp: 55.0000 - val_fp: 11.0000 - val_tn: 45477.0000 - val_fn: 26.0000 - val_accuracy: 0.9992 - val_precision: 0.8333 - val_recall: 0.6790 - val_auc: 0.9257 - val_prc: 0.7750 Epoch 13/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0040 - tp: 189.0000 - fp: 26.0000 - tn: 181936.0000 - fn: 125.0000 - accuracy: 0.9992 - precision: 0.8791 - recall: 0.6019 - auc: 0.9258 - prc: 0.7330 - val_loss: 0.0035 - val_tp: 59.0000 - val_fp: 11.0000 - val_tn: 45477.0000 - val_fn: 22.0000 - val_accuracy: 0.9993 - val_precision: 0.8429 - val_recall: 0.7284 - val_auc: 0.9257 - val_prc: 0.7765 Epoch 14/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0038 - tp: 206.0000 - fp: 31.0000 - tn: 181931.0000 - fn: 108.0000 - accuracy: 0.9992 - precision: 0.8692 - recall: 0.6561 - auc: 0.9337 - prc: 0.7437 - val_loss: 0.0035 - val_tp: 59.0000 - val_fp: 11.0000 - val_tn: 45477.0000 - val_fn: 22.0000 - val_accuracy: 0.9993 - val_precision: 0.8429 - val_recall: 0.7284 - val_auc: 0.9257 - val_prc: 0.7786 Epoch 15/100 90/90 [==============================] - 0s 6ms/step - loss: 0.0040 - tp: 189.0000 - fp: 34.0000 - tn: 181928.0000 - fn: 125.0000 - accuracy: 0.9991 - precision: 0.8475 - recall: 0.6019 - auc: 0.9290 - prc: 0.7396 - val_loss: 0.0034 - val_tp: 60.0000 - val_fp: 11.0000 - val_tn: 45477.0000 - val_fn: 21.0000 - val_accuracy: 0.9993 - val_precision: 0.8451 - val_recall: 0.7407 - val_auc: 0.9257 - val_prc: 0.7819 Epoch 16/100 90/90 [==============================] - 1s 6ms/step - loss: 0.0037 - tp: 202.0000 - fp: 28.0000 - tn: 181934.0000 - fn: 112.0000 - accuracy: 0.9992 - precision: 0.8783 - recall: 0.6433 - auc: 0.9306 - prc: 0.7534 - val_loss: 0.0034 - val_tp: 61.0000 - val_fp: 11.0000 - val_tn: 45477.0000 - val_fn: 20.0000 - val_accuracy: 0.9993 - val_precision: 0.8472 - val_recall: 0.7531 - val_auc: 0.9256 - val_prc: 0.7810 Epoch 17/100 90/90 [==============================] - 0s 6ms/step - loss: 0.0038 - tp: 194.0000 - fp: 25.0000 - tn: 181937.0000 - fn: 120.0000 - accuracy: 0.9992 - precision: 0.8858 - recall: 0.6178 - auc: 0.9306 - prc: 0.7401 - val_loss: 0.0034 - val_tp: 61.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 20.0000 - val_accuracy: 0.9993 - val_precision: 0.8356 - val_recall: 0.7531 - val_auc: 0.9256 - val_prc: 0.7791 Epoch 18/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0040 - tp: 194.0000 - fp: 27.0000 - tn: 181935.0000 - fn: 120.0000 - accuracy: 0.9992 - precision: 0.8778 - recall: 0.6178 - auc: 0.9274 - prc: 0.7235 - val_loss: 0.0034 - val_tp: 62.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 19.0000 - val_accuracy: 0.9993 - val_precision: 0.8378 - val_recall: 0.7654 - val_auc: 0.9256 - val_prc: 0.7815 Epoch 19/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0037 - tp: 204.0000 - fp: 27.0000 - tn: 181935.0000 - fn: 110.0000 - accuracy: 0.9992 - precision: 0.8831 - recall: 0.6497 - auc: 0.9307 - prc: 0.7663 - val_loss: 0.0035 - val_tp: 63.0000 - val_fp: 13.0000 - val_tn: 45475.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8289 - val_recall: 0.7778 - val_auc: 0.9318 - val_prc: 0.7866 Epoch 20/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0038 - tp: 205.0000 - fp: 26.0000 - tn: 181936.0000 - fn: 109.0000 - accuracy: 0.9993 - precision: 0.8874 - recall: 0.6529 - auc: 0.9306 - prc: 0.7374 - val_loss: 0.0035 - val_tp: 63.0000 - val_fp: 13.0000 - val_tn: 45475.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8289 - val_recall: 0.7778 - val_auc: 0.9318 - val_prc: 0.7875 Epoch 21/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0034 - tp: 212.0000 - fp: 26.0000 - tn: 181936.0000 - fn: 102.0000 - accuracy: 0.9993 - precision: 0.8908 - recall: 0.6752 - auc: 0.9403 - prc: 0.7796 - val_loss: 0.0035 - val_tp: 63.0000 - val_fp: 13.0000 - val_tn: 45475.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8289 - val_recall: 0.7778 - val_auc: 0.9318 - val_prc: 0.7907 Epoch 22/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0037 - tp: 213.0000 - fp: 29.0000 - tn: 181933.0000 - fn: 101.0000 - accuracy: 0.9993 - precision: 0.8802 - recall: 0.6783 - auc: 0.9242 - prc: 0.7389 - val_loss: 0.0033 - val_tp: 61.0000 - val_fp: 11.0000 - val_tn: 45477.0000 - val_fn: 20.0000 - val_accuracy: 0.9993 - val_precision: 0.8472 - val_recall: 0.7531 - val_auc: 0.9318 - val_prc: 0.7946 Epoch 23/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0038 - tp: 198.0000 - fp: 28.0000 - tn: 181934.0000 - fn: 116.0000 - accuracy: 0.9992 - precision: 0.8761 - recall: 0.6306 - auc: 0.9339 - prc: 0.7417 - val_loss: 0.0033 - val_tp: 61.0000 - val_fp: 11.0000 - val_tn: 45477.0000 - val_fn: 20.0000 - val_accuracy: 0.9993 - val_precision: 0.8472 - val_recall: 0.7531 - val_auc: 0.9318 - val_prc: 0.7950 Epoch 24/100 90/90 [==============================] - 1s 6ms/step - loss: 0.0037 - tp: 199.0000 - fp: 25.0000 - tn: 181937.0000 - fn: 115.0000 - accuracy: 0.9992 - precision: 0.8884 - recall: 0.6338 - auc: 0.9275 - prc: 0.7536 - val_loss: 0.0033 - val_tp: 63.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8400 - val_recall: 0.7778 - val_auc: 0.9318 - val_prc: 0.7971 Epoch 25/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0036 - tp: 208.0000 - fp: 27.0000 - tn: 181935.0000 - fn: 106.0000 - accuracy: 0.9993 - precision: 0.8851 - recall: 0.6624 - auc: 0.9403 - prc: 0.7603 - val_loss: 0.0032 - val_tp: 63.0000 - val_fp: 11.0000 - val_tn: 45477.0000 - val_fn: 18.0000 - val_accuracy: 0.9994 - val_precision: 0.8514 - val_recall: 0.7778 - val_auc: 0.9380 - val_prc: 0.8047 Epoch 26/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - tp: 212.0000 - fp: 26.0000 - tn: 181936.0000 - fn: 102.0000 - accuracy: 0.9993 - precision: 0.8908 - recall: 0.6752 - auc: 0.9276 - prc: 0.7635 - val_loss: 0.0033 - val_tp: 62.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 19.0000 - val_accuracy: 0.9993 - val_precision: 0.8378 - val_recall: 0.7654 - val_auc: 0.9318 - val_prc: 0.7995 Epoch 27/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0036 - tp: 206.0000 - fp: 25.0000 - tn: 181937.0000 - fn: 108.0000 - accuracy: 0.9993 - precision: 0.8918 - recall: 0.6561 - auc: 0.9340 - prc: 0.7629 - val_loss: 0.0034 - val_tp: 63.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8400 - val_recall: 0.7778 - val_auc: 0.9380 - val_prc: 0.8007 Epoch 28/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0038 - tp: 217.0000 - fp: 29.0000 - tn: 181933.0000 - fn: 97.0000 - accuracy: 0.9993 - precision: 0.8821 - recall: 0.6911 - auc: 0.9291 - prc: 0.7392 - val_loss: 0.0035 - val_tp: 63.0000 - val_fp: 13.0000 - val_tn: 45475.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8289 - val_recall: 0.7778 - val_auc: 0.9441 - val_prc: 0.7940 Epoch 29/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - tp: 216.0000 - fp: 30.0000 - tn: 181932.0000 - fn: 98.0000 - accuracy: 0.9993 - precision: 0.8780 - recall: 0.6879 - auc: 0.9292 - prc: 0.7584 - val_loss: 0.0034 - val_tp: 63.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8400 - val_recall: 0.7778 - val_auc: 0.9441 - val_prc: 0.8047 Epoch 30/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0036 - tp: 215.0000 - fp: 29.0000 - tn: 181933.0000 - fn: 99.0000 - accuracy: 0.9993 - precision: 0.8811 - recall: 0.6847 - auc: 0.9338 - prc: 0.7556 - val_loss: 0.0033 - val_tp: 63.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8400 - val_recall: 0.7778 - val_auc: 0.9380 - val_prc: 0.8025 Epoch 31/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0037 - tp: 206.0000 - fp: 28.0000 - tn: 181934.0000 - fn: 108.0000 - accuracy: 0.9993 - precision: 0.8803 - recall: 0.6561 - auc: 0.9339 - prc: 0.7494 - val_loss: 0.0033 - val_tp: 63.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8400 - val_recall: 0.7778 - val_auc: 0.9380 - val_prc: 0.8059 Epoch 32/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0038 - tp: 201.0000 - fp: 25.0000 - tn: 181937.0000 - fn: 113.0000 - accuracy: 0.9992 - precision: 0.8894 - recall: 0.6401 - auc: 0.9308 - prc: 0.7472 - val_loss: 0.0033 - val_tp: 63.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8400 - val_recall: 0.7778 - val_auc: 0.9380 - val_prc: 0.8043 Epoch 33/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - tp: 217.0000 - fp: 28.0000 - tn: 181934.0000 - fn: 97.0000 - accuracy: 0.9993 - precision: 0.8857 - recall: 0.6911 - auc: 0.9355 - prc: 0.7679 - val_loss: 0.0033 - val_tp: 63.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8400 - val_recall: 0.7778 - val_auc: 0.9380 - val_prc: 0.8051 Epoch 34/100 90/90 [==============================] - 0s 6ms/step - loss: 0.0037 - tp: 209.0000 - fp: 29.0000 - tn: 181933.0000 - fn: 105.0000 - accuracy: 0.9993 - precision: 0.8782 - recall: 0.6656 - auc: 0.9355 - prc: 0.7411 - val_loss: 0.0033 - val_tp: 63.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8400 - val_recall: 0.7778 - val_auc: 0.9380 - val_prc: 0.8046 Epoch 35/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - tp: 203.0000 - fp: 27.0000 - tn: 181935.0000 - fn: 111.0000 - accuracy: 0.9992 - precision: 0.8826 - recall: 0.6465 - auc: 0.9308 - prc: 0.7662 - val_loss: 0.0033 - val_tp: 63.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8400 - val_recall: 0.7778 - val_auc: 0.9380 - val_prc: 0.8065 Epoch 36/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0036 - tp: 208.0000 - fp: 25.0000 - tn: 181937.0000 - fn: 106.0000 - accuracy: 0.9993 - precision: 0.8927 - recall: 0.6624 - auc: 0.9371 - prc: 0.7473 - val_loss: 0.0034 - val_tp: 63.0000 - val_fp: 13.0000 - val_tn: 45475.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8289 - val_recall: 0.7778 - val_auc: 0.9441 - val_prc: 0.8080 Epoch 37/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - tp: 224.0000 - fp: 29.0000 - tn: 181933.0000 - fn: 90.0000 - accuracy: 0.9993 - precision: 0.8854 - recall: 0.7134 - auc: 0.9324 - prc: 0.7676 - val_loss: 0.0033 - val_tp: 63.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8400 - val_recall: 0.7778 - val_auc: 0.9380 - val_prc: 0.8081 Epoch 38/100 90/90 [==============================] - 0s 6ms/step - loss: 0.0036 - tp: 210.0000 - fp: 31.0000 - tn: 181931.0000 - fn: 104.0000 - accuracy: 0.9993 - precision: 0.8714 - recall: 0.6688 - auc: 0.9308 - prc: 0.7517 - val_loss: 0.0034 - val_tp: 63.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8400 - val_recall: 0.7778 - val_auc: 0.9380 - val_prc: 0.8035 Epoch 39/100 90/90 [==============================] - 0s 6ms/step - loss: 0.0035 - tp: 213.0000 - fp: 28.0000 - tn: 181934.0000 - fn: 101.0000 - accuracy: 0.9993 - precision: 0.8838 - recall: 0.6783 - auc: 0.9340 - prc: 0.7633 - val_loss: 0.0033 - val_tp: 63.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8400 - val_recall: 0.7778 - val_auc: 0.9441 - val_prc: 0.8132 Epoch 40/100 90/90 [==============================] - 1s 6ms/step - loss: 0.0035 - tp: 206.0000 - fp: 27.0000 - tn: 181935.0000 - fn: 108.0000 - accuracy: 0.9993 - precision: 0.8841 - recall: 0.6561 - auc: 0.9356 - prc: 0.7639 - val_loss: 0.0034 - val_tp: 63.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8400 - val_recall: 0.7778 - val_auc: 0.9441 - val_prc: 0.8108 Epoch 41/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0034 - tp: 211.0000 - fp: 28.0000 - tn: 181934.0000 - fn: 103.0000 - accuracy: 0.9993 - precision: 0.8828 - recall: 0.6720 - auc: 0.9388 - prc: 0.7732 - val_loss: 0.0034 - val_tp: 63.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8400 - val_recall: 0.7778 - val_auc: 0.9441 - val_prc: 0.8123 Epoch 42/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0034 - tp: 215.0000 - fp: 27.0000 - tn: 181935.0000 - fn: 99.0000 - accuracy: 0.9993 - precision: 0.8884 - recall: 0.6847 - auc: 0.9356 - prc: 0.7786 - val_loss: 0.0033 - val_tp: 63.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8400 - val_recall: 0.7778 - val_auc: 0.9442 - val_prc: 0.8159 Epoch 43/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - tp: 211.0000 - fp: 30.0000 - tn: 181932.0000 - fn: 103.0000 - accuracy: 0.9993 - precision: 0.8755 - recall: 0.6720 - auc: 0.9324 - prc: 0.7492 - val_loss: 0.0033 - val_tp: 63.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8400 - val_recall: 0.7778 - val_auc: 0.9380 - val_prc: 0.8079 Epoch 44/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - tp: 203.0000 - fp: 26.0000 - tn: 181936.0000 - fn: 111.0000 - accuracy: 0.9992 - precision: 0.8865 - recall: 0.6465 - auc: 0.9324 - prc: 0.7567 - val_loss: 0.0033 - val_tp: 62.0000 - val_fp: 9.0000 - val_tn: 45479.0000 - val_fn: 19.0000 - val_accuracy: 0.9994 - val_precision: 0.8732 - val_recall: 0.7654 - val_auc: 0.9441 - val_prc: 0.8162 Epoch 45/100 90/90 [==============================] - 0s 6ms/step - loss: 0.0035 - tp: 207.0000 - fp: 28.0000 - tn: 181934.0000 - fn: 107.0000 - accuracy: 0.9993 - precision: 0.8809 - recall: 0.6592 - auc: 0.9355 - prc: 0.7558 - val_loss: 0.0032 - val_tp: 63.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8400 - val_recall: 0.7778 - val_auc: 0.9503 - val_prc: 0.8229 Epoch 46/100 90/90 [==============================] - 1s 6ms/step - loss: 0.0033 - tp: 216.0000 - fp: 25.0000 - tn: 181937.0000 - fn: 98.0000 - accuracy: 0.9993 - precision: 0.8963 - recall: 0.6879 - auc: 0.9404 - prc: 0.7758 - val_loss: 0.0032 - val_tp: 63.0000 - val_fp: 10.0000 - val_tn: 45478.0000 - val_fn: 18.0000 - val_accuracy: 0.9994 - val_precision: 0.8630 - val_recall: 0.7778 - val_auc: 0.9503 - val_prc: 0.8253 Epoch 47/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0034 - tp: 212.0000 - fp: 30.0000 - tn: 181932.0000 - fn: 102.0000 - accuracy: 0.9993 - precision: 0.8760 - recall: 0.6752 - auc: 0.9371 - prc: 0.7657 - val_loss: 0.0032 - val_tp: 63.0000 - val_fp: 11.0000 - val_tn: 45477.0000 - val_fn: 18.0000 - val_accuracy: 0.9994 - val_precision: 0.8514 - val_recall: 0.7778 - val_auc: 0.9503 - val_prc: 0.8245 Epoch 48/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0034 - tp: 205.0000 - fp: 31.0000 - tn: 181931.0000 - fn: 109.0000 - accuracy: 0.9992 - precision: 0.8686 - recall: 0.6529 - auc: 0.9341 - prc: 0.7665 - val_loss: 0.0032 - val_tp: 62.0000 - val_fp: 7.0000 - val_tn: 45481.0000 - val_fn: 19.0000 - val_accuracy: 0.9994 - val_precision: 0.8986 - val_recall: 0.7654 - val_auc: 0.9441 - val_prc: 0.8210 Epoch 49/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0034 - tp: 208.0000 - fp: 29.0000 - tn: 181933.0000 - fn: 106.0000 - accuracy: 0.9993 - precision: 0.8776 - recall: 0.6624 - auc: 0.9356 - prc: 0.7684 - val_loss: 0.0032 - val_tp: 63.0000 - val_fp: 10.0000 - val_tn: 45478.0000 - val_fn: 18.0000 - val_accuracy: 0.9994 - val_precision: 0.8630 - val_recall: 0.7778 - val_auc: 0.9503 - val_prc: 0.8242 Epoch 50/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - tp: 211.0000 - fp: 26.0000 - tn: 181936.0000 - fn: 103.0000 - accuracy: 0.9993 - precision: 0.8903 - recall: 0.6720 - auc: 0.9340 - prc: 0.7772 - val_loss: 0.0032 - val_tp: 63.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8400 - val_recall: 0.7778 - val_auc: 0.9441 - val_prc: 0.8195 Epoch 51/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - tp: 215.0000 - fp: 27.0000 - tn: 181935.0000 - fn: 99.0000 - accuracy: 0.9993 - precision: 0.8884 - recall: 0.6847 - auc: 0.9420 - prc: 0.7773 - val_loss: 0.0032 - val_tp: 63.0000 - val_fp: 10.0000 - val_tn: 45478.0000 - val_fn: 18.0000 - val_accuracy: 0.9994 - val_precision: 0.8630 - val_recall: 0.7778 - val_auc: 0.9441 - val_prc: 0.8200 Epoch 52/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - tp: 199.0000 - fp: 28.0000 - tn: 181934.0000 - fn: 115.0000 - accuracy: 0.9992 - precision: 0.8767 - recall: 0.6338 - auc: 0.9276 - prc: 0.7716 - val_loss: 0.0033 - val_tp: 63.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8400 - val_recall: 0.7778 - val_auc: 0.9503 - val_prc: 0.8228 Epoch 53/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - tp: 203.0000 - fp: 26.0000 - tn: 181936.0000 - fn: 111.0000 - accuracy: 0.9992 - precision: 0.8865 - recall: 0.6465 - auc: 0.9483 - prc: 0.7781 - val_loss: 0.0032 - val_tp: 63.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8400 - val_recall: 0.7778 - val_auc: 0.9441 - val_prc: 0.8212 Epoch 54/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0035 - tp: 206.0000 - fp: 29.0000 - tn: 181933.0000 - fn: 108.0000 - accuracy: 0.9992 - precision: 0.8766 - recall: 0.6561 - auc: 0.9324 - prc: 0.7506 - val_loss: 0.0032 - val_tp: 63.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8400 - val_recall: 0.7778 - val_auc: 0.9441 - val_prc: 0.8237 Epoch 55/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0032 - tp: 209.0000 - fp: 26.0000 - tn: 181936.0000 - fn: 105.0000 - accuracy: 0.9993 - precision: 0.8894 - recall: 0.6656 - auc: 0.9325 - prc: 0.7780 - val_loss: 0.0032 - val_tp: 63.0000 - val_fp: 10.0000 - val_tn: 45478.0000 - val_fn: 18.0000 - val_accuracy: 0.9994 - val_precision: 0.8630 - val_recall: 0.7778 - val_auc: 0.9503 - val_prc: 0.8266 Epoch 56/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - tp: 203.0000 - fp: 23.0000 - tn: 181939.0000 - fn: 111.0000 - accuracy: 0.9993 - precision: 0.8982 - recall: 0.6465 - auc: 0.9451 - prc: 0.7801 - val_loss: 0.0033 - val_tp: 64.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 17.0000 - val_accuracy: 0.9994 - val_precision: 0.8421 - val_recall: 0.7901 - val_auc: 0.9502 - val_prc: 0.8269 Epoch 57/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - tp: 208.0000 - fp: 30.0000 - tn: 181932.0000 - fn: 106.0000 - accuracy: 0.9993 - precision: 0.8739 - recall: 0.6624 - auc: 0.9435 - prc: 0.7781 - val_loss: 0.0032 - val_tp: 63.0000 - val_fp: 9.0000 - val_tn: 45479.0000 - val_fn: 18.0000 - val_accuracy: 0.9994 - val_precision: 0.8750 - val_recall: 0.7778 - val_auc: 0.9503 - val_prc: 0.8274 Epoch 58/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0032 - tp: 218.0000 - fp: 24.0000 - tn: 181938.0000 - fn: 96.0000 - accuracy: 0.9993 - precision: 0.9008 - recall: 0.6943 - auc: 0.9388 - prc: 0.7856 - val_loss: 0.0032 - val_tp: 64.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 17.0000 - val_accuracy: 0.9994 - val_precision: 0.8421 - val_recall: 0.7901 - val_auc: 0.9441 - val_prc: 0.8222 Epoch 59/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0032 - tp: 209.0000 - fp: 21.0000 - tn: 181941.0000 - fn: 105.0000 - accuracy: 0.9993 - precision: 0.9087 - recall: 0.6656 - auc: 0.9404 - prc: 0.7805 - val_loss: 0.0033 - val_tp: 64.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 17.0000 - val_accuracy: 0.9994 - val_precision: 0.8421 - val_recall: 0.7901 - val_auc: 0.9441 - val_prc: 0.8233 Epoch 60/100 90/90 [==============================] - 1s 6ms/step - loss: 0.0032 - tp: 208.0000 - fp: 23.0000 - tn: 181939.0000 - fn: 106.0000 - accuracy: 0.9993 - precision: 0.9004 - recall: 0.6624 - auc: 0.9340 - prc: 0.7812 - val_loss: 0.0033 - val_tp: 64.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 17.0000 - val_accuracy: 0.9994 - val_precision: 0.8421 - val_recall: 0.7901 - val_auc: 0.9503 - val_prc: 0.8245 Epoch 61/100 90/90 [==============================] - 0s 6ms/step - loss: 0.0030 - tp: 220.0000 - fp: 29.0000 - tn: 181933.0000 - fn: 94.0000 - accuracy: 0.9993 - precision: 0.8835 - recall: 0.7006 - auc: 0.9404 - prc: 0.7940 - val_loss: 0.0033 - val_tp: 63.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8400 - val_recall: 0.7778 - val_auc: 0.9441 - val_prc: 0.8243 Epoch 62/100 90/90 [==============================] - 0s 6ms/step - loss: 0.0032 - tp: 223.0000 - fp: 26.0000 - tn: 181936.0000 - fn: 91.0000 - accuracy: 0.9994 - precision: 0.8956 - recall: 0.7102 - auc: 0.9356 - prc: 0.7857 - val_loss: 0.0032 - val_tp: 63.0000 - val_fp: 11.0000 - val_tn: 45477.0000 - val_fn: 18.0000 - val_accuracy: 0.9994 - val_precision: 0.8514 - val_recall: 0.7778 - val_auc: 0.9441 - val_prc: 0.8238 Epoch 63/100 90/90 [==============================] - 1s 6ms/step - loss: 0.0034 - tp: 206.0000 - fp: 30.0000 - tn: 181932.0000 - fn: 108.0000 - accuracy: 0.9992 - precision: 0.8729 - recall: 0.6561 - auc: 0.9435 - prc: 0.7633 - val_loss: 0.0033 - val_tp: 63.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8400 - val_recall: 0.7778 - val_auc: 0.9441 - val_prc: 0.8242 Epoch 64/100 90/90 [==============================] - 1s 6ms/step - loss: 0.0031 - tp: 222.0000 - fp: 24.0000 - tn: 181938.0000 - fn: 92.0000 - accuracy: 0.9994 - precision: 0.9024 - recall: 0.7070 - auc: 0.9356 - prc: 0.7868 - val_loss: 0.0032 - val_tp: 63.0000 - val_fp: 11.0000 - val_tn: 45477.0000 - val_fn: 18.0000 - val_accuracy: 0.9994 - val_precision: 0.8514 - val_recall: 0.7778 - val_auc: 0.9503 - val_prc: 0.8318 Epoch 65/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0031 - tp: 213.0000 - fp: 27.0000 - tn: 181935.0000 - fn: 101.0000 - accuracy: 0.9993 - precision: 0.8875 - recall: 0.6783 - auc: 0.9373 - prc: 0.7931 - val_loss: 0.0032 - val_tp: 63.0000 - val_fp: 7.0000 - val_tn: 45481.0000 - val_fn: 18.0000 - val_accuracy: 0.9995 - val_precision: 0.9000 - val_recall: 0.7778 - val_auc: 0.9503 - val_prc: 0.8334 Epoch 66/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0032 - tp: 213.0000 - fp: 20.0000 - tn: 181942.0000 - fn: 101.0000 - accuracy: 0.9993 - precision: 0.9142 - recall: 0.6783 - auc: 0.9419 - prc: 0.7828 - val_loss: 0.0032 - val_tp: 64.0000 - val_fp: 10.0000 - val_tn: 45478.0000 - val_fn: 17.0000 - val_accuracy: 0.9994 - val_precision: 0.8649 - val_recall: 0.7901 - val_auc: 0.9503 - val_prc: 0.8326 Epoch 67/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0031 - tp: 215.0000 - fp: 21.0000 - tn: 181941.0000 - fn: 99.0000 - accuracy: 0.9993 - precision: 0.9110 - recall: 0.6847 - auc: 0.9436 - prc: 0.7967 - val_loss: 0.0032 - val_tp: 63.0000 - val_fp: 8.0000 - val_tn: 45480.0000 - val_fn: 18.0000 - val_accuracy: 0.9994 - val_precision: 0.8873 - val_recall: 0.7778 - val_auc: 0.9441 - val_prc: 0.8281 Epoch 68/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0032 - tp: 212.0000 - fp: 27.0000 - tn: 181935.0000 - fn: 102.0000 - accuracy: 0.9993 - precision: 0.8870 - recall: 0.6752 - auc: 0.9452 - prc: 0.7885 - val_loss: 0.0032 - val_tp: 63.0000 - val_fp: 8.0000 - val_tn: 45480.0000 - val_fn: 18.0000 - val_accuracy: 0.9994 - val_precision: 0.8873 - val_recall: 0.7778 - val_auc: 0.9503 - val_prc: 0.8329 Epoch 69/100 90/90 [==============================] - 1s 6ms/step - loss: 0.0033 - tp: 209.0000 - fp: 28.0000 - tn: 181934.0000 - fn: 105.0000 - accuracy: 0.9993 - precision: 0.8819 - recall: 0.6656 - auc: 0.9371 - prc: 0.7721 - val_loss: 0.0032 - val_tp: 63.0000 - val_fp: 8.0000 - val_tn: 45480.0000 - val_fn: 18.0000 - val_accuracy: 0.9994 - val_precision: 0.8873 - val_recall: 0.7778 - val_auc: 0.9503 - val_prc: 0.8345 Epoch 70/100 90/90 [==============================] - 1s 6ms/step - loss: 0.0034 - tp: 209.0000 - fp: 31.0000 - tn: 181931.0000 - fn: 105.0000 - accuracy: 0.9993 - precision: 0.8708 - recall: 0.6656 - auc: 0.9403 - prc: 0.7689 - val_loss: 0.0032 - val_tp: 63.0000 - val_fp: 8.0000 - val_tn: 45480.0000 - val_fn: 18.0000 - val_accuracy: 0.9994 - val_precision: 0.8873 - val_recall: 0.7778 - val_auc: 0.9503 - val_prc: 0.8338 Epoch 71/100 90/90 [==============================] - 1s 6ms/step - loss: 0.0030 - tp: 223.0000 - fp: 27.0000 - tn: 181935.0000 - fn: 91.0000 - accuracy: 0.9994 - precision: 0.8920 - recall: 0.7102 - auc: 0.9436 - prc: 0.7955 - val_loss: 0.0032 - val_tp: 58.0000 - val_fp: 7.0000 - val_tn: 45481.0000 - val_fn: 23.0000 - val_accuracy: 0.9993 - val_precision: 0.8923 - val_recall: 0.7160 - val_auc: 0.9503 - val_prc: 0.8335 Epoch 72/100 90/90 [==============================] - 1s 6ms/step - loss: 0.0033 - tp: 202.0000 - fp: 24.0000 - tn: 181938.0000 - fn: 112.0000 - accuracy: 0.9993 - precision: 0.8938 - recall: 0.6433 - auc: 0.9339 - prc: 0.7595 - val_loss: 0.0032 - val_tp: 63.0000 - val_fp: 8.0000 - val_tn: 45480.0000 - val_fn: 18.0000 - val_accuracy: 0.9994 - val_precision: 0.8873 - val_recall: 0.7778 - val_auc: 0.9503 - val_prc: 0.8332 Epoch 73/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - tp: 208.0000 - fp: 31.0000 - tn: 181931.0000 - fn: 106.0000 - accuracy: 0.9992 - precision: 0.8703 - recall: 0.6624 - auc: 0.9418 - prc: 0.7697 - val_loss: 0.0032 - val_tp: 58.0000 - val_fp: 8.0000 - val_tn: 45480.0000 - val_fn: 23.0000 - val_accuracy: 0.9993 - val_precision: 0.8788 - val_recall: 0.7160 - val_auc: 0.9503 - val_prc: 0.8364 Epoch 74/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0032 - tp: 213.0000 - fp: 24.0000 - tn: 181938.0000 - fn: 101.0000 - accuracy: 0.9993 - precision: 0.8987 - recall: 0.6783 - auc: 0.9403 - prc: 0.7833 - val_loss: 0.0032 - val_tp: 63.0000 - val_fp: 11.0000 - val_tn: 45477.0000 - val_fn: 18.0000 - val_accuracy: 0.9994 - val_precision: 0.8514 - val_recall: 0.7778 - val_auc: 0.9503 - val_prc: 0.8343 Epoch 75/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0032 - tp: 210.0000 - fp: 19.0000 - tn: 181943.0000 - fn: 104.0000 - accuracy: 0.9993 - precision: 0.9170 - recall: 0.6688 - auc: 0.9388 - prc: 0.7823 - val_loss: 0.0032 - val_tp: 63.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8400 - val_recall: 0.7778 - val_auc: 0.9503 - val_prc: 0.8339 Epoch 76/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0032 - tp: 210.0000 - fp: 28.0000 - tn: 181934.0000 - fn: 104.0000 - accuracy: 0.9993 - precision: 0.8824 - recall: 0.6688 - auc: 0.9418 - prc: 0.7756 - val_loss: 0.0032 - val_tp: 58.0000 - val_fp: 8.0000 - val_tn: 45480.0000 - val_fn: 23.0000 - val_accuracy: 0.9993 - val_precision: 0.8788 - val_recall: 0.7160 - val_auc: 0.9503 - val_prc: 0.8351 Epoch 77/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0030 - tp: 214.0000 - fp: 23.0000 - tn: 181939.0000 - fn: 100.0000 - accuracy: 0.9993 - precision: 0.9030 - recall: 0.6815 - auc: 0.9420 - prc: 0.7937 - val_loss: 0.0032 - val_tp: 63.0000 - val_fp: 8.0000 - val_tn: 45480.0000 - val_fn: 18.0000 - val_accuracy: 0.9994 - val_precision: 0.8873 - val_recall: 0.7778 - val_auc: 0.9503 - val_prc: 0.8341 Epoch 78/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - tp: 209.0000 - fp: 22.0000 - tn: 181940.0000 - fn: 105.0000 - accuracy: 0.9993 - precision: 0.9048 - recall: 0.6656 - auc: 0.9418 - prc: 0.7757 - val_loss: 0.0032 - val_tp: 63.0000 - val_fp: 9.0000 - val_tn: 45479.0000 - val_fn: 18.0000 - val_accuracy: 0.9994 - val_precision: 0.8750 - val_recall: 0.7778 - val_auc: 0.9503 - val_prc: 0.8337 Epoch 79/100 90/90 [==============================] - 1s 6ms/step - loss: 0.0032 - tp: 213.0000 - fp: 30.0000 - tn: 181932.0000 - fn: 101.0000 - accuracy: 0.9993 - precision: 0.8765 - recall: 0.6783 - auc: 0.9403 - prc: 0.7737 - val_loss: 0.0032 - val_tp: 63.0000 - val_fp: 8.0000 - val_tn: 45480.0000 - val_fn: 18.0000 - val_accuracy: 0.9994 - val_precision: 0.8873 - val_recall: 0.7778 - val_auc: 0.9503 - val_prc: 0.8342 Epoch 80/100 90/90 [==============================] - 1s 6ms/step - loss: 0.0032 - tp: 212.0000 - fp: 28.0000 - tn: 181934.0000 - fn: 102.0000 - accuracy: 0.9993 - precision: 0.8833 - recall: 0.6752 - auc: 0.9403 - prc: 0.7720 - val_loss: 0.0032 - val_tp: 58.0000 - val_fp: 8.0000 - val_tn: 45480.0000 - val_fn: 23.0000 - val_accuracy: 0.9993 - val_precision: 0.8788 - val_recall: 0.7160 - val_auc: 0.9503 - val_prc: 0.8380 Epoch 81/100 90/90 [==============================] - 0s 6ms/step - loss: 0.0031 - tp: 210.0000 - fp: 23.0000 - tn: 181939.0000 - fn: 104.0000 - accuracy: 0.9993 - precision: 0.9013 - recall: 0.6688 - auc: 0.9467 - prc: 0.8023 - val_loss: 0.0032 - val_tp: 64.0000 - val_fp: 11.0000 - val_tn: 45477.0000 - val_fn: 17.0000 - val_accuracy: 0.9994 - val_precision: 0.8533 - val_recall: 0.7901 - val_auc: 0.9502 - val_prc: 0.8332 Epoch 82/100 90/90 [==============================] - 1s 6ms/step - loss: 0.0030 - tp: 216.0000 - fp: 28.0000 - tn: 181934.0000 - fn: 98.0000 - accuracy: 0.9993 - precision: 0.8852 - recall: 0.6879 - auc: 0.9467 - prc: 0.8001 - val_loss: 0.0032 - val_tp: 64.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 17.0000 - val_accuracy: 0.9994 - val_precision: 0.8421 - val_recall: 0.7901 - val_auc: 0.9503 - val_prc: 0.8333 Epoch 83/100 90/90 [==============================] - 1s 6ms/step - loss: 0.0032 - tp: 214.0000 - fp: 27.0000 - tn: 181935.0000 - fn: 100.0000 - accuracy: 0.9993 - precision: 0.8880 - recall: 0.6815 - auc: 0.9435 - prc: 0.7755 - val_loss: 0.0032 - val_tp: 63.0000 - val_fp: 9.0000 - val_tn: 45479.0000 - val_fn: 18.0000 - val_accuracy: 0.9994 - val_precision: 0.8750 - val_recall: 0.7778 - val_auc: 0.9503 - val_prc: 0.8357 Epoch 84/100 90/90 [==============================] - 0s 6ms/step - loss: 0.0030 - tp: 216.0000 - fp: 21.0000 - tn: 181941.0000 - fn: 98.0000 - accuracy: 0.9993 - precision: 0.9114 - recall: 0.6879 - auc: 0.9451 - prc: 0.7989 - val_loss: 0.0032 - val_tp: 64.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 17.0000 - val_accuracy: 0.9994 - val_precision: 0.8421 - val_recall: 0.7901 - val_auc: 0.9503 - val_prc: 0.8344 Epoch 85/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - tp: 211.0000 - fp: 25.0000 - tn: 181937.0000 - fn: 103.0000 - accuracy: 0.9993 - precision: 0.8941 - recall: 0.6720 - auc: 0.9403 - prc: 0.7796 - val_loss: 0.0032 - val_tp: 63.0000 - val_fp: 9.0000 - val_tn: 45479.0000 - val_fn: 18.0000 - val_accuracy: 0.9994 - val_precision: 0.8750 - val_recall: 0.7778 - val_auc: 0.9503 - val_prc: 0.8368 Epoch 86/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0032 - tp: 206.0000 - fp: 27.0000 - tn: 181935.0000 - fn: 108.0000 - accuracy: 0.9993 - precision: 0.8841 - recall: 0.6561 - auc: 0.9515 - prc: 0.7932 - val_loss: 0.0033 - val_tp: 64.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 17.0000 - val_accuracy: 0.9994 - val_precision: 0.8421 - val_recall: 0.7901 - val_auc: 0.9503 - val_prc: 0.8350 Epoch 87/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0033 - tp: 208.0000 - fp: 24.0000 - tn: 181938.0000 - fn: 106.0000 - accuracy: 0.9993 - precision: 0.8966 - recall: 0.6624 - auc: 0.9403 - prc: 0.7714 - val_loss: 0.0033 - val_tp: 63.0000 - val_fp: 9.0000 - val_tn: 45479.0000 - val_fn: 18.0000 - val_accuracy: 0.9994 - val_precision: 0.8750 - val_recall: 0.7778 - val_auc: 0.9503 - val_prc: 0.8372 Epoch 88/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0032 - tp: 215.0000 - fp: 26.0000 - tn: 181936.0000 - fn: 99.0000 - accuracy: 0.9993 - precision: 0.8921 - recall: 0.6847 - auc: 0.9482 - prc: 0.7848 - val_loss: 0.0033 - val_tp: 63.0000 - val_fp: 12.0000 - val_tn: 45476.0000 - val_fn: 18.0000 - val_accuracy: 0.9993 - val_precision: 0.8400 - val_recall: 0.7778 - val_auc: 0.9503 - val_prc: 0.8360 Epoch 89/100 90/90 [==============================] - 0s 5ms/step - loss: 0.0030 - tp: 224.0000 - fp: 24.0000 - tn: 181938.0000 - fn: 90.0000 - accuracy: 0.9994 - precision: 0.9032 - recall: 0.7134 - auc: 0.9451 - prc: 0.7970 - val_loss: 0.0032 - val_tp: 58.0000 - val_fp: 8.0000 - val_tn: 45480.0000 - val_fn: 23.0000 - val_accuracy: 0.9993 - val_precision: 0.8788 - val_recall: 0.7160 - val_auc: 0.9503 - val_prc: 0.8369 Epoch 90/100 79/90 [=========================>....] - ETA: 0s - loss: 0.0033 - tp: 186.0000 - fp: 20.0000 - tn: 161496.0000 - fn: 90.0000 - accuracy: 0.9993 - precision: 0.9029 - recall: 0.6739 - auc: 0.9448 - prc: 0.7648Restoring model weights from the end of the best epoch: 80. 90/90 [==============================] - 0s 6ms/step - loss: 0.0031 - tp: 218.0000 - fp: 22.0000 - tn: 181940.0000 - fn: 96.0000 - accuracy: 0.9994 - precision: 0.9083 - recall: 0.6943 - auc: 0.9467 - prc: 0.7786 - val_loss: 0.0032 - val_tp: 60.0000 - val_fp: 8.0000 - val_tn: 45480.0000 - val_fn: 21.0000 - val_accuracy: 0.9994 - val_precision: 0.8824 - val_recall: 0.7407 - val_auc: 0.9503 - val_prc: 0.8375 Epoch 90: 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)
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.0034967793617397547 tp : 72.0 fp : 8.0 tn : 56857.0 fn : 25.0 accuracy : 0.9994206428527832 precision : 0.8999999761581421 recall : 0.7422680258750916 auc : 0.927476704120636 prc : 0.7999517917633057 Legitimate Transactions Detected (True Negatives): 56857 Legitimate Transactions Incorrectly Detected (False Positives): 8 Fraudulent Transactions Missed (False Negatives): 25 Fraudulent Transactions Detected (True Positives): 72 Total Fraudulent Transactions: 97
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');
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');
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 11ms/step - loss: 1.1025 - tp: 210.0000 - fp: 588.0000 - tn: 238239.0000 - fn: 201.0000 - accuracy: 0.9967 - precision: 0.2632 - recall: 0.5109 - auc: 0.8965 - prc: 0.3408 - val_loss: 0.0114 - val_tp: 56.0000 - val_fp: 41.0000 - val_tn: 45447.0000 - val_fn: 25.0000 - val_accuracy: 0.9986 - val_precision: 0.5773 - val_recall: 0.6914 - val_auc: 0.9455 - val_prc: 0.6191 Epoch 2/100 90/90 [==============================] - 1s 6ms/step - loss: 0.5621 - tp: 223.0000 - fp: 1125.0000 - tn: 180837.0000 - fn: 91.0000 - accuracy: 0.9933 - precision: 0.1654 - recall: 0.7102 - auc: 0.9329 - prc: 0.4102 - val_loss: 0.0168 - val_tp: 67.0000 - val_fp: 81.0000 - val_tn: 45407.0000 - val_fn: 14.0000 - val_accuracy: 0.9979 - val_precision: 0.4527 - val_recall: 0.8272 - val_auc: 0.9617 - val_prc: 0.6582 Epoch 3/100 90/90 [==============================] - 1s 6ms/step - loss: 0.4982 - tp: 229.0000 - fp: 1838.0000 - tn: 180124.0000 - fn: 85.0000 - accuracy: 0.9895 - precision: 0.1108 - recall: 0.7293 - auc: 0.9385 - prc: 0.3783 - val_loss: 0.0244 - val_tp: 67.0000 - val_fp: 145.0000 - val_tn: 45343.0000 - val_fn: 14.0000 - val_accuracy: 0.9965 - val_precision: 0.3160 - val_recall: 0.8272 - val_auc: 0.9612 - val_prc: 0.6633 Epoch 4/100 90/90 [==============================] - 0s 6ms/step - loss: 0.3663 - tp: 246.0000 - fp: 3063.0000 - tn: 178899.0000 - fn: 68.0000 - accuracy: 0.9828 - precision: 0.0743 - recall: 0.7834 - auc: 0.9502 - prc: 0.3940 - val_loss: 0.0333 - val_tp: 67.0000 - val_fp: 221.0000 - val_tn: 45267.0000 - val_fn: 14.0000 - val_accuracy: 0.9948 - val_precision: 0.2326 - val_recall: 0.8272 - val_auc: 0.9658 - val_prc: 0.6699 Epoch 5/100 90/90 [==============================] - 1s 6ms/step - loss: 0.3579 - tp: 251.0000 - fp: 4357.0000 - tn: 177605.0000 - fn: 63.0000 - accuracy: 0.9758 - precision: 0.0545 - recall: 0.7994 - auc: 0.9478 - prc: 0.3349 - val_loss: 0.0446 - val_tp: 70.0000 - val_fp: 354.0000 - val_tn: 45134.0000 - val_fn: 11.0000 - val_accuracy: 0.9920 - val_precision: 0.1651 - val_recall: 0.8642 - val_auc: 0.9685 - val_prc: 0.6304 Epoch 6/100 90/90 [==============================] - 1s 6ms/step - loss: 0.3682 - tp: 259.0000 - fp: 5524.0000 - tn: 176438.0000 - fn: 55.0000 - accuracy: 0.9694 - precision: 0.0448 - recall: 0.8248 - auc: 0.9403 - prc: 0.2853 - val_loss: 0.0539 - val_tp: 71.0000 - val_fp: 515.0000 - val_tn: 44973.0000 - val_fn: 10.0000 - val_accuracy: 0.9885 - val_precision: 0.1212 - val_recall: 0.8765 - val_auc: 0.9691 - val_prc: 0.5536 Epoch 7/100 90/90 [==============================] - 1s 6ms/step - loss: 0.2656 - tp: 272.0000 - fp: 6172.0000 - tn: 175790.0000 - fn: 42.0000 - accuracy: 0.9659 - precision: 0.0422 - recall: 0.8662 - auc: 0.9638 - prc: 0.2857 - val_loss: 0.0598 - val_tp: 72.0000 - val_fp: 610.0000 - val_tn: 44878.0000 - val_fn: 9.0000 - val_accuracy: 0.9864 - val_precision: 0.1056 - val_recall: 0.8889 - val_auc: 0.9684 - val_prc: 0.5479 Epoch 8/100 90/90 [==============================] - 0s 6ms/step - loss: 0.2976 - tp: 269.0000 - fp: 6541.0000 - tn: 175421.0000 - fn: 45.0000 - accuracy: 0.9639 - precision: 0.0395 - recall: 0.8567 - auc: 0.9509 - prc: 0.2741 - val_loss: 0.0647 - val_tp: 72.0000 - val_fp: 665.0000 - val_tn: 44823.0000 - val_fn: 9.0000 - val_accuracy: 0.9852 - val_precision: 0.0977 - val_recall: 0.8889 - val_auc: 0.9680 - val_prc: 0.5290 Epoch 9/100 90/90 [==============================] - 0s 6ms/step - loss: 0.2602 - tp: 271.0000 - fp: 7145.0000 - tn: 174817.0000 - fn: 43.0000 - accuracy: 0.9606 - precision: 0.0365 - recall: 0.8631 - auc: 0.9636 - prc: 0.2577 - val_loss: 0.0696 - val_tp: 73.0000 - val_fp: 733.0000 - val_tn: 44755.0000 - val_fn: 8.0000 - val_accuracy: 0.9837 - val_precision: 0.0906 - val_recall: 0.9012 - val_auc: 0.9684 - val_prc: 0.5221 Epoch 10/100 90/90 [==============================] - 1s 6ms/step - loss: 0.2536 - tp: 276.0000 - fp: 7339.0000 - tn: 174623.0000 - fn: 38.0000 - accuracy: 0.9595 - precision: 0.0362 - recall: 0.8790 - auc: 0.9638 - prc: 0.2559 - val_loss: 0.0731 - val_tp: 73.0000 - val_fp: 782.0000 - val_tn: 44706.0000 - val_fn: 8.0000 - val_accuracy: 0.9827 - val_precision: 0.0854 - val_recall: 0.9012 - val_auc: 0.9684 - val_prc: 0.5147 Epoch 11/100 90/90 [==============================] - 1s 6ms/step - loss: 0.2511 - tp: 272.0000 - fp: 7558.0000 - tn: 174404.0000 - fn: 42.0000 - accuracy: 0.9583 - precision: 0.0347 - recall: 0.8662 - auc: 0.9650 - prc: 0.2357 - val_loss: 0.0782 - val_tp: 74.0000 - val_fp: 861.0000 - val_tn: 44627.0000 - val_fn: 7.0000 - val_accuracy: 0.9810 - val_precision: 0.0791 - val_recall: 0.9136 - val_auc: 0.9688 - val_prc: 0.4952 Epoch 12/100 90/90 [==============================] - 1s 6ms/step - loss: 0.2387 - tp: 278.0000 - fp: 7560.0000 - tn: 174402.0000 - fn: 36.0000 - accuracy: 0.9583 - precision: 0.0355 - recall: 0.8854 - auc: 0.9701 - prc: 0.2369 - val_loss: 0.0789 - val_tp: 74.0000 - val_fp: 872.0000 - val_tn: 44616.0000 - val_fn: 7.0000 - val_accuracy: 0.9807 - val_precision: 0.0782 - val_recall: 0.9136 - val_auc: 0.9708 - val_prc: 0.5010 Epoch 13/100 90/90 [==============================] - 1s 6ms/step - loss: 0.2682 - tp: 278.0000 - fp: 7623.0000 - tn: 174339.0000 - fn: 36.0000 - accuracy: 0.9580 - precision: 0.0352 - recall: 0.8854 - auc: 0.9593 - prc: 0.2581 - val_loss: 0.0774 - val_tp: 74.0000 - val_fp: 849.0000 - val_tn: 44639.0000 - val_fn: 7.0000 - val_accuracy: 0.9812 - val_precision: 0.0802 - val_recall: 0.9136 - val_auc: 0.9712 - val_prc: 0.5023 Epoch 14/100 86/90 [===========================>..] - ETA: 0s - loss: 0.2844 - tp: 267.0000 - fp: 7282.0000 - tn: 168541.0000 - fn: 38.0000 - accuracy: 0.9584 - precision: 0.0354 - recall: 0.8754 - auc: 0.9561 - prc: 0.2436Restoring model weights from the end of the best epoch: 4. 90/90 [==============================] - 1s 6ms/step - loss: 0.2914 - tp: 273.0000 - fp: 7515.0000 - tn: 174447.0000 - fn: 41.0000 - accuracy: 0.9585 - precision: 0.0351 - recall: 0.8694 - auc: 0.9550 - prc: 0.2436 - val_loss: 0.0797 - val_tp: 75.0000 - val_fp: 882.0000 - val_tn: 44606.0000 - val_fn: 6.0000 - val_accuracy: 0.9805 - val_precision: 0.0784 - val_recall: 0.9259 - val_auc: 0.9708 - val_prc: 0.5038 Epoch 14: early stopping
Check training history
plot_metrics(weighted_history)
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.03274456411600113 tp : 81.0 fp : 252.0 tn : 56613.0 fn : 16.0 accuracy : 0.9952951073646545 precision : 0.2432432472705841 recall : 0.8350515365600586 auc : 0.9679247140884399 prc : 0.6531848311424255 Legitimate Transactions Detected (True Negatives): 56613 Legitimate Transactions Incorrectly Detected (False Positives): 252 Fraudulent Transactions Missed (False Negatives): 16 Fraudulent Transactions Detected (True Positives): 81 Total Fraudulent Transactions: 97
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');
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');
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
(181962, 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
(363924, 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: [-1.45352952 0.79696871 -0.66601721 1.19337228 -0.53348935 -0.59578957 -2.0979605 0.8951992 -1.03275912 -3.61762633 2.51868193 -2.2973935 0.56276617 -5. -2.0200714 -2.99465523 -5. -1.470018 0.49047894 0.01528834 0.71729537 0.3147249 -1.19057248 -0.11189896 0.41024869 -0.93852853 0.85165508 0.58924332 -0.06818104] 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.50927734375
To use this dataset, you'll need the number of steps per epoch.
The definition of "epoch" in this case is less clear. Say it's the number of batches required to see each negative example once:
resampled_steps_per_epoch = np.ceil(2.0*neg/BATCH_SIZE)
resampled_steps_per_epoch
278.0
Train on the oversampled data
Now try training the model with the resampled data set instead of using class weights to see how these methods compare.
resampled_model = make_model()
resampled_model.load_weights(initial_weights)
# Reset the bias to zero, since this dataset is balanced.
output_layer = resampled_model.layers[-1]
output_layer.bias.assign([0])
val_ds = tf.data.Dataset.from_tensor_slices((val_features, val_labels)).cache()
val_ds = val_ds.batch(BATCH_SIZE).prefetch(2)
resampled_history = resampled_model.fit(
resampled_ds,
epochs=EPOCHS,
steps_per_epoch=resampled_steps_per_epoch,
callbacks=[early_stopping],
validation_data=val_ds)
Epoch 1/100 278/278 [==============================] - 9s 26ms/step - loss: 0.3300 - tp: 261082.0000 - fp: 67295.0000 - tn: 274419.0000 - fn: 23510.0000 - accuracy: 0.8550 - precision: 0.7951 - recall: 0.9174 - auc: 0.9558 - prc: 0.9586 - val_loss: 0.1785 - val_tp: 75.0000 - val_fp: 1195.0000 - val_tn: 44293.0000 - val_fn: 6.0000 - val_accuracy: 0.9736 - val_precision: 0.0591 - val_recall: 0.9259 - val_auc: 0.9764 - val_prc: 0.7125 Epoch 2/100 278/278 [==============================] - 6s 23ms/step - loss: 0.1676 - tp: 264165.0000 - fp: 16889.0000 - tn: 267825.0000 - fn: 20465.0000 - accuracy: 0.9344 - precision: 0.9399 - recall: 0.9281 - auc: 0.9838 - prc: 0.9859 - val_loss: 0.0980 - val_tp: 75.0000 - val_fp: 974.0000 - val_tn: 44514.0000 - val_fn: 6.0000 - val_accuracy: 0.9785 - val_precision: 0.0715 - val_recall: 0.9259 - val_auc: 0.9755 - val_prc: 0.7231 Epoch 3/100 278/278 [==============================] - 6s 23ms/step - loss: 0.1284 - tp: 268163.0000 - fp: 12440.0000 - tn: 271299.0000 - fn: 17442.0000 - accuracy: 0.9475 - precision: 0.9557 - recall: 0.9389 - auc: 0.9910 - prc: 0.9916 - val_loss: 0.0751 - val_tp: 75.0000 - val_fp: 901.0000 - val_tn: 44587.0000 - val_fn: 6.0000 - val_accuracy: 0.9801 - val_precision: 0.0768 - val_recall: 0.9259 - val_auc: 0.9744 - val_prc: 0.7121 Epoch 4/100 278/278 [==============================] - 7s 24ms/step - loss: 0.1085 - tp: 270417.0000 - fp: 10773.0000 - tn: 273770.0000 - fn: 14384.0000 - accuracy: 0.9558 - precision: 0.9617 - recall: 0.9495 - auc: 0.9939 - prc: 0.9940 - val_loss: 0.0620 - val_tp: 75.0000 - val_fp: 851.0000 - val_tn: 44637.0000 - val_fn: 6.0000 - val_accuracy: 0.9812 - val_precision: 0.0810 - val_recall: 0.9259 - val_auc: 0.9754 - val_prc: 0.6973 Epoch 5/100 278/278 [==============================] - 6s 23ms/step - loss: 0.0972 - tp: 272156.0000 - fp: 10008.0000 - tn: 274621.0000 - fn: 12559.0000 - accuracy: 0.9604 - precision: 0.9645 - recall: 0.9559 - auc: 0.9951 - prc: 0.9950 - val_loss: 0.0549 - val_tp: 75.0000 - val_fp: 842.0000 - val_tn: 44646.0000 - val_fn: 6.0000 - val_accuracy: 0.9814 - val_precision: 0.0818 - val_recall: 0.9259 - val_auc: 0.9692 - val_prc: 0.6978 Epoch 6/100 278/278 [==============================] - 6s 23ms/step - loss: 0.0895 - tp: 273467.0000 - fp: 9649.0000 - tn: 275101.0000 - fn: 11127.0000 - accuracy: 0.9635 - precision: 0.9659 - recall: 0.9609 - auc: 0.9958 - prc: 0.9956 - val_loss: 0.0485 - val_tp: 75.0000 - val_fp: 782.0000 - val_tn: 44706.0000 - val_fn: 6.0000 - val_accuracy: 0.9827 - val_precision: 0.0875 - val_recall: 0.9259 - val_auc: 0.9667 - val_prc: 0.7006 Epoch 7/100 278/278 [==============================] - 7s 24ms/step - loss: 0.0832 - tp: 274498.0000 - fp: 9373.0000 - tn: 275563.0000 - fn: 9910.0000 - accuracy: 0.9661 - precision: 0.9670 - recall: 0.9652 - auc: 0.9963 - prc: 0.9962 - val_loss: 0.0451 - val_tp: 74.0000 - val_fp: 749.0000 - val_tn: 44739.0000 - val_fn: 7.0000 - val_accuracy: 0.9834 - val_precision: 0.0899 - val_recall: 0.9136 - val_auc: 0.9680 - val_prc: 0.6990 Epoch 8/100 278/278 [==============================] - 6s 23ms/step - loss: 0.0788 - tp: 275682.0000 - fp: 9166.0000 - tn: 275405.0000 - fn: 9091.0000 - accuracy: 0.9679 - precision: 0.9678 - recall: 0.9681 - auc: 0.9966 - prc: 0.9964 - val_loss: 0.0422 - val_tp: 74.0000 - val_fp: 729.0000 - val_tn: 44759.0000 - val_fn: 7.0000 - val_accuracy: 0.9838 - val_precision: 0.0922 - val_recall: 0.9136 - val_auc: 0.9689 - val_prc: 0.7002 Epoch 9/100 278/278 [==============================] - 6s 23ms/step - loss: 0.0747 - tp: 276283.0000 - fp: 8934.0000 - tn: 275833.0000 - fn: 8294.0000 - accuracy: 0.9697 - precision: 0.9687 - recall: 0.9709 - auc: 0.9969 - prc: 0.9967 - val_loss: 0.0399 - val_tp: 75.0000 - val_fp: 682.0000 - val_tn: 44806.0000 - val_fn: 6.0000 - val_accuracy: 0.9849 - val_precision: 0.0991 - val_recall: 0.9259 - val_auc: 0.9695 - val_prc: 0.7007 Epoch 10/100 278/278 [==============================] - 6s 23ms/step - loss: 0.0720 - tp: 277998.0000 - fp: 8933.0000 - tn: 275246.0000 - fn: 7167.0000 - accuracy: 0.9717 - precision: 0.9689 - recall: 0.9749 - auc: 0.9971 - prc: 0.9968 - val_loss: 0.0383 - val_tp: 73.0000 - val_fp: 682.0000 - val_tn: 44806.0000 - val_fn: 8.0000 - val_accuracy: 0.9849 - val_precision: 0.0967 - val_recall: 0.9012 - val_auc: 0.9698 - val_prc: 0.7018 Epoch 11/100 278/278 [==============================] - 6s 23ms/step - loss: 0.0692 - tp: 278186.0000 - fp: 8659.0000 - tn: 276014.0000 - fn: 6485.0000 - accuracy: 0.9734 - precision: 0.9698 - recall: 0.9772 - auc: 0.9973 - prc: 0.9970 - val_loss: 0.0367 - val_tp: 74.0000 - val_fp: 660.0000 - val_tn: 44828.0000 - val_fn: 7.0000 - val_accuracy: 0.9854 - val_precision: 0.1008 - val_recall: 0.9136 - val_auc: 0.9702 - val_prc: 0.7020 Epoch 12/100 276/278 [============================>.] - ETA: 0s - loss: 0.0670 - tp: 276112.0000 - fp: 8604.0000 - tn: 274316.0000 - fn: 6216.0000 - accuracy: 0.9738 - precision: 0.9698 - recall: 0.9780 - auc: 0.9974 - prc: 0.9971Restoring model weights from the end of the best epoch: 2. 278/278 [==============================] - 6s 23ms/step - loss: 0.0671 - tp: 278092.0000 - fp: 8677.0000 - tn: 276316.0000 - fn: 6259.0000 - accuracy: 0.9738 - precision: 0.9697 - recall: 0.9780 - auc: 0.9974 - prc: 0.9971 - val_loss: 0.0353 - val_tp: 74.0000 - val_fp: 633.0000 - val_tn: 44855.0000 - val_fn: 7.0000 - val_accuracy: 0.9860 - val_precision: 0.1047 - val_recall: 0.9136 - val_auc: 0.9704 - val_prc: 0.7018 Epoch 12: 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)
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 [==============================] - 3s 51ms/step - loss: 0.6201 - tp: 18361.0000 - fp: 12180.0000 - tn: 53666.0000 - fn: 2322.0000 - accuracy: 0.8324 - precision: 0.6012 - recall: 0.8877 - auc: 0.9476 - prc: 0.8715 - val_loss: 0.8273 - val_tp: 78.0000 - val_fp: 24283.0000 - val_tn: 21205.0000 - val_fn: 3.0000 - val_accuracy: 0.4670 - val_precision: 0.0032 - val_recall: 0.9630 - val_auc: 0.9378 - val_prc: 0.4261 Epoch 2/1000 20/20 [==============================] - 1s 27ms/step - loss: 0.5165 - tp: 18820.0000 - fp: 10072.0000 - tn: 10384.0000 - fn: 1684.0000 - accuracy: 0.7130 - precision: 0.6514 - recall: 0.9179 - auc: 0.8998 - prc: 0.9236 - val_loss: 0.6968 - val_tp: 76.0000 - val_fp: 18526.0000 - val_tn: 26962.0000 - val_fn: 5.0000 - val_accuracy: 0.5933 - val_precision: 0.0041 - val_recall: 0.9383 - val_auc: 0.9461 - val_prc: 0.5271 Epoch 3/1000 20/20 [==============================] - 1s 28ms/step - loss: 0.4434 - tp: 19145.0000 - fp: 8327.0000 - tn: 11911.0000 - fn: 1577.0000 - accuracy: 0.7582 - precision: 0.6969 - recall: 0.9239 - auc: 0.9211 - prc: 0.9418 - val_loss: 0.5855 - val_tp: 76.0000 - val_fp: 13098.0000 - val_tn: 32390.0000 - val_fn: 5.0000 - val_accuracy: 0.7125 - val_precision: 0.0058 - val_recall: 0.9383 - val_auc: 0.9522 - val_prc: 0.6018 Epoch 4/1000 20/20 [==============================] - 1s 28ms/step - loss: 0.3971 - tp: 18676.0000 - fp: 6939.0000 - tn: 13732.0000 - fn: 1613.0000 - accuracy: 0.7912 - precision: 0.7291 - recall: 0.9205 - auc: 0.9333 - prc: 0.9494 - val_loss: 0.4957 - val_tp: 74.0000 - val_fp: 8821.0000 - val_tn: 36667.0000 - val_fn: 7.0000 - val_accuracy: 0.8063 - val_precision: 0.0083 - val_recall: 0.9136 - val_auc: 0.9582 - val_prc: 0.6391 Epoch 5/1000 20/20 [==============================] - 1s 30ms/step - loss: 0.3539 - tp: 18791.0000 - fp: 5597.0000 - tn: 14895.0000 - fn: 1677.0000 - accuracy: 0.8224 - precision: 0.7705 - recall: 0.9181 - auc: 0.9425 - prc: 0.9574 - val_loss: 0.4256 - val_tp: 74.0000 - val_fp: 5917.0000 - val_tn: 39571.0000 - val_fn: 7.0000 - val_accuracy: 0.8700 - val_precision: 0.0124 - val_recall: 0.9136 - val_auc: 0.9627 - val_prc: 0.6553 Epoch 6/1000 20/20 [==============================] - 1s 30ms/step - loss: 0.3204 - tp: 18968.0000 - fp: 4538.0000 - tn: 15788.0000 - fn: 1666.0000 - accuracy: 0.8485 - precision: 0.8069 - recall: 0.9193 - auc: 0.9496 - prc: 0.9629 - val_loss: 0.3721 - val_tp: 74.0000 - val_fp: 4201.0000 - val_tn: 41287.0000 - val_fn: 7.0000 - val_accuracy: 0.9077 - val_precision: 0.0173 - val_recall: 0.9136 - val_auc: 0.9667 - val_prc: 0.6676 Epoch 7/1000 20/20 [==============================] - 1s 28ms/step - loss: 0.2984 - tp: 18860.0000 - fp: 3840.0000 - tn: 16600.0000 - fn: 1660.0000 - accuracy: 0.8657 - precision: 0.8308 - recall: 0.9191 - auc: 0.9549 - prc: 0.9662 - val_loss: 0.3278 - val_tp: 74.0000 - val_fp: 3004.0000 - val_tn: 42484.0000 - val_fn: 7.0000 - val_accuracy: 0.9339 - val_precision: 0.0240 - val_recall: 0.9136 - val_auc: 0.9699 - val_prc: 0.6773 Epoch 8/1000 20/20 [==============================] - 1s 28ms/step - loss: 0.2782 - tp: 18666.0000 - fp: 3317.0000 - tn: 17303.0000 - fn: 1674.0000 - accuracy: 0.8781 - precision: 0.8491 - recall: 0.9177 - auc: 0.9594 - prc: 0.9690 - val_loss: 0.2909 - val_tp: 74.0000 - val_fp: 2254.0000 - val_tn: 43234.0000 - val_fn: 7.0000 - val_accuracy: 0.9504 - val_precision: 0.0318 - val_recall: 0.9136 - val_auc: 0.9721 - val_prc: 0.6832 Epoch 9/1000 20/20 [==============================] - 1s 29ms/step - loss: 0.2605 - tp: 18624.0000 - fp: 2839.0000 - tn: 17810.0000 - fn: 1687.0000 - accuracy: 0.8895 - precision: 0.8677 - recall: 0.9169 - auc: 0.9632 - prc: 0.9718 - val_loss: 0.2620 - val_tp: 74.0000 - val_fp: 1835.0000 - val_tn: 43653.0000 - val_fn: 7.0000 - val_accuracy: 0.9596 - val_precision: 0.0388 - val_recall: 0.9136 - val_auc: 0.9734 - val_prc: 0.6925 Epoch 10/1000 20/20 [==============================] - 1s 29ms/step - loss: 0.2446 - tp: 19007.0000 - fp: 2353.0000 - tn: 17919.0000 - fn: 1681.0000 - accuracy: 0.9015 - precision: 0.8898 - recall: 0.9187 - auc: 0.9661 - prc: 0.9744 - val_loss: 0.2393 - val_tp: 75.0000 - val_fp: 1589.0000 - val_tn: 43899.0000 - val_fn: 6.0000 - val_accuracy: 0.9650 - val_precision: 0.0451 - val_recall: 0.9259 - val_auc: 0.9746 - val_prc: 0.6991 Epoch 11/1000 20/20 [==============================] - 1s 29ms/step - loss: 0.2357 - tp: 18790.0000 - fp: 2209.0000 - tn: 18283.0000 - fn: 1678.0000 - accuracy: 0.9051 - precision: 0.8948 - recall: 0.9180 - auc: 0.9687 - prc: 0.9755 - val_loss: 0.2203 - val_tp: 75.0000 - val_fp: 1427.0000 - val_tn: 44061.0000 - val_fn: 6.0000 - val_accuracy: 0.9686 - val_precision: 0.0499 - val_recall: 0.9259 - val_auc: 0.9753 - val_prc: 0.7045 Epoch 12/1000 20/20 [==============================] - 1s 29ms/step - loss: 0.2219 - tp: 18790.0000 - fp: 1963.0000 - tn: 18533.0000 - fn: 1674.0000 - accuracy: 0.9112 - precision: 0.9054 - recall: 0.9182 - auc: 0.9720 - prc: 0.9778 - val_loss: 0.2033 - val_tp: 75.0000 - val_fp: 1307.0000 - val_tn: 44181.0000 - val_fn: 6.0000 - val_accuracy: 0.9712 - val_precision: 0.0543 - val_recall: 0.9259 - val_auc: 0.9759 - val_prc: 0.7086 Epoch 13/1000 20/20 [==============================] - 1s 28ms/step - loss: 0.2133 - tp: 18788.0000 - fp: 1833.0000 - tn: 18717.0000 - fn: 1622.0000 - accuracy: 0.9156 - precision: 0.9111 - recall: 0.9205 - auc: 0.9741 - prc: 0.9792 - val_loss: 0.1891 - val_tp: 75.0000 - val_fp: 1243.0000 - val_tn: 44245.0000 - val_fn: 6.0000 - val_accuracy: 0.9726 - val_precision: 0.0569 - val_recall: 0.9259 - val_auc: 0.9761 - val_prc: 0.7107 Epoch 14/1000 20/20 [==============================] - 1s 29ms/step - loss: 0.2056 - tp: 18712.0000 - fp: 1702.0000 - tn: 18942.0000 - fn: 1604.0000 - accuracy: 0.9193 - precision: 0.9166 - recall: 0.9210 - auc: 0.9756 - prc: 0.9801 - val_loss: 0.1766 - val_tp: 75.0000 - val_fp: 1171.0000 - val_tn: 44317.0000 - val_fn: 6.0000 - val_accuracy: 0.9742 - val_precision: 0.0602 - val_recall: 0.9259 - val_auc: 0.9760 - val_prc: 0.7146 Epoch 15/1000 20/20 [==============================] - 1s 28ms/step - loss: 0.1949 - tp: 18902.0000 - fp: 1482.0000 - tn: 19001.0000 - fn: 1575.0000 - accuracy: 0.9254 - precision: 0.9273 - recall: 0.9231 - auc: 0.9782 - prc: 0.9822 - val_loss: 0.1659 - val_tp: 75.0000 - val_fp: 1144.0000 - val_tn: 44344.0000 - val_fn: 6.0000 - val_accuracy: 0.9748 - val_precision: 0.0615 - val_recall: 0.9259 - val_auc: 0.9760 - val_prc: 0.7149 Epoch 16/1000 20/20 [==============================] - 1s 28ms/step - loss: 0.1945 - tp: 18691.0000 - fp: 1533.0000 - tn: 19156.0000 - fn: 1580.0000 - accuracy: 0.9240 - precision: 0.9242 - recall: 0.9221 - auc: 0.9780 - prc: 0.9816 - val_loss: 0.1556 - val_tp: 75.0000 - val_fp: 1078.0000 - val_tn: 44410.0000 - val_fn: 6.0000 - val_accuracy: 0.9762 - val_precision: 0.0650 - val_recall: 0.9259 - val_auc: 0.9757 - val_prc: 0.7169 Epoch 17/1000 20/20 [==============================] - 1s 28ms/step - loss: 0.1848 - tp: 18904.0000 - fp: 1333.0000 - tn: 19156.0000 - fn: 1567.0000 - accuracy: 0.9292 - precision: 0.9341 - recall: 0.9235 - auc: 0.9801 - prc: 0.9834 - val_loss: 0.1474 - val_tp: 75.0000 - val_fp: 1061.0000 - val_tn: 44427.0000 - val_fn: 6.0000 - val_accuracy: 0.9766 - val_precision: 0.0660 - val_recall: 0.9259 - val_auc: 0.9755 - val_prc: 0.7233 Epoch 18/1000 20/20 [==============================] - 1s 28ms/step - loss: 0.1795 - tp: 18892.0000 - fp: 1332.0000 - tn: 19170.0000 - fn: 1566.0000 - accuracy: 0.9292 - precision: 0.9341 - recall: 0.9235 - auc: 0.9810 - prc: 0.9840 - val_loss: 0.1409 - val_tp: 75.0000 - val_fp: 1074.0000 - val_tn: 44414.0000 - val_fn: 6.0000 - val_accuracy: 0.9763 - val_precision: 0.0653 - val_recall: 0.9259 - val_auc: 0.9758 - val_prc: 0.7283 Epoch 19/1000 20/20 [==============================] - 1s 28ms/step - loss: 0.1777 - tp: 19003.0000 - fp: 1336.0000 - tn: 19109.0000 - fn: 1512.0000 - accuracy: 0.9305 - precision: 0.9343 - recall: 0.9263 - auc: 0.9815 - prc: 0.9844 - val_loss: 0.1338 - val_tp: 75.0000 - val_fp: 1035.0000 - val_tn: 44453.0000 - val_fn: 6.0000 - val_accuracy: 0.9772 - val_precision: 0.0676 - val_recall: 0.9259 - val_auc: 0.9756 - val_prc: 0.7286 Epoch 20/1000 20/20 [==============================] - 1s 31ms/step - loss: 0.1729 - tp: 18882.0000 - fp: 1274.0000 - tn: 19325.0000 - fn: 1479.0000 - accuracy: 0.9328 - precision: 0.9368 - recall: 0.9274 - auc: 0.9827 - prc: 0.9849 - val_loss: 0.1269 - val_tp: 75.0000 - val_fp: 997.0000 - val_tn: 44491.0000 - val_fn: 6.0000 - val_accuracy: 0.9780 - val_precision: 0.0700 - val_recall: 0.9259 - val_auc: 0.9757 - val_prc: 0.7233 Epoch 21/1000 20/20 [==============================] - 1s 29ms/step - loss: 0.1660 - tp: 18989.0000 - fp: 1145.0000 - tn: 19319.0000 - fn: 1507.0000 - accuracy: 0.9353 - precision: 0.9431 - recall: 0.9265 - auc: 0.9840 - prc: 0.9862 - val_loss: 0.1218 - val_tp: 75.0000 - val_fp: 995.0000 - val_tn: 44493.0000 - val_fn: 6.0000 - val_accuracy: 0.9780 - val_precision: 0.0701 - val_recall: 0.9259 - val_auc: 0.9756 - val_prc: 0.7267 Epoch 22/1000 20/20 [==============================] - 1s 28ms/step - loss: 0.1626 - tp: 19009.0000 - fp: 1148.0000 - tn: 19349.0000 - fn: 1454.0000 - accuracy: 0.9365 - precision: 0.9430 - recall: 0.9289 - auc: 0.9847 - prc: 0.9866 - val_loss: 0.1173 - val_tp: 75.0000 - val_fp: 996.0000 - val_tn: 44492.0000 - val_fn: 6.0000 - val_accuracy: 0.9780 - val_precision: 0.0700 - val_recall: 0.9259 - val_auc: 0.9755 - val_prc: 0.7190 Epoch 23/1000 20/20 [==============================] - 1s 28ms/step - loss: 0.1578 - tp: 18958.0000 - fp: 1108.0000 - tn: 19419.0000 - fn: 1475.0000 - accuracy: 0.9369 - precision: 0.9448 - recall: 0.9278 - auc: 0.9853 - prc: 0.9871 - val_loss: 0.1134 - val_tp: 75.0000 - val_fp: 1001.0000 - val_tn: 44487.0000 - val_fn: 6.0000 - val_accuracy: 0.9779 - val_precision: 0.0697 - val_recall: 0.9259 - val_auc: 0.9759 - val_prc: 0.7199 Epoch 24/1000 20/20 [==============================] - 1s 30ms/step - loss: 0.1560 - tp: 19078.0000 - fp: 1074.0000 - tn: 19418.0000 - fn: 1390.0000 - accuracy: 0.9398 - precision: 0.9467 - recall: 0.9321 - auc: 0.9862 - prc: 0.9877 - val_loss: 0.1095 - val_tp: 75.0000 - val_fp: 1013.0000 - val_tn: 44475.0000 - val_fn: 6.0000 - val_accuracy: 0.9776 - val_precision: 0.0689 - val_recall: 0.9259 - val_auc: 0.9760 - val_prc: 0.7203 Epoch 25/1000 20/20 [==============================] - 1s 30ms/step - loss: 0.1526 - tp: 19222.0000 - fp: 1068.0000 - tn: 19256.0000 - fn: 1414.0000 - accuracy: 0.9394 - precision: 0.9474 - recall: 0.9315 - auc: 0.9865 - prc: 0.9882 - val_loss: 0.1054 - val_tp: 75.0000 - val_fp: 984.0000 - val_tn: 44504.0000 - val_fn: 6.0000 - val_accuracy: 0.9783 - val_precision: 0.0708 - val_recall: 0.9259 - val_auc: 0.9754 - val_prc: 0.7213 Epoch 26/1000 20/20 [==============================] - 1s 28ms/step - loss: 0.1504 - tp: 18992.0000 - fp: 1073.0000 - tn: 19475.0000 - fn: 1420.0000 - accuracy: 0.9391 - precision: 0.9465 - recall: 0.9304 - auc: 0.9869 - prc: 0.9881 - val_loss: 0.1024 - val_tp: 75.0000 - val_fp: 988.0000 - val_tn: 44500.0000 - val_fn: 6.0000 - val_accuracy: 0.9782 - val_precision: 0.0706 - val_recall: 0.9259 - val_auc: 0.9759 - val_prc: 0.7215 Epoch 27/1000 20/20 [==============================] - 1s 29ms/step - loss: 0.1464 - tp: 19140.0000 - fp: 974.0000 - tn: 19433.0000 - fn: 1413.0000 - accuracy: 0.9417 - precision: 0.9516 - recall: 0.9313 - auc: 0.9877 - prc: 0.9889 - val_loss: 0.1001 - val_tp: 75.0000 - val_fp: 994.0000 - val_tn: 44494.0000 - val_fn: 6.0000 - val_accuracy: 0.9781 - val_precision: 0.0702 - val_recall: 0.9259 - val_auc: 0.9763 - val_prc: 0.7218 Epoch 28/1000 20/20 [==============================] - 1s 28ms/step - loss: 0.1436 - tp: 19103.0000 - fp: 978.0000 - tn: 19526.0000 - fn: 1353.0000 - accuracy: 0.9431 - precision: 0.9513 - recall: 0.9339 - auc: 0.9884 - prc: 0.9894 - val_loss: 0.0974 - val_tp: 75.0000 - val_fp: 986.0000 - val_tn: 44502.0000 - val_fn: 6.0000 - val_accuracy: 0.9782 - val_precision: 0.0707 - val_recall: 0.9259 - val_auc: 0.9757 - val_prc: 0.7216 Epoch 29/1000 18/20 [==========================>...] - ETA: 0s - loss: 0.1445 - tp: 17095.0000 - fp: 886.0000 - tn: 17596.0000 - fn: 1287.0000 - accuracy: 0.9411 - precision: 0.9507 - recall: 0.9300 - auc: 0.9883 - prc: 0.9891Restoring model weights from the end of the best epoch: 19. 20/20 [==============================] - 1s 30ms/step - loss: 0.1440 - tp: 18978.0000 - fp: 976.0000 - tn: 19592.0000 - fn: 1414.0000 - accuracy: 0.9417 - precision: 0.9511 - recall: 0.9307 - auc: 0.9884 - prc: 0.9891 - val_loss: 0.0939 - val_tp: 75.0000 - val_fp: 951.0000 - val_tn: 44537.0000 - val_fn: 6.0000 - val_accuracy: 0.9790 - val_precision: 0.0731 - val_recall: 0.9259 - val_auc: 0.9757 - val_prc: 0.7220 Epoch 29: early stopping
Re-check training history
plot_metrics(resampled_history)
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.13431145250797272 tp : 87.0 fp : 1381.0 tn : 55484.0 fn : 10.0 accuracy : 0.9755802154541016 precision : 0.05926430597901344 recall : 0.8969072103500366 auc : 0.9665377140045166 prc : 0.7062013745307922 Legitimate Transactions Detected (True Negatives): 55484 Legitimate Transactions Incorrectly Detected (False Positives): 1381 Fraudulent Transactions Missed (False Negatives): 10 Fraudulent Transactions Detected (True Positives): 87 Total Fraudulent Transactions: 97
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');
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');
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.