The Cost-Benefit Matrix (CostBenefitMatrix) objective is an objective that assigns costs to each of the quadrants of a confusion matrix to quantify the cost of being correct or incorrect.
CostBenefitMatrix
Confusion matrices are tables that summarize the number of correct and incorrectly-classified predictions, broken down by each class. They allow us to quickly understand the performance of a classification model and where the model gets “confused” when it is making predictions. For the binary classification problem, there are four possible combinations of prediction and actual target values possible:
true positives (correct positive assignments)
true negatives (correct negative assignments)
false positives (incorrect positive assignments)
false negatives (incorrect negative assignments)
An example of how to calculate a confusion matrix can be found here.
Although the confusion matrix is an incredibly useful visual for understanding our model, each prediction that is correctly or incorrectly classified is treated equally. For example, for detecting breast cancer, the confusion matrix does not take into consideration that it could be much more costly to incorrectly classify a malignant tumor as benign than it is to incorrectly classify a benign tumor as malignant. This is where the cost-benefit matrix shines: it uses the cost of each of the four possible outcomes to weigh each outcome differently. By scoring using the cost-benefit matrix, we can measure the score of the model by a concrete unit that is more closely related to the goal of the model. In the below example, we will show how the cost-benefit matrix objective can be used, and how it can give us better real-world impact when compared to using other standard machine learning objectives.
In this example, we will be using a customer churn data set taken from Kaggle.
This dataset includes records of over 7000 customers, and includes customer account information, demographic information, services they signed up for, and whether or not the customer “churned” or left within the last month.
The target we want to predict is whether the customer churned (“Yes”) or did not churn (“No”). In the dataset, approximately 73.5% of customers did not churn, and 26.5% did. We will refer to the customers who churned as the “positive” class and the customers who did not churn as the “negative” class.
[1]:
from evalml.demos.churn import load_churn X, y = load_churn()
Number of Features Categorical 16 Numeric 3 Number of training examples: 7043 Labels No 73.46% Yes 26.54% Name: Churn, dtype: object
In this example, let’s say that correctly identifying customers who will churn (true positive case) will give us a net profit of $400, because it allows us to intervene, incentivize the customer to stay, and sign a new contract. Incorrectly classifying customers who were not going to churn as customers who will churn (false positive case) will cost $100 to represent the marketing and effort used to try to retain the user. Not identifying customers who will churn (false negative case) will cost us $200 to represent the lost in revenue from losing a customer. Finally, correctly identifying customers who will not churn (true negative case) will not cost us anything ($0), as nothing needs to be done for that customer.
We can represent these values in our CostBenefitMatrix objective, where a negative value represents a cost and a positive value represents a profit–note that this means that the greater the score, the more profit we will make.
[2]:
from evalml.objectives import CostBenefitMatrix cost_benefit_matrix = CostBenefitMatrix(true_positive=400, true_negative=0, false_positive=-100, false_negative=-200)
First, let us run AutoML search to train pipelines using the default objective for binary classification (log loss).
[3]:
from evalml import AutoMLSearch automl = AutoMLSearch(problem_type='binary', objective='log_loss_binary') automl.search(X, y) ll_pipeline = automl.best_pipeline ll_pipeline.fit(X, y) ll_pipeline.score(X, y, ['log_loss_binary'])
Using default limit of max_pipelines=5. Generating pipelines to search over... ***************************** * Beginning pipeline search * ***************************** Optimizing for Log Loss Binary. Lower score is better. Searching up to 5 pipelines. Allowed model families: linear_model, extra_trees, random_forest, catboost, xgboost
(1/5) Mode Baseline Binary Classification P... Elapsed:00:00 Starting cross validation Finished cross validation - mean Log Loss Binary: 9.166 (2/5) Extra Trees Classifier w/ Imputer + O... Elapsed:00:00 Starting cross validation Finished cross validation - mean Log Loss Binary: 0.432 (3/5) Elastic Net Classifier w/ Imputer + O... Elapsed:00:04 Starting cross validation Finished cross validation - mean Log Loss Binary: 0.579 (4/5) CatBoost Classifier w/ Imputer Elapsed:00:06 Starting cross validation Finished cross validation - mean Log Loss Binary: 0.600 (5/5) XGBoost Classifier w/ Imputer + One H... Elapsed:00:07 Starting cross validation Finished cross validation - mean Log Loss Binary: 0.433 Search finished after 01:33 Best pipeline: Extra Trees Classifier w/ Imputer + One Hot Encoder Best pipeline Log Loss Binary: 0.432368
OrderedDict([('Log Loss Binary', 0.41992009443842054)])
When we train our pipelines using log loss as our primary objective, we try to find pipelines that minimize log loss. However, our ultimate goal in training models is to find a model that gives us the most profit. When we score our pipeline on the cost benefit matrix (using the costs outlined above) to determine the profit we would earn from the predictions made by this model, we get approximately 14.851, indicating $14.85 in profit per customer. Across our 7043 customers, we get a total of approximately $104,588.55 in profit.
[4]:
ll_pipeline.score(X, y, [cost_benefit_matrix])
OrderedDict([('Cost Benefit Matrix', 15.547351980690053)])
Let’s try rerunning our AutoML search, but this time using the cost-benefit matrix as our primary objective to optimize.
[5]:
automl = AutoMLSearch(problem_type='binary', objective=cost_benefit_matrix) automl.search(X, y) cbm_pipeline = automl.best_pipeline cbm_pipeline.fit(X, y)
Using default limit of max_pipelines=5. Generating pipelines to search over... ***************************** * Beginning pipeline search * ***************************** Optimizing for Cost Benefit Matrix. Greater score is better. Searching up to 5 pipelines. Allowed model families: linear_model, extra_trees, random_forest, catboost, xgboost
(1/5) Mode Baseline Binary Classification P... Elapsed:00:00 Starting cross validation Finished cross validation - mean Cost Benefit Matrix: -53.074 (2/5) Extra Trees Classifier w/ Imputer + O... Elapsed:00:00 Starting cross validation Finished cross validation - mean Cost Benefit Matrix: 14.411 (3/5) Elastic Net Classifier w/ Imputer + O... Elapsed:00:03 Starting cross validation Finished cross validation - mean Cost Benefit Matrix: -53.074 (4/5) CatBoost Classifier w/ Imputer Elapsed:00:05 Starting cross validation Finished cross validation - mean Cost Benefit Matrix: 16.087 (5/5) XGBoost Classifier w/ Imputer + One H... Elapsed:00:07 Starting cross validation Finished cross validation - mean Cost Benefit Matrix: 23.129 Search finished after 01:24 Best pipeline: XGBoost Classifier w/ Imputer + One Hot Encoder Best pipeline Cost Benefit Matrix: 23.129440
<evalml.pipelines.utils.make_pipeline.<locals>.GeneratedPipeline at 0x7f08eefea828>
Now, if we calculate the cost-benefit matrix score on our best pipeline, we see that with this pipeline optimized for our cost-benefit matrix objective, we are able to generate $42.24 in profit per customer. Across our 7043 customers, we get a total of approximately $297,496.32 in profit–much more than the profit returned from our previous pipeline! Custom objectives like CostBenefitMatrix are just one example of how using EvalML can help find pipelines that can perform better on real-world problems, rather than on arbitrary standard statistical metrics.
[6]:
cbm_pipeline.score(X, y, [cost_benefit_matrix])
OrderedDict([('Cost Benefit Matrix', 42.2405225046145)])
Finally, we can graph the confusion matrices for both pipelines to show that our pipeline trained with the cost-benefit matrix is able to correctly classify more samples than the pipeline trained with log loss.
[7]:
from evalml.model_understanding.graphs import graph_confusion_matrix # pipeline trained with log loss y_pred = ll_pipeline.predict(X) graph_confusion_matrix(y, y_pred)
[8]:
# pipeline trained with cost-benefit matrix y_pred = cbm_pipeline.predict(X) graph_confusion_matrix(y, y_pred)