Estimating Standard Performance Metrics for Binary Classification
This tutorial explains how to use NannyML to estimate the performance of binary classification
models in the absence of target data. To find out how CBPE
estimates performance, read the explanation of Confidence-based
Performance Estimation.
Note
The following example uses timestamps. These are optional but have an impact on the way data is chunked and results are plotted. You can read more about them in the data requirements.
Just The Code
>>> import nannyml as nml
>>> from IPython.display import display
>>> reference_df = nml.load_synthetic_car_loan_dataset()[0]
>>> analysis_df = nml.load_synthetic_car_loan_dataset()[1]
>>> display(reference_df.head(3))
>>> estimator = nml.CBPE(
... y_pred_proba='y_pred_proba',
... y_pred='y_pred',
... y_true='repaid',
... timestamp_column_name='timestamp',
... metrics=['roc_auc', 'accuracy', 'f1'],
... chunk_size=5000,
... problem_type='classification_binary',
>>> )
>>> estimator.fit(reference_df)
>>> results = estimator.estimate(analysis_df)
>>> display(results.filter(period='analysis').to_df())
>>> metric_fig = results.plot()
>>> metric_fig.show()
Advanced configuration
To learn how
Chunk
works and to set up custom chunkings check out the chunking tutorialTo learn how
ConstantThreshold
works and to set up custom threshold check out the thresholds tutorial
Walkthrough
For simplicity this guide is based on a synthetic dataset included in the library, where the monitored model predicts whether a customer will repay a loan to buy a car. Check out Car Loan Dataset to learn more about this dataset.
In order to monitor a model, NannyML needs to learn about it from a reference dataset. Then it can monitor the data that is subject to actual analysis, provided as the analysis dataset. You can read more about this in our section on data periods.
We start by loading the dataset we’ll be using:
>>> import nannyml as nml
>>> from IPython.display import display
>>> reference_df = nml.load_synthetic_car_loan_dataset()[0]
>>> analysis_df = nml.load_synthetic_car_loan_dataset()[1]
>>> display(reference_df.head(3))
id |
car_value |
salary_range |
debt_to_income_ratio |
loan_length |
repaid_loan_on_prev_car |
size_of_downpayment |
driver_tenure |
repaid |
timestamp |
y_pred_proba |
y_pred |
|
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 |
0 |
39811 |
40K - 60K € |
0.63295 |
19 |
False |
40% |
0.212653 |
1 |
2018-01-01 00:00:00.000 |
0.99 |
1 |
1 |
1 |
12679 |
40K - 60K € |
0.718627 |
7 |
True |
10% |
4.92755 |
0 |
2018-01-01 00:08:43.152 |
0.07 |
0 |
2 |
2 |
19847 |
40K - 60K € |
0.721724 |
17 |
False |
0% |
0.520817 |
1 |
2018-01-01 00:17:26.304 |
1 |
1 |
Next we create the Confidence-based Performance Estimation
(CBPE
)
estimator. In this example, we will estimate the following metrics: roc_auc, accuracy, and f1.
We specify the following parameters in the initialization of the estimator:
y_pred_proba: the name of the column in the reference data that contains the predicted probabilities.
y_pred: the name of the column in the reference data that contains the predicted classes.
y_true: the name of the column in the reference data that contains the true classes.
timestamp_column_name (Optional): the name of the column in the reference data that contains timestamps.
metrics: a list of metrics to estimate. For more information about the metrics that can be estimated for binary classification, check out the Binary Performance Estimation page.
chunk_size (Optional): the number of observations in each chunk of data used to estimate performance. For more information about chunking configurations check out the chunking tutorial.
problem_type: the type of problem being monitored. In this example we will monitor a binary classification problem.
thresholds (Optional): the thresholds used to calculate the alert flag. For more information about thresholds, check out the thresholds tutorial.
>>> estimator = nml.CBPE(
... y_pred_proba='y_pred_proba',
... y_pred='y_pred',
... y_true='repaid',
... timestamp_column_name='timestamp',
... metrics=['roc_auc', 'accuracy', 'f1'],
... chunk_size=5000,
... problem_type='classification_binary',
>>> )
The CBPE
estimator is then fitted using the
fit()
method on the reference data.
>>> estimator.fit(reference_df)
The fitted estimator
can be used to estimate performance on other data, for which performance cannot be calculated.
Typically, this would be used on the latest production data where target is missing. In our example this is
the analysis_df
data.
NannyML can then output a dataframe that contains all the results. Let’s have a look at the results for analysis period only.
>>> results = estimator.estimate(analysis_df)
>>> display(results.filter(period='analysis').to_df())
chunk
key
|
chunk_index
|
start_index
|
end_index
|
start_date
|
end_date
|
period
|
roc_auc
value
|
sampling_error
|
realized
|
upper_confidence_boundary
|
lower_confidence_boundary
|
upper_threshold
|
lower_threshold
|
alert
|
accuracy
value
|
sampling_error
|
realized
|
upper_confidence_boundary
|
lower_confidence_boundary
|
upper_threshold
|
lower_threshold
|
alert
|
f1
value
|
sampling_error
|
realized
|
upper_confidence_boundary
|
lower_confidence_boundary
|
upper_threshold
|
lower_threshold
|
alert
|
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 |
[0:4999] |
0 |
0 |
4999 |
2018-10-30 18:00:00 |
2018-11-30 00:27:16.848000 |
analysis |
0.970744 |
0.00181072 |
nan |
0.976176 |
0.965311 |
0.97866 |
0.963317 |
False |
0.941792 |
0.0032614 |
nan |
0.951576 |
0.932008 |
0.951421 |
0.935859 |
False |
0.94303 |
0.00643972 |
nan |
0.96235 |
0.923711 |
0.95085 |
0.93466 |
False |
1 |
[5000:9999] |
1 |
5000 |
9999 |
2018-11-30 00:36:00 |
2018-12-30 07:03:16.848000 |
analysis |
0.971011 |
0.00181072 |
nan |
0.976443 |
0.965578 |
0.97866 |
0.963317 |
False |
0.943321 |
0.0032614 |
nan |
0.953105 |
0.933537 |
0.951421 |
0.935859 |
False |
0.941324 |
0.00643972 |
nan |
0.960644 |
0.922005 |
0.95085 |
0.93466 |
False |
2 |
[10000:14999] |
2 |
10000 |
14999 |
2018-12-30 07:12:00 |
2019-01-29 13:39:16.848000 |
analysis |
0.971407 |
0.00181072 |
nan |
0.976839 |
0.965975 |
0.97866 |
0.963317 |
False |
0.945492 |
0.0032614 |
nan |
0.955276 |
0.935707 |
0.951421 |
0.935859 |
False |
0.943574 |
0.00643972 |
nan |
0.962893 |
0.924255 |
0.95085 |
0.93466 |
False |
3 |
[15000:19999] |
3 |
15000 |
19999 |
2019-01-29 13:48:00 |
2019-02-28 20:15:16.848000 |
analysis |
0.971091 |
0.00181072 |
nan |
0.976524 |
0.965659 |
0.97866 |
0.963317 |
False |
0.944816 |
0.0032614 |
nan |
0.9546 |
0.935032 |
0.951421 |
0.935859 |
False |
0.943159 |
0.00643972 |
nan |
0.962478 |
0.923839 |
0.95085 |
0.93466 |
False |
4 |
[20000:24999] |
4 |
20000 |
24999 |
2019-02-28 20:24:00 |
2019-03-31 02:51:16.848000 |
analysis |
0.971123 |
0.00181072 |
nan |
0.976555 |
0.965691 |
0.97866 |
0.963317 |
False |
0.944645 |
0.0032614 |
nan |
0.954429 |
0.934861 |
0.951421 |
0.935859 |
False |
0.944204 |
0.00643972 |
nan |
0.963523 |
0.924885 |
0.95085 |
0.93466 |
False |
5 |
[25000:29999] |
5 |
25000 |
29999 |
2019-03-31 03:00:00 |
2019-04-30 09:27:16.848000 |
analysis |
0.96109 |
0.00181072 |
nan |
0.966522 |
0.955658 |
0.97866 |
0.963317 |
True |
0.910714 |
0.0032614 |
nan |
0.920498 |
0.90093 |
0.951421 |
0.935859 |
True |
0.911753 |
0.00643972 |
nan |
0.931073 |
0.892434 |
0.95085 |
0.93466 |
True |
6 |
[30000:34999] |
6 |
30000 |
34999 |
2019-04-30 09:36:00 |
2019-05-30 16:03:16.848000 |
analysis |
0.961825 |
0.00181072 |
nan |
0.967257 |
0.956393 |
0.97866 |
0.963317 |
True |
0.911149 |
0.0032614 |
nan |
0.920934 |
0.901365 |
0.951421 |
0.935859 |
True |
0.911766 |
0.00643972 |
nan |
0.931085 |
0.892446 |
0.95085 |
0.93466 |
True |
7 |
[35000:39999] |
7 |
35000 |
39999 |
2019-05-30 16:12:00 |
2019-06-29 22:39:16.848000 |
analysis |
0.961073 |
0.00181072 |
nan |
0.966506 |
0.955641 |
0.97866 |
0.963317 |
True |
0.911193 |
0.0032614 |
nan |
0.920977 |
0.901409 |
0.951421 |
0.935859 |
True |
0.911661 |
0.00643972 |
nan |
0.93098 |
0.892342 |
0.95085 |
0.93466 |
True |
8 |
[40000:44999] |
8 |
40000 |
44999 |
2019-06-29 22:48:00 |
2019-07-30 05:15:16.848000 |
analysis |
0.962533 |
0.00181072 |
nan |
0.967966 |
0.957101 |
0.97866 |
0.963317 |
True |
0.914389 |
0.0032614 |
nan |
0.924174 |
0.904605 |
0.951421 |
0.935859 |
True |
0.913763 |
0.00643972 |
nan |
0.933082 |
0.894444 |
0.95085 |
0.93466 |
True |
9 |
[45000:49999] |
9 |
45000 |
49999 |
2019-07-30 05:24:00 |
2019-08-29 11:51:16.848000 |
analysis |
0.961316 |
0.00181072 |
nan |
0.966748 |
0.955884 |
0.97866 |
0.963317 |
True |
0.911731 |
0.0032614 |
nan |
0.921516 |
0.901947 |
0.951421 |
0.935859 |
True |
0.914751 |
0.00643972 |
nan |
0.93407 |
0.895432 |
0.95085 |
0.93466 |
True |
Apart from chunk-related data, the results data have the following columns for each metric that was estimated:
value - the estimate of a metric for a specific chunk.
sampling_error - the estimate of the Sampling Error.
realized - when target values are available for a chunk, the realized performance metric will also be calculated and included within the results.
upper_confidence_boundary and lower_confidence_boundary - These values show the confidence band of the relevant metric and are equal to estimated value +/- 3 times the estimated sampling error.
upper_threshold and lower_threshold - crossing these thresholds will raise an alert on significant performance change. The thresholds are calculated based on the actual performance of the monitored model on chunks in the reference partition. The thresholds are 3 standard deviations away from the mean performance calculated on chunks. The thresholds are calculated during
fit
phase. You can also set up custom thresholds using constant or standard deviations thresholds, to learn more about it check out our tutorial on thresholds.alert - flag indicating potentially significant performance change.
True
if estimated performance crosses upper or lower threshold.
These results can be also plotted. Our plot contains several key elements.
The purple dashed step plot shows the estimated performance in each chunk of the provided data. Thick squared point markers indicate the middle of these chunks.
The black vertical line splits the reference and analysis periods.
The low-saturated purple area around the estimated performance in the analysis period corresponds to the confidence band which is calculated as the estimated performance +/- 3 times the estimated Sampling Error.
The red horizontal dashed lines show upper and lower thresholds that indicate the range of expected performance values.
The red diamond-shaped point markers in the middle of a chunk indicate that an alert has been raised. Alerts are caused by the estimated performance crossing the upper or lower threshold.
>>> metric_fig = results.plot()
>>> metric_fig.show()
Additional information such as the chunk index range and chunk date range (if timestamps were provided) is shown in the hover for each chunk (these are interactive plots, though only static views are included here).
Insights
After reviewing the performance estimation results, we should be able to see any indications of performance change that NannyML has detected based upon the model’s inputs and outputs alone.
What’s next
The Data Drift functionality can help us to understand whether data drift is causing the performance problem. When the target values become available we can compared realized and estimated performance results.