Building a Fraud Prediction Model with EvalML

In this demo, we will build an optimized fraud prediction model using EvalML. To optimize the pipeline, we will set up an objective function to minimize the percentage of total transaction value lost to fraud. At the end of this demo, we also show you how introducing the right objective during the training results in a much better than using a generic machine learning metric like AUC.

[1]:
import evalml
from evalml import AutoMLSearch
from evalml.objectives import FraudCost

Configure “Cost of Fraud”

To optimize the pipelines toward the specific business needs of this model, we can set our own assumptions for the cost of fraud. These parameters are

  • retry_percentage - what percentage of customers will retry a transaction if it is declined?

  • interchange_fee - how much of each successful transaction do you collect?

  • fraud_payout_percentage - the percentage of fraud will you be unable to collect

  • amount_col - the column in the data the represents the transaction amount

Using these parameters, EvalML determines attempt to build a pipeline that will minimize the financial loss due to fraud.

[2]:
fraud_objective = FraudCost(retry_percentage=.5,
                            interchange_fee=.02,
                            fraud_payout_percentage=.75,
                            amount_col='amount')

Search for best pipeline

In order to validate the results of the pipeline creation and optimization process, we will save some of our data as the holdout set.

[3]:
X, y = evalml.demos.load_fraud(n_rows=5000)
             Number of Features
Boolean                       1
Categorical                   6
Numeric                       5

Number of training examples: 5000
Targets
False    86.20%
True     13.80%
Name: fraud, dtype: object

EvalML natively supports one-hot encoding. Here we keep 1 out of the 6 categorical columns to decrease computation time.

[4]:
cols_to_drop = ['datetime', 'expiration_date', 'country', 'region', 'provider']
for col in cols_to_drop:
    X.ww.pop(col)

X_train, X_holdout, y_train, y_holdout = evalml.preprocessing.split_data(X, y, problem_type='binary', test_size=0.2, random_seed=0)

X.ww
[4]:
Physical Type Logical Type Semantic Tag(s)
Column
card_id int64 Integer ['numeric']
store_id int64 Integer ['numeric']
amount int64 Integer ['numeric']
currency category Categorical ['category']
customer_present bool Boolean []
lat float64 Double ['numeric']
lng float64 Double ['numeric']

Because the fraud labels are binary, we will use AutoMLSearch(X_train=X_train, y_train=y_train, problem_type='binary'). When we call .search(), the search for the best pipeline will begin.

[5]:
automl = AutoMLSearch(X_train=X_train, y_train=y_train,
                      problem_type='binary',
                      objective=fraud_objective,
                      additional_objectives=['auc', 'f1', 'precision'],
                      max_batches=1,
                      optimize_thresholds=True)

automl.search()
Generating pipelines to search over...
8 pipelines ready for search.

*****************************
* Beginning pipeline search *
*****************************

Optimizing for Fraud Cost.
Lower score is better.

Using SequentialEngine to train and score pipelines.
Searching up to 1 batches for a total of 9 pipelines.
Allowed model families: random_forest, catboost, extra_trees, xgboost, linear_model, lightgbm, decision_tree

Evaluating Baseline Pipeline: Mode Baseline Binary Classification Pipeline
Mode Baseline Binary Classification Pipeline:
        Starting cross validation
        Finished cross validation - mean Fraud Cost: 0.165

*****************************
* Evaluating Batch Number 1 *
*****************************

Elastic Net Classifier w/ Imputer + One Hot Encoder + SMOTENC Oversampler + Standard Scaler:
        Starting cross validation
        Finished cross validation - mean Fraud Cost: 0.008
Decision Tree Classifier w/ Imputer + One Hot Encoder + SMOTENC Oversampler:
        Starting cross validation
        Finished cross validation - mean Fraud Cost: 0.010
Random Forest Classifier w/ Imputer + One Hot Encoder + SMOTENC Oversampler:
        Starting cross validation
        Finished cross validation - mean Fraud Cost: 0.008
