Building a Lead Scoring Model with EvalML#
In this demo, we will build an optimized lead scoring model using EvalML. To optimize the pipeline, we will set up an objective function to maximize the revenue generated with true positives while taking into account the cost of false positives. At the end of this demo, we also show you how introducing the right objective during the training is significantly better than using a generic machine learning metric like AUC.
[1]:
import evalml
from evalml import AutoMLSearch
from evalml.objectives import LeadScoring
/home/docs/checkouts/readthedocs.org/user_builds/feature-labs-inc-evalml/envs/v0.77.0/lib/python3.8/site-packages/shap/utils/_clustering.py:35: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.
def _pt_shuffle_rec(i, indexes, index_mask, partition_tree, M, pos):
/home/docs/checkouts/readthedocs.org/user_builds/feature-labs-inc-evalml/envs/v0.77.0/lib/python3.8/site-packages/shap/utils/_clustering.py:54: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.
def delta_minimization_order(all_masks, max_swap_size=100, num_passes=2):
/home/docs/checkouts/readthedocs.org/user_builds/feature-labs-inc-evalml/envs/v0.77.0/lib/python3.8/site-packages/shap/utils/_clustering.py:63: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.
def _reverse_window(order, start, length):
/home/docs/checkouts/readthedocs.org/user_builds/feature-labs-inc-evalml/envs/v0.77.0/lib/python3.8/site-packages/shap/utils/_clustering.py:69: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.
def _reverse_window_score_gain(masks, order, start, length):
/home/docs/checkouts/readthedocs.org/user_builds/feature-labs-inc-evalml/envs/v0.77.0/lib/python3.8/site-packages/shap/utils/_clustering.py:77: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.
def _mask_delta_score(m1, m2):
/home/docs/checkouts/readthedocs.org/user_builds/feature-labs-inc-evalml/envs/v0.77.0/lib/python3.8/site-packages/shap/links.py:5: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.
def identity(x):
/home/docs/checkouts/readthedocs.org/user_builds/feature-labs-inc-evalml/envs/v0.77.0/lib/python3.8/site-packages/shap/links.py:10: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.
def _identity_inverse(x):
/home/docs/checkouts/readthedocs.org/user_builds/feature-labs-inc-evalml/envs/v0.77.0/lib/python3.8/site-packages/shap/links.py:15: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.
def logit(x):
/home/docs/checkouts/readthedocs.org/user_builds/feature-labs-inc-evalml/envs/v0.77.0/lib/python3.8/site-packages/shap/links.py:20: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.
def _logit_inverse(x):
/home/docs/checkouts/readthedocs.org/user_builds/feature-labs-inc-evalml/envs/v0.77.0/lib/python3.8/site-packages/shap/utils/_masked_model.py:363: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.
def _build_fixed_single_output(averaged_outs, last_outs, outputs, batch_positions, varying_rows, num_varying_rows, link, linearizing_weights):
/home/docs/checkouts/readthedocs.org/user_builds/feature-labs-inc-evalml/envs/v0.77.0/lib/python3.8/site-packages/shap/utils/_masked_model.py:385: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.
def _build_fixed_multi_output(averaged_outs, last_outs, outputs, batch_positions, varying_rows, num_varying_rows, link, linearizing_weights):
/home/docs/checkouts/readthedocs.org/user_builds/feature-labs-inc-evalml/envs/v0.77.0/lib/python3.8/site-packages/shap/utils/_masked_model.py:428: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.
def _init_masks(cluster_matrix, M, indices_row_pos, indptr):
/home/docs/checkouts/readthedocs.org/user_builds/feature-labs-inc-evalml/envs/v0.77.0/lib/python3.8/site-packages/shap/utils/_masked_model.py:439: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.
def _rec_fill_masks(cluster_matrix, indices_row_pos, indptr, indices, M, ind):
/home/docs/checkouts/readthedocs.org/user_builds/feature-labs-inc-evalml/envs/v0.77.0/lib/python3.8/site-packages/shap/maskers/_tabular.py:186: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.
def _single_delta_mask(dind, masked_inputs, last_mask, data, x, noop_code):
/home/docs/checkouts/readthedocs.org/user_builds/feature-labs-inc-evalml/envs/v0.77.0/lib/python3.8/site-packages/shap/maskers/_tabular.py:197: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.
def _delta_masking(masks, x, curr_delta_inds, varying_rows_out,
/home/docs/checkouts/readthedocs.org/user_builds/feature-labs-inc-evalml/envs/v0.77.0/lib/python3.8/site-packages/shap/maskers/_image.py:175: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.
def _jit_build_partition_tree(xmin, xmax, ymin, ymax, zmin, zmax, total_ywidth, total_zwidth, M, clustering, q):
/home/docs/checkouts/readthedocs.org/user_builds/feature-labs-inc-evalml/envs/v0.77.0/lib/python3.8/site-packages/shap/explainers/_partition.py:676: NumbaDeprecationWarning: The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.
def lower_credit(i, value, M, values, clustering):
The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.
The 'nopython' keyword argument was not supplied to the 'numba.jit' decorator. The implicit default value for this argument is currently False, but it will be changed to True in Numba 0.59.0. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.
Configure LeadScoring#
To optimize the pipelines toward the specific business needs of this model, you can set your own assumptions for how much value is gained through true positives and the cost associated with false positives. These parameters are
true_positive
- dollar amount to be gained with a successful leadfalse_positive
- dollar amount to be lost with an unsuccessful lead
Using these parameters, EvalML builds a pileline that will maximize the amount of revenue per lead generated.
[2]:
lead_scoring_objective = LeadScoring(true_positives=25, false_positives=-5)
Dataset#
We will be utilizing a dataset detailing a customer’s job, country, state, zip, online action, the dollar amount of that action and whether they were a successful lead.
[3]:
from urllib.request import urlopen
import pandas as pd
import woodwork as ww
customers_data = urlopen(
"https://featurelabs-static.s3.amazonaws.com/lead_scoring_ml_apps/customers.csv"
)
interactions_data = urlopen(
"https://featurelabs-static.s3.amazonaws.com/lead_scoring_ml_apps/interactions.csv"
)
leads_data = urlopen(
"https://featurelabs-static.s3.amazonaws.com/lead_scoring_ml_apps/previous_leads.csv"
)
customers = pd.read_csv(customers_data)
interactions = pd.read_csv(interactions_data)
leads = pd.read_csv(leads_data)
X = customers.merge(interactions, on="customer_id").merge(leads, on="customer_id")
y = X["label"]
X = X.drop(
[
"customer_id",
"date_registered",
"birthday",
"phone",
"email",
"owner",
"company",
"id",
"time_x",
"session",
"referrer",
"time_y",
"label",
"country",
],
axis=1,
)
display(X.head())
job | state | zip | action | amount | |
---|---|---|---|---|---|
0 | Engineer, mining | NY | 60091.0 | page_view | NaN |
1 | Psychologist, forensic | CA | NaN | purchase | 135.23 |
2 | Psychologist, forensic | CA | NaN | page_view | NaN |
3 | Air cabin crew | NaN | 60091.0 | download | NaN |
4 | Air cabin crew | NaN | 60091.0 | page_view | NaN |
We will convert our data into Woodwork data structures. Doing so enables us to have more control over the types passed to and inferred by AutoML.
[4]:
X.ww.init(semantic_tags={"job": "category"}, logical_types={"job": "Categorical"})
y = ww.init_series(y)
X.ww
[4]:
Physical Type | Logical Type | Semantic Tag(s) | |
---|---|---|---|
Column | |||
job | category | Categorical | ['category'] |
state | category | Categorical | ['category'] |
zip | Int64 | IntegerNullable | ['numeric'] |
action | category | Categorical | ['category'] |
amount | float64 | Double | ['numeric'] |
Search for the best pipeline#
In order to validate the results of the pipeline creation and optimization process, we will save some of our data as a holdout set.
EvalML natively supports one-hot encoding and imputation so the above NaN
and categorical values will be taken care of.
[5]:
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
[5]:
Physical Type | Logical Type | Semantic Tag(s) | |
---|---|---|---|
Column | |||
job | category | Categorical | ['category'] |
state | category | Categorical | ['category'] |
zip | Int64 | IntegerNullable | ['numeric'] |
action | category | Categorical | ['category'] |
amount | float64 | Double | ['numeric'] |
Because the lead scoring labels are binary, we will use set the problem type to “binary”. When we call .search()
, the search for the best pipeline will begin.
[6]:
automl = AutoMLSearch(
X_train=X_train,
y_train=y_train,
problem_type="binary",
objective=lead_scoring_objective,
additional_objectives=["auc"],
allowed_model_families=["catboost", "random_forest", "linear_model"],
max_batches=3,
verbose=True,
)
automl.search(interactive_plot=False)
AutoMLSearch will use mean CV score to rank pipelines.
*****************************
* Beginning pipeline search *
*****************************
Optimizing for Lead Scoring.
Greater score is better.
Using SequentialEngine to train and score pipelines.
Searching up to 3 batches for a total of None pipelines.
Allowed model families:
Evaluating Baseline Pipeline: Mode Baseline Binary Classification Pipeline
Mode Baseline Binary Classification Pipeline:
Starting cross validation
Finished cross validation - mean Lead Scoring: 0.000
*****************************
* Evaluating Batch Number 1 *
*****************************
Random Forest Classifier w/ Label Encoder + Imputer + One Hot Encoder + Oversampler:
Starting cross validation
Finished cross validation - mean Lead Scoring: 0.023
*****************************
* Evaluating Batch Number 2 *
*****************************
Random Forest Classifier w/ Label Encoder + Imputer + One Hot Encoder + Oversampler + RF Classifier Select From Model:
Starting cross validation
Finished cross validation - mean Lead Scoring: 0.017
*****************************
* Evaluating Batch Number 3 *
*****************************
Elastic Net Classifier w/ Label Encoder + Select Columns By Type Transformer + Label Encoder + Imputer + Standard Scaler + Select Columns Transformer + Select Columns Transformer + Label Encoder + Imputer + One Hot Encoder + Standard Scaler + Oversampler:
Starting cross validation
Finished cross validation - mean Lead Scoring: 0.012
CatBoost Classifier w/ Label Encoder + Select Columns By Type Transformer + Label Encoder + Imputer + Select Columns Transformer + Select Columns Transformer + Label Encoder + Imputer + Oversampler:
Starting cross validation
Finished cross validation - mean Lead Scoring: 0.505
Logistic Regression Classifier w/ Label Encoder + Select Columns By Type Transformer + Label Encoder + Imputer + Standard Scaler + Select Columns Transformer + Select Columns Transformer + Label Encoder + Imputer + One Hot Encoder + Standard Scaler + Oversampler:
Starting cross validation
Finished cross validation - mean Lead Scoring: 0.019
Search finished after 17.52 seconds
Best pipeline: CatBoost Classifier w/ Label Encoder + Select Columns By Type Transformer + Label Encoder + Imputer + Select Columns Transformer + Select Columns Transformer + Label Encoder + Imputer + Oversampler
Best pipeline Lead Scoring: 0.505483
[6]:
{1: {'Random Forest Classifier w/ Label Encoder + Imputer + One Hot Encoder + Oversampler': 3.3063879013061523,
'Total time of batch': 3.430328369140625},
2: {'Random Forest Classifier w/ Label Encoder + Imputer + One Hot Encoder + Oversampler + RF Classifier Select From Model': 3.668225049972534,
'Total time of batch': 3.7947194576263428},
3: {'Elastic Net Classifier w/ Label Encoder + Select Columns By Type Transformer + Label Encoder + Imputer + Standard Scaler + Select Columns Transformer + Select Columns Transformer + Label Encoder + Imputer + One Hot Encoder + Standard Scaler + Oversampler': 3.214024782180786,
'CatBoost Classifier w/ Label Encoder + Select Columns By Type Transformer + Label Encoder + Imputer + Select Columns Transformer + Select Columns Transformer + Label Encoder + Imputer + Oversampler': 1.370504379272461,
'Logistic Regression Classifier w/ Label Encoder + Select Columns By Type Transformer + Label Encoder + Imputer + Standard Scaler + Select Columns Transformer + Select Columns Transformer + Label Encoder + Imputer + One Hot Encoder + Standard Scaler + Oversampler': 4.562978982925415,
'Total time of batch': 9.597259044647217}}
View rankings and select pipeline#
Once the fitting process is done, we can see all of the pipelines that were searched, ranked by their score on the lead scoring objective we defined.
[7]:
automl.rankings
[7]:
id | pipeline_name | search_order | ranking_score | mean_cv_score | standard_deviation_cv_score | percent_better_than_baseline | high_variance_cv | parameters | |
---|---|---|---|---|---|---|---|---|---|
0 | 4 | CatBoost Classifier w/ Label Encoder + Select ... | 4 | 0.505483 | 0.505483 | 0.015122 | inf | False | {'Label Encoder': {'positive_label': None}, 'N... |
1 | 1 | Random Forest Classifier w/ Label Encoder + Im... | 1 | 0.022581 | 0.022581 | 0.039111 | inf | False | {'Label Encoder': {'positive_label': None}, 'I... |
2 | 5 | Logistic Regression Classifier w/ Label Encode... | 5 | 0.019360 | 0.019360 | 0.032740 | inf | False | {'Label Encoder': {'positive_label': None}, 'N... |
3 | 2 | Random Forest Classifier w/ Label Encoder + Im... | 2 | 0.017204 | 0.017204 | 0.029799 | inf | False | {'Label Encoder': {'positive_label': None}, 'I... |
4 | 3 | Elastic Net Classifier w/ Label Encoder + Sele... | 3 | 0.011828 | 0.011828 | 0.035386 | inf | False | {'Label Encoder': {'positive_label': None}, 'N... |
5 | 0 | Mode Baseline Binary Classification Pipeline | 0 | 0.000000 | 0.000000 | 0.000000 | 0.0 | False | {'Label Encoder': {'positive_label': None}, 'B... |
To select the best pipeline we can call automl.best_pipeline
.
[8]:
best_pipeline = automl.best_pipeline
Describe pipeline#
You can get more details about any pipeline, including how it performed on other objective functions by calling .describe_pipeline()
and specifying the id
of the pipeline.
[9]:
automl.describe_pipeline(automl.rankings.iloc[0]["id"])
*********************************************************************************************************************************************************************************************************
* CatBoost Classifier w/ Label Encoder + Select Columns By Type Transformer + Label Encoder + Imputer + Select Columns Transformer + Select Columns Transformer + Label Encoder + Imputer + Oversampler *
*********************************************************************************************************************************************************************************************************
Problem Type: binary
Model Family: CatBoost
Pipeline Steps
==============
1. Label Encoder
* positive_label : None
2. Select Columns By Type Transformer
* column_types : ['category', 'EmailAddress', 'URL']
* exclude : True
3. Label Encoder
* positive_label : None
4. Imputer
* categorical_impute_strategy : most_frequent
* numeric_impute_strategy : mean
* boolean_impute_strategy : most_frequent
* categorical_fill_value : None
* numeric_fill_value : None
* boolean_fill_value : None
5. Select Columns Transformer
* columns : ['zip', 'amount']
6. Select Columns Transformer
* columns : ['job', 'state', 'action']
7. Label Encoder
* positive_label : None
8. Imputer
* categorical_impute_strategy : most_frequent
* numeric_impute_strategy : mean
* boolean_impute_strategy : most_frequent
* categorical_fill_value : None
* numeric_fill_value : None
* boolean_fill_value : None
9. Oversampler
* sampling_ratio : 0.25
* k_neighbors_default : 5
* n_jobs : -1
* sampling_ratio_dict : None
* categorical_features : [2, 3, 4]
* k_neighbors : 5
10. CatBoost Classifier
* n_estimators : 10
* eta : 0.03
* max_depth : 6
* bootstrap_type : None
* silent : True
* allow_writing_files : False
* n_jobs : -1
Training
========
Training for binary problems.
Objective to optimize binary classification pipeline thresholds for: <evalml.objectives.lead_scoring.LeadScoring object at 0x7f6d4c4dc370>
Total training time (including CV): 1.4 seconds
Cross Validation
----------------
Lead Scoring AUC # Training # Validation
0 0.523 0.879 3,099 1,550
1 0.500 0.868 3,099 1,550
2 0.494 0.897 3,100 1,549
mean 0.505 0.881 - -
std 0.015 0.014 - -
coef of var 0.030 0.016 - -
Evaluate on hold out#
Finally, since the best pipeline was trained on all of the training data, we evaluate it on the holdout dataset.
[10]:
best_pipeline_score = best_pipeline.score(
X_holdout, y_holdout, objectives=["auc", lead_scoring_objective]
)
best_pipeline_score
[10]:
OrderedDict([('AUC', 0.873904502870958),
('Lead Scoring', 0.37403267411865865)])
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 lead scoring objective to see how the best pipelines compare.
[11]:
automl_auc = evalml.AutoMLSearch(
X_train=X_train,
y_train=y_train,
problem_type="binary",
objective="auc",
additional_objectives=[lead_scoring_objective],
allowed_model_families=["catboost", "random_forest", "linear_model"],
max_batches=3,
verbose=True,
)
automl_auc.search(interactive_plot=False)
AutoMLSearch will use mean CV score to rank pipelines.
*****************************
* Beginning pipeline search *
*****************************
Optimizing for AUC.
Greater score is better.
Using SequentialEngine to train and score pipelines.
Searching up to 3 batches for a total of None pipelines.
Allowed model families:
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 *
*****************************
Random Forest Classifier w/ Label Encoder + Imputer + One Hot Encoder + Oversampler:
Starting cross validation
Finished cross validation - mean AUC: 0.652
*****************************
* Evaluating Batch Number 2 *
*****************************
Random Forest Classifier w/ Label Encoder + Imputer + One Hot Encoder + Oversampler + RF Classifier Select From Model:
Starting cross validation
Finished cross validation - mean AUC: 0.646
*****************************
* Evaluating Batch Number 3 *
*****************************
Elastic Net Classifier w/ Label Encoder + Select Columns By Type Transformer + Label Encoder + Imputer + Standard Scaler + Select Columns Transformer + Select Columns Transformer + Label Encoder + Imputer + One Hot Encoder + Standard Scaler + Oversampler:
Starting cross validation
Finished cross validation - mean AUC: 0.645
CatBoost Classifier w/ Label Encoder + Select Columns By Type Transformer + Label Encoder + Imputer + Select Columns Transformer + Select Columns Transformer + Label Encoder + Imputer + Oversampler:
Starting cross validation
Finished cross validation - mean AUC: 0.881
Logistic Regression Classifier w/ Label Encoder + Select Columns By Type Transformer + Label Encoder + Imputer + Standard Scaler + Select Columns Transformer + Select Columns Transformer + Label Encoder + Imputer + One Hot Encoder + Standard Scaler + Oversampler:
Starting cross validation
Finished cross validation - mean AUC: 0.647
Search finished after 19.28 seconds
Best pipeline: CatBoost Classifier w/ Label Encoder + Select Columns By Type Transformer + Label Encoder + Imputer + Select Columns Transformer + Select Columns Transformer + Label Encoder + Imputer + Oversampler
Best pipeline AUC: 0.881234
[11]:
{1: {'Random Forest Classifier w/ Label Encoder + Imputer + One Hot Encoder + Oversampler': 4.213857412338257,
'Total time of batch': 4.3373541831970215},
2: {'Random Forest Classifier w/ Label Encoder + Imputer + One Hot Encoder + Oversampler + RF Classifier Select From Model': 4.434894561767578,
'Total time of batch': 4.561416387557983},
3: {'Elastic Net Classifier w/ Label Encoder + Select Columns By Type Transformer + Label Encoder + Imputer + Standard Scaler + Select Columns Transformer + Select Columns Transformer + Label Encoder + Imputer + One Hot Encoder + Standard Scaler + Oversampler': 4.006518125534058,
'CatBoost Classifier w/ Label Encoder + Select Columns By Type Transformer + Label Encoder + Imputer + Select Columns Transformer + Select Columns Transformer + Label Encoder + Imputer + Oversampler': 1.8225805759429932,
'Logistic Regression Classifier w/ Label Encoder + Select Columns By Type Transformer + Label Encoder + Imputer + Standard Scaler + Select Columns Transformer + Select Columns Transformer + Label Encoder + Imputer + One Hot Encoder + Standard Scaler + Oversampler': 3.6771702766418457,
'Total time of batch': 9.954506635665894}}
[12]:
automl_auc.rankings
[12]:
id | pipeline_name | search_order | ranking_score | mean_cv_score | standard_deviation_cv_score | percent_better_than_baseline | high_variance_cv | parameters | |
---|---|---|---|---|---|---|---|---|---|
0 | 4 | CatBoost Classifier w/ Label Encoder + Select ... | 4 | 0.881234 | 0.881234 | 0.014260 | 38.123362 | False | {'Label Encoder': {'positive_label': None}, 'N... |
1 | 1 | Random Forest Classifier w/ Label Encoder + Im... | 1 | 0.652452 | 0.652452 | 0.061740 | 15.245206 | False | {'Label Encoder': {'positive_label': None}, 'I... |
2 | 5 | Logistic Regression Classifier w/ Label Encode... | 5 | 0.646823 | 0.646823 | 0.043723 | 14.682289 | False | {'Label Encoder': {'positive_label': None}, 'N... |
3 | 2 | Random Forest Classifier w/ Label Encoder + Im... | 2 | 0.645598 | 0.645598 | 0.053493 | 14.559799 | False | {'Label Encoder': {'positive_label': None}, 'I... |
4 | 3 | Elastic Net Classifier w/ Label Encoder + Sele... | 3 | 0.645471 | 0.645471 | 0.042740 | 14.547088 | False | {'Label Encoder': {'positive_label': None}, 'N... |
5 | 0 | Mode Baseline Binary Classification Pipeline | 0 | 0.500000 | 0.500000 | 0.000000 | 0.000000 | False | {'Label Encoder': {'positive_label': None}, 'B... |
Like before, we can look at the rankings and pick the best pipeline.
[13]:
best_pipeline_auc = automl_auc.best_pipeline
[14]:
# get the auc and lead scoring score on holdout data
best_pipeline_auc_score = best_pipeline_auc.score(
X_holdout, y_holdout, objectives=["auc", lead_scoring_objective]
)
best_pipeline_auc_score
[14]:
OrderedDict([('AUC', 0.873904502870958),
('Lead Scoring', 0.37403267411865865)])
[15]:
assert best_pipeline_score["Lead Scoring"] >= best_pipeline_auc_score["Lead Scoring"]
assert best_pipeline_auc_score["Lead Scoring"] >= 0
When we optimize for AUC, we can see that the AUC score from this pipeline is similar to the AUC score from the pipeline optimized for lead scoring. However, the revenue per lead is much smaller per lead when optimized for AUC and was much larger when optimized for lead scoring. As a result, we would have a huge gain on the amount of revenue if we optimized for lead scoring.
This happens because optimizing for AUC does not take into account the user-specified true_positive
(dollar amount to be gained with a successful lead) and false_positive
(dollar amount to be lost with an unsuccessful lead) values. Thus, the best pipelines may produce the highest AUC but may not actually generate the most revenue through lead scoring.
This example highlights how performance in the real world can diverge greatly from machine learning metrics.