We'll be starting this series with an analysis of a credit card fraud dataset. We're going to use this as an example of one of the most common enterprise use cases for machine learning: a learning to rank problem. In these cases we are looking to prioritize a set of records for escalation into some form of a review process. In this particular case, we'll be looking to identify payment records that might be fraudulent. In a company such as Visa or Stripe, records that score highly might be automatically blocked, while those with mid-level scores would be sent to a domain expert such as a fraud analyst for a decision.

A key piece of this is the model that identifies the records of interest. Here we will look at several options for models that generate a probability measure or numeric score for each record. The advantage of this approach is that:

- We can rank order the list to ensure that the highest value records are examined first, and
- we can work our way as deeply into the list as we like, deciding to allocate more resources if we want to go deeper or less if we're capturing enough value with those records at the top.

You can download the dataset we're using from Kaggle: https://www.kaggle.com/dalpozz/creditcardfraud

Python packages we'll be making extensive use of:

- pandas
- numpy
- matplotlib
- sklearn
- keras/tensorflow

```
%matplotlib inline
import sys
sys.path.append('../')
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
```

We're going to start with Logistic Regression Model. These models are the workhorses of the enterprise because they are fast to fit, give an interpretable rank ordering of coefficients, and are easily deployed in production settings. For those that aren't familiar, please see http://www.stat.cmu.edu/~cshalizi/uADA/12/lectures/ch12.pdf for an in depth treatment.

This is a method "stolen" from statistics. The machine learning flavor is slightly different, in that we don't try to generate an inference or causal interpretation, but are looking solely for predictive performance.

```
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
from sklearn.model_selection import train_test_split
```

```
# Load the data. Don't forget to change this variable to your dataset location.
credit_dataset_location = '/Users/ianblumenfeld/data/credit_card_fraud/creditcard.csv'
data = pd.read_csv(credit_dataset_location)
```

```
# We're going to make a copy of the dataset to work with. This is because we'll be cutting this dataset in
# a few different ways and I like to keep a clean copy of the initial one around. Let's also take a quick look at
# what's inside.
df = data.copy()
df.describe()
```

Looking at the above you can see a couple of things:

- There are almost 300K records.
- Most of the transactions are valid. The fraud rate corresponds to the mean of the Class column, meaning that only 0.17% of the records are faudulent, or 492 total records. This situation (where one class is much more heavily represented in the data) is referred to as
**unbalanced**. - Other than the time column, the other column labels are uninterpretable. This is because this particular dataset is has been dimensionally reduced through principal component analysis (PCA) before it was released to us. We are seeing only the 28 most significant components rather than the real-space features. This is actually the reason I chose this dataset- as the feature set we have available isn't in real-space, we don't have any temptation to try to interpret the coefficients and can focus entirely on the predictive value of the models.
- The features are all pretty much on the same order, so we don't really need to rescale them.

```
# Now we'll do the slicing. We'll be grabbing the last 75K points to reserve for validation. These correspond to
# the later time period, so they have the additional benefit of being uncorrelated in time with the data we
# use to train the model.
df['validation_set'] = df.index>df.shape[0]-75000
df_val = df[df.validation_set==True].copy()
# The earlier data we'll use for model training.
df = df[df.validation_set==False].copy()
```

```
# For the purposes of this tutorial, we're going to exlude the Time column. We'll come back to this at a later date.
# For those unfamiliar we're using a list comprehension to make the list of feature columns we'd like.
cols = [col for col in df.columns if col not in ['Class', 'validation_set', 'Time']]
# Create our feature and outcomes dataframe for model training.
features = df[cols]
outcomes = df[['Class']]
# Create our feature and outcomes dataframe for model validation.
features_val = df_val[cols]
outcomes_val = df_val[['Class']]
```

```
# Now we need to generate a random split of the training data for training of the model. This will help prevent us
# from overfitting.
x_train, x_test, y_train, y_test = train_test_split(features, outcomes, test_size=0.33, random_state=15)
```

Now we'll fit the model. We'll use an L1 regularization penalty here. This forces the parameter values to take on large values or be zero'ed out. This is a way to ensure that only those features with a strong contribution to the outcome prediction are used. This is a good first order approach to see if a decent model is even achievable. It isn't usually the final model shipped though, because L1 can cost you some predictive power. Most folks will use it as a feature selection step, then drop the resulting feature choices into another algorithm (eg. Boosted Tree or LR with L2 regularization) or they'll use an elastic net (LR with L1 and L2 regularization terms). Because those add more hyperparameters to tune, I don't usually go there at the outset.