LightGBM Classifier w/ Imputer + One Hot Encoder + SMOTENC Oversampler:
        Starting cross validation
        Finished cross validation - mean Fraud Cost: 0.008
Logistic Regression Classifier w/ Imputer + One Hot Encoder + SMOTENC Oversampler + Standard Scaler:
        Starting cross validation
        Finished cross validation - mean Fraud Cost: 0.008
XGBoost Classifier w/ Imputer + One Hot Encoder + SMOTENC Oversampler:
        Starting cross validation
        Finished cross validation - mean Fraud Cost: 0.008
Extra Trees Classifier w/ Imputer + One Hot Encoder + SMOTENC Oversampler:
        Starting cross validation
        Finished cross validation - mean Fraud Cost: 0.008
CatBoost Classifier w/ Imputer + SMOTENC Oversampler:
        Starting cross validation
        Finished cross validation - mean Fraud Cost: 0.008

Search finished after 00:17
Best pipeline: Elastic Net Classifier w/ Imputer + One Hot Encoder + SMOTENC Oversampler + Standard Scaler
Best pipeline Fraud Cost: 0.007802

View rankings and select pipelines

Once the fitting process is done, we can see all of the pipelines that were searched, ranked by their score on the fraud detection objective we defined.

[6]:
automl.rankings
[6]:
id pipeline_name search_order mean_cv_score standard_deviation_cv_score validation_score percent_better_than_baseline high_variance_cv parameters
0 1 Elastic Net Classifier w/ Imputer + One Hot En... 1 0.007802 0.000039 0.007758 15.698661 False {'Imputer': {'categorical_impute_strategy': 'm...
1 5 Logistic Regression Classifier w/ Imputer + On... 5 0.007802 0.000039 0.007758 15.698661 False {'Imputer': {'categorical_impute_strategy': 'm...
2 3 Random Forest Classifier w/ Imputer + One Hot ... 3 0.007803 0.000040 0.007758 15.698619 False {'Imputer': {'categorical_impute_strategy': 'm...
3 6 XGBoost Classifier w/ Imputer + One Hot Encode... 6 0.007803 0.000040 0.007758 15.698619 False {'Imputer': {'categorical_impute_strategy': 'm...
4 7 Extra Trees Classifier w/ Imputer + One Hot En... 7 0.007803 0.000040 0.007758 15.698619 False {'Imputer': {'categorical_impute_strategy': 'm...
5 8 CatBoost Classifier w/ Imputer + SMOTENC Overs... 8 0.007803 0.000040 0.007758 15.698619 False {'Imputer': {'categorical_impute_strategy': 'm...
6 4 LightGBM Classifier w/ Imputer + One Hot Encod... 4 0.007808 0.000044 0.007758 15.698096 False {'Imputer': {'categorical_impute_strategy': 'm...
7 2 Decision Tree Classifier w/ Imputer + One Hot ... 2 0.010165 0.001163 0.009317 15.462437 False {'Imputer': {'categorical_impute_strategy': 'm...
8 0 Mode Baseline Binary Classification Pipeline 0 0.164789 0.002964 0.168169 0.000000 False {'Baseline Classifier': {'strategy': 'mode'}}

To select the best pipeline we can call automl.best_pipeline.

[7]:
best_pipeline = automl.best_pipeline

Describe pipelines

We can get more details about any pipeline created during the search process, including how it performed on other objective functions, by calling the describe_pipeline method and passing the id of the pipeline of interest.

[8]:
automl.describe_pipeline(automl.rankings.iloc[1]["id"])

*******************************************************************************************************
* Logistic Regression Classifier w/ Imputer + One Hot Encoder + SMOTENC Oversampler + Standard Scaler *
*******************************************************************************************************

Problem Type: binary
Model Family: Linear

Pipeline Steps
==============
1. Imputer
         * categorical_impute_strategy : most_frequent
         * numeric_impute_strategy : mean
         * categorical_fill_value : None
         * numeric_fill_value : None
2. One Hot Encoder
         * top_n : 10
         * features_to_encode : None
         * categories : None
         * drop : if_binary
         * handle_unknown : ignore
         * handle_missing : error
3. SMOTENC Oversampler
         * sampling_ratio : 0.25
         * k_neighbors_default : 5
         * n_jobs : -1
         * sampling_ratio_dict : None
         * categorical_features : [3, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
         * k_neighbors : 5
4. Standard Scaler
5. Logistic Regression Classifier
         * penalty : l2
         * C : 1.0
         * n_jobs : -1
         * multi_class : auto
         * solver : lbfgs

Training
========
Training for binary problems.
Objective to optimize binary classification pipeline thresholds for: <evalml.objectives.fraud_cost.FraudCost object at 0x7f495c54a100>
Total training time (including CV): 3.2 seconds

Cross Validation
----------------
             Fraud Cost   AUC    F1  Precision # Training # Validation
0                 0.008 0.854 0.000      0.000      2,666        1,334
1                 0.008 0.799 0.000      0.000      2,667        1,333
2                 0.008 0.828 0.421      1.000      2,667        1,333
mean              0.008 0.827 0.140      0.333          -            -
std               0.000 0.027 0.243      0.577          -            -
coef of var       0.005 0.033 1.732      1.732          -            -

Evaluate on holdout data

Finally, since the best pipeline is already trained, we evaluate it on the holdout data.

Now, we can score the pipeline on the holdout data using both our fraud cost objective and the AUC (Area under the ROC Curve) objective.

[9]:
best_pipeline.score(X_holdout, y_holdout, objectives=["auc", fraud_objective])
[9]:
OrderedDict([('AUC', 0.8097111537038906),
             ('Fraud Cost', 0.007834043025742435)])

Why optimize for a problem-specific objective?

To demonstrate the importance of optimizing for the right objective, let’s search for another pipeline using AUC, a common machine learning metric. After that, we will score the holdout data using the fraud cost objective to see how the best pipelines compare.

[10]:
automl_auc = AutoMLSearch(X_train=X_train, y_train=y_train,
                          problem_type='binary',
                          objective='auc',
                          additional_objectives=['f1', 'precision'],
                          max_batches=1,
                          optimize_thresholds=True)

automl_auc.search()
Generating pipelines to search over...
8 pipelines ready for search.

*****************************
* Beginning pipeline search *
*****************************

Optimizing for AUC.
Greater score is better.

Using SequentialEngine to train and score pipelines.
Searching up to 1 batches for a total of 9 pipelines.
Allowed model families: random_forest, catboost, extra_trees, xgboost, linear_model, lightgbm, decision_tree

Evaluating Baseline Pipeline: Mode Baseline Binary Classification Pipeline
Mode Baseline Binary Classification Pipeline:
        Starting cross validation
        Finished cross validation - mean AUC: 0.500

*****************************
* Evaluating Batch Number 1 *
*****************************

Elastic Net Classifier w/ Imputer + One Hot Encoder + SMOTENC Oversampler + Standard Scaler:
        Starting cross validation
        Finished cross validation - mean AUC: 0.827
Decision Tree Classifier w/ Imputer + One Hot Encoder + SMOTENC Oversampler:
        Starting cross validation
        Finished cross validation - mean AUC: 0.815
Random Forest Classifier w/ Imputer + One Hot Encoder + SMOTENC Oversampler:
        Starting cross validation
        Finished cross validation - mean AUC: 0.853
LightGBM Classifier w/ Imputer + One Hot Encoder + SMOTENC Oversampler:
        Starting cross validation
        Finished cross validation - mean AUC: 0.846
Logistic Regression Classifier w/ Imputer + One Hot Encoder + SMOTENC Oversampler + Standard Scaler:
        Starting cross validation
        Finished cross validation - mean AUC: 0.827
XGBoost Classifier w/ Imputer + One Hot Encoder + SMOTENC Oversampler:
        Starting cross validation
        Finished cross validation - mean AUC: 0.854
Extra Trees Classifier w/ Imputer + One Hot Encoder + SMOTENC Oversampler:
        Starting cross validation
        Finished cross validation - mean AUC: 0.845
CatBoost Classifier w/ Imputer + SMOTENC Oversampler:
        Starting cross validation
        Finished cross validation - mean AUC: 0.846

Search finished after 00:15
Best pipeline: XGBoost Classifier w/ Imputer + One Hot Encoder + SMOTENC Oversampler
Best pipeline AUC: 0.853977

Like before, we can look at the rankings of all of the pipelines searched and pick the best pipeline.

[11]:
automl_auc.rankings
[11]:
id pipeline_name search_order mean_cv_score standard_deviation_cv_score validation_score percent_better_than_baseline high_variance_cv parameters
0 6 XGBoost Classifier w/ Imputer + One Hot Encode... 6 0.853977 0.009406 0.861938 35.397731 False {'Imputer': {'categorical_impute_strategy': 'm...
1 3 Random Forest Classifier w/ Imputer + One Hot ... 3 0.853164 0.010011 0.863894 35.316397 False {'Imputer': {'categorical_impute_strategy': 'm...
2 8 CatBoost Classifier w/ Imputer + SMOTENC Overs... 8 0.846309 0.015450 0.853370 34.630851 False {'Imputer': {'categorical_impute_strategy': 'm...
3 4 LightGBM Classifier w/ Imputer + One Hot Encod... 4 0.845618 0.004352 0.850487 34.561798 False {'Imputer': {'categorical_impute_strategy': 'm...
4 7 Extra Trees Classifier w/ Imputer + One Hot En... 7 0.845031 0.008247 0.853294 34.503064 False {'Imputer': {'categorical_impute_strategy': 'm...
5 1 Elastic Net Classifier w/ Imputer + One Hot En... 1 0.827102 0.027002 0.853667 32.710221 False {'Imputer': {'categorical_impute_strategy': 'm...
6 5 Logistic Regression Classifier w/ Imputer + On... 5 0.826675 0.027444 0.853544 32.667497 False {'Imputer': {'categorical_impute_strategy': 'm...
7 2 Decision Tree Classifier w/ Imputer + One Hot ... 2 0.814606 0.008220 0.806586 31.460578 False {'Imputer': {'categorical_impute_strategy': 'm...
8 0 Mode Baseline Binary Classification Pipeline 0 0.500000 0.000000 0.500000 0.000000 False {'Baseline Classifier': {'strategy': 'mode'}}
[12]:
best_pipeline_auc = automl_auc.best_pipeline
[13]:
# get the fraud score on holdout data
best_pipeline_auc.score(X_holdout, y_holdout,  objectives=["auc", fraud_objective])
[13]:
OrderedDict([('AUC', 0.8540216550657386), ('Fraud Cost', 0.00783500768341736)])
[14]:
# fraud score on fraud optimized again
best_pipeline.score(X_holdout, y_holdout, objectives=["auc", fraud_objective])
[14]:
OrderedDict([('AUC', 0.8097111537038906),
             ('Fraud Cost', 0.007834043025742435)])

When we optimize for AUC, we can see that the AUC score from this pipeline performs better compared to the AUC score from the pipeline optimized for fraud cost; however, the losses due to fraud are a much larger percentage of the total transaction amount when optimized for AUC and much smaller when optimized for fraud cost. As a result, we lose a noticable percentage of the total transaction amount by not optimizing for fraud cost specifically.

Optimizing for AUC does not take into account the user-specified retry_percentage, interchange_fee, fraud_payout_percentage values, which could explain the decrease in fraud performance. Thus, the best pipelines may produce the highest AUC but may not actually reduce the amount loss due to your specific type fraud.

This example highlights how performance in the real world can diverge greatly from machine learning metrics.