Class Likelihood Ratios to measure classification performance
This example demonstrates the class_likelihood_ratios
function, which computes the positive and negative likelihood ratios (LR+,
LR-) to assess the predictive power of a binary classifier. As we will see,
these metrics are independent of the proportion between classes in the test set,
which makes them very useful when the available data for a study has a different
class proportion than the target application.
A typical use is a case-control study in medicine, which has nearly balanced
classes while the general population has large class imbalance. In such
application, the pre-test probability of an individual having the target
condition can be chosen to be the prevalence, i.e. the proportion of a
particular population found to be affected by a medical condition. The post-test
probabilities represent then the probability that the condition is truly present
given a positive test result.
In this example we first discuss the link between pre-test and post-test odds
given by the Class likelihood ratios. Then we evaluate their behavior in
some controlled scenarios. In the last section we plot them as a function of the
prevalence of the positive class.
# Authors: The scikit-learn developers# SPDX-License-Identifier: BSD-3-Clause
Pre-test vs. post-test analysis
Suppose we have a population of subjects with physiological measurements X
that can hopefully serve as indirect bio-markers of the disease and actual
disease indicators y (ground truth). Most of the people in the population do
not carry the disease but a minority (in this case around 10%) does:
fromsklearn.datasetsimportmake_classificationX,y=make_classification(n_samples=10_000,weights=[0.9,0.1],random_state=0)print(f"Percentage of people carrying the disease: {100*y.mean():.2f}%")
Percentage of people carrying the disease: 10.37%
A machine learning model is built to diagnose if a person with some given
physiological measurements is likely to carry the disease of interest. To
evaluate the model, we need to assess its performance on a held-out test set:
Then we can fit our diagnosis model and compute the positive likelihood
ratio to evaluate the usefulness of this classifier as a disease diagnosis
tool:
Since the positive class likelihood ratio is much larger than 1.0, it means
that the machine learning-based diagnosis tool is useful: the post-test odds
that the condition is truly present given a positive test result are more than
12 times larger than the pre-test odds.
Cross-validation of likelihood ratios
We assess the variability of the measurements for the class likelihood ratios
in some particular cases.
We confirm that the model is useful: the post-test odds are between 12 and 20
times larger than the pre-test odds.
On the contrary, let’s consider a dummy model that will output random
predictions with similar odds as the average disease prevalence in the
training set:
/home/circleci/project/sklearn/utils/_param_validation.py:218:UndefinedMetricWarning:No samples were predicted for the positive classand `positive_likelihood_ratio` isset to `np.nan`.Use the `replace_undefined_by` param to
returnfunc(*args,**kwargs)/home/circleci/project/sklearn/utils/_param_validation.py:218:UndefinedMetricWarning:No samples were predicted for the positive `positive_likelihood_ratio` to `npnan` the `replace_undefined_by` param to
positive
negative
mean
1.0
1.0
std
0.0
0.0
The absence of positive predictions means there will be no true positives nor
false positives, leading to an undefined LR+ that by no means should be
interpreted as an infinite LR+ (the classifier perfectly identifying
positive cases). In such situation the
class_likelihood_ratios function returns nan and
raises a warning by default. Indeed, the value of LR- helps us discard this
model.
A similar scenario may arise when cross-validating highly imbalanced data with
few samples: some folds will have no samples with the disease and therefore
they will output no true positives nor false negatives when used for testing.
Mathematically this leads to an infinite LR+, which should also not be
interpreted as the model perfectly identifying positive cases. Such event
leads to a higher variance of the estimated likelihood ratios, but can still
be interpreted as an increment of the post-test odds of having the condition.
/home/circleci/project/sklearn/utils/_param_validation.py:218:UndefinedMetricWarning: `positive_likelihood_ratio` is ill-defined andset to `np.nan`.Use the `replace_undefined_by` param to
returnfunc(*args,**kwargs)/home/circleci/project/sklearn/utils/_param_validation.py:218:UndefinedMetricWarning: `positive_likelihood_ratio` is ill-defined andset to `npnan` the `replace_undefined_by` param to
positive
negative
mean
9.400000
0.373333
std
10.532489
0.235430
Invariance with respect to prevalence
The likelihood ratios are independent of the disease prevalence and can be
extrapolated between populations regardless of any possible class imbalance,
as long as the same model is applied to all of them. Notice that in the
plots below the decision boundary is constant (see
SVM: Separating hyperplane for unbalanced classes for
a study of the boundary decision for unbalanced classes).
Here we train a LogisticRegression base model
on a case-control study with a prevalence of 50%. It is then evaluated over
populations with varying prevalence. We use the
make_classification function to ensure the
data-generating process is always the same as shown in the plots below. The
label 1 corresponds to the positive class “disease”, whereas the label 0
stands for “no-disease”.
We will now show the decision boundary for each level of prevalence. Note that
we only plot a subset of the original data to better assess the linear model
decision boundary.
In the plots below we observe that the class likelihood ratios re-computed
with different prevalences are indeed constant within one standard deviation
of those computed with on balanced classes.
/home/circleci/project/sklearn/utils/_param_validation.py:218:UndefinedMetricWarning:No samples were predicted for the positive classand `positive_likelihood_ratio` isset to `np.nan`.Use the `replace_undefined_by` param to
returnfunc(*args,**kwargs)
/home/circleci/project/sklearn/utils/_param_validation.py:218:UndefinedMetricWarning:No samples were predicted for the positive classand `positive_likelihood_ratio` isset to `np.nan`.Use the `replace_undefined_by` param to
returnfunc(*args,**kwargs)
/home/circleci/project/sklearn/utils/_param_validation.py:218:UndefinedMetricWarning:No samples were predicted for the positive classand `positive_likelihood_ratio` isset to `np.nan`.Use the `replace_undefined_by` param to
returnfunc(*args,**kwargs)
/home/circleci/project/sklearn/utils/_param_validation.py:218:UndefinedMetricWarning:No samples were predicted for the positive classand `positive_likelihood_ratio` isset to `np.nan`.Use the `replace_undefined_by` param to
returnfunc(*args,**kwargs)
/home/circleci/project/sklearn/utils/_param_validation.py:218:UndefinedMetricWarning:No samples were predicted for the positive classand `positive_likelihood_ratio` isset to `np.nan`.Use the `replace_undefined_by` param to
returnfunc(*args,**kwargs)
/home/circleci/project/sklearn/utils/_param_validation.py:218:UndefinedMetricWarning:No samples were predicted for the positive classand `positive_likelihood_ratio` isset to `np.nan`.Use the `replace_undefined_by` param to
returnfunc(*args,**kwargs)
/home/circleci/project/sklearn/utils/_param_validation.py:218:UndefinedMetricWarning:No samples were predicted for the positive classand `positive_likelihood_ratio` isset to `np.nan`.Use the `replace_undefined_by` param to
returnfunc(*args,**kwargs)
/home/circleci/project/sklearn/utils/_param_validation.py:218:UndefinedMetricWarning:No samples were predicted for the positive classand `positive_likelihood_ratio` isset to `np.nan`.Use the `replace_undefined_by` param to
returnfunc(*args,**kwargs)
cross_validate
(
estimator
,
X
,
y
,
scoring
=
scoring
,
cv
=
10
))
.
.
Use
returnfunc(*args,**kwargs)
/home/circleci/project/sklearn/utils/_param_validation.py:218:UndefinedMetricWarning: `positive_likelihood_ratio` is ill-defined andset to `np.nan`.Use the `replace_undefined_by` param to
returnfunc(*args,**kwargs)
/home/circleci/project/sklearn/utils/_param_validation.py:218:UndefinedMetricWarning: `positive_likelihood_ratio` is ill-defined andset to `np.nan`.Use the `replace_undefined_by` param to
returnfunc(*args,**kwargs)
/home/circleci/project/sklearn/utils/_param_validation.py:218:UndefinedMetricWarning: `positive_likelihood_ratio` is ill-defined andset to `np.nan`.Use the `replace_undefined_by` param to