You can read more about regularization here: https://en.wikipedia.org/wiki/Regularization_(mathematics)

```
# The 'ravel' term here is just transforming the outcome data from n by 1 to 1 by n, which the fit methods prefer.
model = LogisticRegression(penalty='l1',).fit(x_train, np.ravel(y_train))
```

For evaluating our models we'll be using the Area under the Curve (AUC) of Receiver Operator Characteristic (ROC) Curve. This is the main metric we'll use for the learning to rank problem. There are a number of reasons this metric is most appropriate for these kinds of models:

- For unbalanced datasets like this naive measures like accuracy work very poorly. This is because we can generate excellent accuracy by guessing "Not Fraud" for every record.
- There is a direct interpretation for this value- roughly speaking it represents the probability that when we draw two samples at random from the data, that the one with the higher score actually is of higher "risk".

Because of this, the AUC metric is the workhorse of enterprise ML. It directly measures the performance of the most commonly used data product.

```
auc_train = metrics.roc_auc_score(y_train,model.predict_proba(x_train.values)[:,1])
auc_test = metrics.roc_auc_score(y_test.values,model.predict_proba(x_test.values)[:,1])
auc_val = metrics.roc_auc_score(outcomes_val,model.predict_proba(features_val.values)[:,1])
print('AUC for the training set: %f' % auc_train)
print('AUC for the test set: %f' % auc_test)
print('AUC for the validation set: %f' % auc_val)
```

The auc's here show that this model is pretty effective at rank ordering our records. We would also want to examine the ROC curves themselves, which will give us a visual representation of the performance of our model.

```
# get the roc curves values
roc_train = metrics.roc_curve(y_train,model.predict_proba(x_train)[:,1])
roc_test = metrics.roc_curve(y_test,model.predict_proba(x_test)[:,1])
roc_val = metrics.roc_curve(outcomes_val,model.predict_proba(features_val)[:,1])
```

```
plt.plot(roc_train[0], roc_train[1], 'b', label='Training')
plt.plot(roc_test[0], roc_test[1], 'g', label='Testing')
plt.plot(roc_val[0], roc_val[1], 'k', label='Validation')
plt.plot([0,1], [0,1], 'r', label='x=y')
plt.legend()
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
```

The curve here shows us visually that our rank ordering will be accurate. Moreover, if we decided that we wanted a specific prediction, we can see that we could easily set a threshold which would yield greater than an 80% true positive rate at the cost of very few false positives. We could also go higher depending on whether our expected losses justify the extra expenditure. (Remember that the actual fraud rate is very low- a false positive rate of 1% means we will flag 2800 records in our dataset that aren't fraudulent that we'll need to sort through by manual review. So the expected loss must be high.)

Given this, can we do better? To do so we need to enable more complex interactions between the variables than the simple linear ones in our logistic regression. We could do this by combining and crossing terms together, but a better option is to build a deep network and allow it to figure out the relationships for itself. We've already learned not to try to interpret the model parameters, so it's only one step further to move to a black box model.

We'll be using Keras as our programming framework of choice, with Tensorflow as the backend. Why not raw tensorflow? In enterprise contexts we care most about 3 things:

- Models that are accurate enough to drive value.
- Getting end to end as quickly as possible.
- Iterating as quickly as possible.

This makes high level APIs very valuable for development purposes (the same reason that frameworks like Ruby On Rails are so popular for SaaS startups). We should be working with lower level libraries only if we find ourselves with a niche use case that really requires the extra flexibility, such that the extra developer time is worthwhile. We will also want to pick setting that allow us to fit and evaluate model performance quickly. This means limiting ourselves to moderately sized networks and fast fits (this tends to constrain the hyperparameter exploration we might do).

```
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout
from keras.regularizers import l1_l2
```

```
# We're going to upweight the fraudulent data points. This will make it easier for the more general optimization
# techniques needed to fit a deep network to latch onto the results. This means we won't get a straight up probability
# measure back, but the rank ordering should still be preserved.
class_weight = {0:1, 1:499}
```

One of the interesting things to me about deep models is that they can be thought of as deconstructed Machine Learning models. A machine learning model basically consists of three parts:

- A calculation engine. In the LR model above, this is logistic function.
- An optimization method, also known as a learning algorithm. The default sklearn logistic regression uses the LIBLINEAR library, which implements the Trust Region Newton Method (see http://fa.bianp.net/blog/2013/numerical-optimizers-for-logistic-regression/ for more details).
- A cost function, also known as a loss function, which is the thing to be minimized by the optimization method. For the LR model we set up, this is least square + the L1 regularization term.

In deep models we need to declare all of these explicitly, which we'll do below.

```
# First we set up the model.
# There are several schools of thought on the number of units in a given layer.
# I tend to use one of two rules of thumb:
# - ~0.5x the number of features or
# - ~2x the number of features in the input data.
num_units=50
# We'll be using a combination of l1 and l2 regularization. While random dropout is almost always valuable,
# regularization in the layers doesn't always help. It's imperative to check it. In this case regularization
# does help.
reg = l1_l2()
# Our model type here will be a set of connected layers. But this is really Keras boilerplate.
deep_model = Sequential()
# We'll be using rectified linear units (relus) for activation with a 20% dropout.
deep_model.add(Dense(num_units, input_dim=x_train.shape[1],
kernel_regularizer=reg,
activation='relu'))
deep_model.add(Dropout(0.2))
# Layer 2
deep_model.add(Dense(num_units,
kernel_regularizer=reg,
activation='relu'))
deep_model.add(Dropout(0.2))
# Layer 3
deep_model.add(Dense(num_units,
kernel_regularizer=reg,
activation='relu'))
deep_model.add(Dropout(0.2))
# Output!
deep_model.add(Dense(1, activation='sigmoid'))
```

```
# We'll use adaptive gradient descent (adagrad) for optimization, because it tends to produce better results with
# fewer epochs. We'll use binary cross entropy, since this is a binary classification problem- a record is either
# fraudulent or not.
deep_model.compile(optimizer='adagrad', loss='binary_crossentropy', metrics=['accuracy'])
```

```
deep_model.fit(
x_train.values,
y_train.values,
batch_size=1024,
epochs=100,
verbose=0,
class_weight=class_weight,
validation_data=(x_test.values, y_test.values))
```

```
deep_auc_train = metrics.roc_auc_score(y_train, deep_model.predict_proba(x_train.values))
deep_auc_test = metrics.roc_auc_score(y_test.values, deep_model.predict_proba(x_test.values))
deep_auc_val = metrics.roc_auc_score(outcomes_val.values, deep_model.predict_proba(features_val.values))
print()
print('AUC for the training set: %f' % deep_auc_train)
print('AUC for the test set: %f' % deep_auc_test)
print('AUC for the validation set: %f' % deep_auc_val)
```

The deep model does ~1% better than the LR model. Because of the low event rate this can be quite meaningful, although we would also need the value captured to be commensurate. Before we wrap, let's look at the visual.

```
deep_roc_train = metrics.roc_curve(y_train.values,deep_model.predict_proba(x_train.values))
deep_roc_test = metrics.roc_curve(y_test.values,deep_model.predict_proba(x_test.values))
deep_roc_val = metrics.roc_curve(outcomes_val.values,deep_model.predict_proba(features_val.values))
```

```
plt.plot(deep_roc_train[0], deep_roc_train[1], 'b', label='Training')
plt.plot(deep_roc_test[0], deep_roc_test[1], 'g', label='Testing')
plt.plot(deep_roc_val[0], deep_roc_val[1], 'k', label='Validation')
plt.plot([0,1], [0,1], 'r', label='x=y')
plt.legend()
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
```

And we can compare the validation curves for the two models directly:

```
plt.plot(deep_roc_val[0], deep_roc_val[1], 'b', label='Deep Model')
plt.plot(roc_val[0], roc_val[1], 'g', label='Logisitic Regression')
plt.plot([0,1], [0,1], 'r', label='x=y')
plt.legend()
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
```

So there you have it. We've completed one use case, one commonly used in products and payments technology companies to identify fraudulent transactions. But this use case touches on one of the core issues we need to address in enterprise contexts: **In real world data sets, truthiness is squishy.**

What I mean by this is that in datasets like MNIST, there is a clear True/False condition in the training data- either an image is a 3 or it is not. Even with the labeled categorical data we still can't be completely sure of the True/False condition. A human might have mislabeled it, or the system determining the label could have been wrong. Moreover, in some cases (hospital admissions being a canonical one), even having a True label isn't quite accurate because having received that label is probabilistic. What this leads to is that the hardest part isn't the fitting of the models, or the deployment of the product (although there are parts of this that are annoyingly difficult), but rather the formulation of the problem. Until next time.