Skip to content

FEA Confusion matrix derived metrics #19556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 201 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
201 commits
Select commit Hold shift + click to select a range
9dbfbc8
added a function with confusion matrix derived metrics (fpr, tpr, tnr…
Nov 2, 2019
64a5a7b
changed the true postive sum in the function
Nov 2, 2019
523eaa0
add print
Nov 2, 2019
b977216
remove one print
Nov 2, 2019
5a061ef
remove print statements
Nov 3, 2019
6493977
add coauthors.
Nov 3, 2019
141fa4a
fix doc string outputs
Nov 3, 2019
9615ae8
pep8 test
Nov 3, 2019
79e1562
trivial
Nov 3, 2019
8f21052
remove imported but unused flake8
Nov 3, 2019
3ffd830
to trigger test
Nov 3, 2019
fb73c6e
Take over PR #15522
haochunchang May 18, 2020
c780053
Modify doc and zero-division in the weighted average.
haochunchang May 18, 2020
408c2db
Add tests for binary, multiclass and empty prediction.
haochunchang May 19, 2020
4adfe2e
Add tpr_fpr_tnr_fnr_scores to test_common.py.
haochunchang May 20, 2020
53d6fd2
Remove pred_sum variable
haochunchang May 20, 2020
88c41af
Merge branch 'master' of https://github.com/scikit-learn/scikit-learn…
haochunchang Sep 29, 2020
a5b5262
Fix linting
haochunchang Sep 29, 2020
f74fc10
Fix parameter documentation
haochunchang Oct 5, 2020
44f8b7d
Add specificity_score as a single metric
Pawel-Kranzberg Feb 25, 2021
56132a3
Update __init__.py
Pawel-Kranzberg Feb 25, 2021
18074d2
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Feb 25, 2021
995a65c
Fix linting, add what's new entry
Pawel-Kranzberg Feb 25, 2021
88b21e6
Merge branch 'confusion-matrix-derived-metrics' of https://github.com…
Pawel-Kranzberg Feb 25, 2021
769b3af
Fix linting
Pawel-Kranzberg Feb 25, 2021
edd6ff0
Fix linting
Pawel-Kranzberg Feb 25, 2021
b8100d7
Added scorer, fixed tests
Pawel-Kranzberg Feb 25, 2021
aeed5e5
Linting
Pawel-Kranzberg Feb 25, 2021
dc5423f
Further tests
Pawel-Kranzberg Feb 25, 2021
9c1a0a4
Test bug fixes
Pawel-Kranzberg Feb 25, 2021
2e8efc9
Test bug fixes
Pawel-Kranzberg Feb 25, 2021
91b4479
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Mar 18, 2021
867e287
Update v1.0.rst
Pawel-Kranzberg Apr 18, 2021
8473832
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Apr 19, 2021
f29b0c9
Add `metrics.fpr_tpr_fnr_tnr_scores` to v1.0.rst
Pawel-Kranzberg Apr 22, 2021
6311b30
tpr, fpr, tnr, fnr to upper case
Pawel-Kranzberg Apr 22, 2021
50125d7
Update test_classification.py
Pawel-Kranzberg Apr 22, 2021
bae4c36
Linting fix
Pawel-Kranzberg Apr 22, 2021
5a391fa
Typo fix
Pawel-Kranzberg Apr 22, 2021
07b9a68
Revert "Typo fix"
Pawel-Kranzberg Apr 23, 2021
366a113
retrigger checks
Pawel-Kranzberg Apr 23, 2021
9214303
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Apr 23, 2021
1e5159f
retrigger checks
Pawel-Kranzberg Apr 25, 2021
8fc7ec0
retrigger checks
Pawel-Kranzberg Apr 27, 2021
459708f
retrigger checks
Pawel-Kranzberg Apr 30, 2021
ff3cb6a
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg May 23, 2021
b6167ec
Apply suggestions from code review
Pawel-Kranzberg Sep 11, 2021
8560720
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Sep 13, 2021
2b8efcc
Fix recall
Pawel-Kranzberg Sep 13, 2021
bea1cb0
Linting fixes
Pawel-Kranzberg Sep 13, 2021
d150e06
Fix linting
Pawel-Kranzberg Sep 13, 2021
cf36e6a
Fix linting
Pawel-Kranzberg Sep 13, 2021
aa76f66
Fix linting
Pawel-Kranzberg Sep 13, 2021
3e6ecd6
Fix linting
Pawel-Kranzberg Sep 13, 2021
54b5bfb
Fix linting
Pawel-Kranzberg Sep 13, 2021
eb56ee0
Fix linting
Pawel-Kranzberg Sep 13, 2021
d2d150b
Fix linting
Pawel-Kranzberg Sep 13, 2021
66a6bb4
Fix linting
Pawel-Kranzberg Sep 13, 2021
ff585f1
Fix linting
Pawel-Kranzberg Sep 13, 2021
ff79159
Fix linting
Pawel-Kranzberg Sep 13, 2021
83f2788
Fix docs formatting
Pawel-Kranzberg Sep 13, 2021
657eac7
Fix linting in _classification.py
Pawel-Kranzberg Sep 13, 2021
0426087
Doc fixes in _classification.py
Pawel-Kranzberg Sep 15, 2021
636f87c
Revert change to v1.0.rst
Pawel-Kranzberg Sep 15, 2021
356659c
Revert "Revert change to v1.0.rst"
Pawel-Kranzberg Sep 15, 2021
71cf7cf
Update v1.0.rst
Pawel-Kranzberg Sep 15, 2021
fa77a96
Update v1.1.rst
Pawel-Kranzberg Sep 15, 2021
dc81e7e
Update _classification.py
Pawel-Kranzberg Sep 15, 2021
2dc9b25
Update _classification.py
Pawel-Kranzberg Sep 15, 2021
23f6d01
Fix linting
Pawel-Kranzberg Sep 15, 2021
c7084c4
Update docs in _classification.py
Pawel-Kranzberg Sep 15, 2021
77421ae
Add npv_score
Pawel-Kranzberg Sep 15, 2021
5027a6a
Add npv_score
Pawel-Kranzberg Sep 15, 2021
8b8a196
Add npv_score
Pawel-Kranzberg Sep 15, 2021
483178d
Add npv_score
Pawel-Kranzberg Sep 15, 2021
34849f6
npv_score
Pawel-Kranzberg Sep 15, 2021
ecc055a
Add npv_score
Pawel-Kranzberg Sep 18, 2021
136130f
Add `npv_score`
Pawel-Kranzberg Sep 18, 2021
60e27ed
Fix linting
Pawel-Kranzberg Sep 18, 2021
20b87a2
Add `npv_score`
Pawel-Kranzberg Sep 18, 2021
29b682c
Fix linting
Pawel-Kranzberg Sep 18, 2021
0791962
Fix test_common.py
Pawel-Kranzberg Sep 18, 2021
d04a4e2
Add `npv_score`
Pawel-Kranzberg Sep 18, 2021
3b5853c
Update docs
Pawel-Kranzberg Sep 18, 2021
18361ea
Update model_evaluation.rst
Pawel-Kranzberg Sep 18, 2021
797c69f
Update _classification.py
Pawel-Kranzberg Sep 18, 2021
d1701fb
Fix docs
Pawel-Kranzberg Sep 18, 2021
dc2602f
Fix model_evaluation.rst
Pawel-Kranzberg Sep 18, 2021
e61eb88
Fix docs
Pawel-Kranzberg Sep 18, 2021
6daf00d
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Sep 24, 2021
d253c58
Update _classification.py
Pawel-Kranzberg Jan 20, 2022
e2544f0
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Jan 20, 2022
f38be61
Implement review
Pawel-Kranzberg Jan 20, 2022
ba5f5ea
Fix linting
Pawel-Kranzberg Jan 20, 2022
3199d00
Update v1.1.rst
Pawel-Kranzberg Jan 20, 2022
244920a
Update _classification.py
Pawel-Kranzberg Jan 20, 2022
4e9a3ee
Update _classification.py
Pawel-Kranzberg Jan 20, 2022
93c5293
Update _classification.py
Pawel-Kranzberg Jan 21, 2022
1b5611a
Update the tpr_fpr_tnr_fnr_score doc and specificity test
Pawel-Kranzberg Jan 22, 2022
21065f4
Update test_classification.py
Pawel-Kranzberg Jan 22, 2022
2c621a6
Update test_classification.py
Pawel-Kranzberg Jan 22, 2022
4ff05d1
Update test_classification.py
Pawel-Kranzberg Jan 24, 2022
264731e
Update test_classification.py
Pawel-Kranzberg Jan 24, 2022
1806ad0
Update test_classification.py
Pawel-Kranzberg Jan 24, 2022
955a286
Update test_classification.py
Pawel-Kranzberg Jan 25, 2022
d3798f5
Update test_classification.py
Pawel-Kranzberg Jan 25, 2022
90f2bb0
Update test_classification.py
Pawel-Kranzberg Jan 25, 2022
cc3dc7a
Update test_classification.py
Pawel-Kranzberg Jan 25, 2022
3ac784c
Update test_classification.py
Pawel-Kranzberg Jan 25, 2022
0958afb
Update test_classification.py
Pawel-Kranzberg Jan 25, 2022
79473c8
Update test_classification.py
Pawel-Kranzberg Jan 25, 2022
83e84df
Update test_classification.py
Pawel-Kranzberg Jan 26, 2022
bdf7920
Update test_classification.py
Pawel-Kranzberg Jan 26, 2022
81573d5
Update test_classification.py
Pawel-Kranzberg Jan 26, 2022
e75c3fa
Update test_classification.py
Pawel-Kranzberg Jan 26, 2022
1a3068c
Update test_classification.py
Pawel-Kranzberg Jan 26, 2022
e4c8cc8
Update model_evaluation.rst
Pawel-Kranzberg Jan 26, 2022
cfb6a62
Update model_evaluation.rst
Pawel-Kranzberg Jan 26, 2022
eb9e340
Update model_evaluation.rst
Pawel-Kranzberg Feb 1, 2022
8687508
Update model_evaluation.rst
Pawel-Kranzberg Feb 1, 2022
13e7f98
Update model_evaluation.rst
Pawel-Kranzberg Feb 1, 2022
1f60a9c
Update model_evaluation.rst
Pawel-Kranzberg Feb 1, 2022
b7b2e0f
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Sep 27, 2022
e2801db
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Sep 27, 2022
713ac2a
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Sep 27, 2022
0d87bf0
Update model_evaluation.rst
Pawel-Kranzberg Sep 27, 2022
3cb2c1c
Update v1.2.rst
Pawel-Kranzberg Sep 27, 2022
f31bd33
Update test_classification.py
Pawel-Kranzberg Sep 27, 2022
ce20ed1
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Sep 27, 2022
75edeed
Update test_classification.py
Pawel-Kranzberg Sep 27, 2022
831d424
Update test_classification.py
Pawel-Kranzberg Sep 27, 2022
221d7ca
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Oct 26, 2022
e63212f
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Nov 3, 2022
41d5359
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Nov 4, 2022
32b0908
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Nov 4, 2022
68c9dc2
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Nov 7, 2022
0d66c27
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Nov 9, 2022
8b29be7
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Nov 10, 2022
7ce9cd5
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Nov 11, 2022
6baffdb
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Nov 11, 2022
27c9f2f
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Nov 12, 2022
c5ad594
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Nov 12, 2022
b0fbdd1
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Nov 13, 2022
5b4b5e8
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Nov 13, 2022
5c5d579
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Nov 14, 2022
0172034
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Nov 16, 2022
c7bc276
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Nov 17, 2022
a0b9cfc
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Nov 18, 2022
6b4cdc3
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Nov 19, 2022
d36b6b1
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Nov 21, 2022
88d4382
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Nov 25, 2022
71f9642
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Nov 28, 2022
23d354e
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Nov 28, 2022
ad868f3
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Jan 17, 2023
1c94949
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Jan 24, 2023
644e9e3
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Jan 26, 2023
843625f
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Feb 2, 2023
f7e7b8b
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Feb 16, 2023
6daa85e
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Mar 5, 2023
52fc7ab
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Mar 15, 2023
ae55a07
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Apr 6, 2023
602a690
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Apr 15, 2023
31bd50c
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg May 26, 2023
b036a50
Update test_classification.py
Pawel-Kranzberg May 26, 2023
5468e46
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Jun 2, 2023
aa8e1cd
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Jun 5, 2023
b175784
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Jun 9, 2023
d51cba2
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Jun 12, 2023
4f89b18
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Jun 15, 2023
452b986
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Jun 16, 2023
1c839e5
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Jun 22, 2023
3660888
Update test_multiclass.py
Pawel-Kranzberg Jun 22, 2023
1787120
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Jun 23, 2023
9826317
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Jul 8, 2023
7e99660
Update v1.4.rst
Pawel-Kranzberg Jul 8, 2023
7f74ff6
Update v1.2.rst
Pawel-Kranzberg Jul 8, 2023
0718030
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Jul 14, 2023
19ec849
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Jul 19, 2023
a56d590
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Jan 4, 2024
4eef595
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Jan 17, 2024
5b7bd15
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Jan 27, 2024
801406e
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Feb 4, 2024
861c59a
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Feb 18, 2024
7df0be9
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Mar 3, 2024
7f14b02
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Mar 10, 2024
8c3aa4e
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Mar 17, 2024
1155f2f
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Apr 20, 2024
9e3fa26
Fix linting in test_classification.py
Pawel-Kranzberg Apr 20, 2024
29769a7
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Apr 28, 2024
86fa01a
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Apr 29, 2024
c4be363
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg May 1, 2024
bdf83b2
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg May 19, 2024
582387c
Fix linting in _classification.py
Pawel-Kranzberg May 19, 2024
4aa4449
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Jun 21, 2024
a41837c
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Sep 30, 2024
918c720
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Mar 18, 2025
7877673
Remove the legacy assert_no_warnings in test_classification.py
Pawel-Kranzberg Mar 18, 2025
9da387c
Convert score values to float in _classification.py
Pawel-Kranzberg Mar 18, 2025
d7e513e
Merge branch 'main' into confusion-matrix-derived-metrics
Pawel-Kranzberg Mar 18, 2025
5ee31a5
Update _classification.py
Pawel-Kranzberg Mar 18, 2025
d4bbd82
Update _classification.py
Pawel-Kranzberg Mar 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 109 additions & 6 deletions doc/modules/model_evaluation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ Scoring string name Function
'precision' etc. :func:`metrics.precision_score` suffixes apply as with 'f1'
'recall' etc. :func:`metrics.recall_score` suffixes apply as with 'f1'
'jaccard' etc. :func:`metrics.jaccard_score` suffixes apply as with 'f1'
'specificity' etc. :func:`metrics.specificity_score` suffixes apply as with 'f1'
'npv' etc. :func:`metrics.npv_score` suffixes apply as with 'f1'
'roc_auc' :func:`metrics.roc_auc_score`
'roc_auc_ovr' :func:`metrics.roc_auc_score`
'roc_auc_ovo' :func:`metrics.roc_auc_score`
Expand Down Expand Up @@ -536,6 +538,8 @@ Some also work in the multilabel case:
precision_recall_fscore_support
precision_score
recall_score
specificity_score
npv_score
roc_auc_score
zero_one_loss
d2_log_loss_score
Expand Down Expand Up @@ -603,7 +607,6 @@ The :func:`accuracy_score` function computes the
`accuracy <https://en.wikipedia.org/wiki/Accuracy_and_precision>`_, either the fraction
(default) or the count (normalize=False) of correct predictions.


In multilabel classification, the function returns the subset accuracy. If
the entire set of predicted labels for a sample strictly match with the true
set of labels, then the subset accuracy is 1.0; otherwise it is 0.0.
Expand Down Expand Up @@ -742,7 +745,7 @@ or *informedness*.

* Our definition: [Mosley2013]_, [Kelleher2015]_ and [Guyon2015]_, where
[Guyon2015]_ adopt the adjusted version to ensure that random predictions
have a score of :math:`0` and perfect predictions have a score of :math:`1`..
have a score of :math:`0` and perfect predictions have a score of :math:`1`.
* Class balanced accuracy as described in [Mosley2013]_: the minimum between the precision
and the recall for each class is computed. Those values are then averaged over the total
number of classes to get the balanced accuracy.
Expand Down Expand Up @@ -855,6 +858,42 @@ false negatives and true positives as follows::
for an example of using a confusion matrix to classify text
documents.

.. _tpr_fpr_tnr_fnr_score:

TPR FPR TNR FNR score
---------------------

The :func:`tpr_fpr_tnr_fnr_score` function computes the true positive rate (TPR),
false positive rate (FPR), true negative rate (TNR) and false negative rate (FNR)
of predictions, based on the `confusion matrix <https://en.wikipedia.org/wiki/Confusion_matrix>`_.
The rates are defined as

.. math::

\texttt{TPR} = \frac{TP}{P}} = \frac{TP}{TP + FN}} = 1 - FNR

\texttt{FPR} = \frac{FP}{N}} = \frac{FP}{TN + FP}} = 1 - TNR

\texttt{TNR} = \frac{TN}{N}} = \frac{TN}{TN + FP}} = 1 - FPR

\texttt{FNR} = \frac{FN}{P}} = \frac{FN}{TP + FN}} = 1 - TPR

>>> from sklearn.metrics import tpr_fpr_tnr_fnr_score
>>> y_true = [2, 0, 2, 2, 0, 1]
>>> y_pred = [0, 0, 2, 2, 0, 2]
>>> tpr_fpr_tnr_fnr_score(y_true, y_pred)
(array([1. , 0. , 0.66666667]),
array([0.25 , 0. , 0.33333333]),
array([0.75 , 1. , 0.66666667]),
array([0. , 1. , 0.33333333]))

.. note::

* True positive rate (TPR) is also called recall, sensitivity, or hit rate.
* False positive rate (FPR) is also called fall-out.
* True negative rate (TNR) is also called specificity, or selectivity.
* false negative rate (FNR) is also called miss rate.

.. _classification_report:

Classification report
Expand Down Expand Up @@ -1006,6 +1045,18 @@ precision-recall curve as follows.
:scale: 75
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am thinking that we should document the function returning the ratios.
I think that we should move the confusion matrix presentation before the accuracy score that use the TP FP TN FR already. Like this we could document the ratio function just after the confusion matrix.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Attempted.

:align: center

Precision can also be referred to as the `positive predictive value (PPV)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am thinking that we could isolate these two metrics in a new section where we can provide more details regarding binary and multiclass case and the effect of the averaging similarly to the precision-recall section

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Attempted.

<https://en.wikipedia.org/wiki/Positive_and_negative_predictive_values>`_,
e.g. in the context of bioscience. A closely related metric is
`negative predictive value (NPV) <https://en.wikipedia.org/wiki/Positive_and_negative_predictive_values>`_
, implemented by the :func:`npv_score`.

Recall can also be called the hit rate, or true positive rate (TPR). Especially
in biostatistics, it is also known as `sensitivity <https://en.wikipedia.org/wiki/Sensitivity_and_specificity>`_
, which is related to `specificity <https://en.wikipedia.org/wiki/Sensitivity_and_specificity>`_.
In turn, specificity is also referred to as selectivity, or true negative rate (TNR),
and is implemented by the :func:`specificity_score`.

.. rubric:: Examples

* See :ref:`sphx_glr_auto_examples_model_selection_plot_grid_search_digits.py`
Expand Down Expand Up @@ -1044,10 +1095,10 @@ following table:
+-------------------+------------------------------------------------+
| | Actual class (observation) |
+-------------------+---------------------+--------------------------+
| Predicted class | tp (true positive) | fp (false positive) |
| Predicted class | TP (true positive) | FP (false positive) |
| (expectation) | Correct result | Unexpected result |
| +---------------------+--------------------------+
| | fn (false negative) | tn (true negative) |
| | FN (false negative) | TN (true negative) |
| | Missing result | Correct absence of result|
+-------------------+---------------------+--------------------------+

Expand Down Expand Up @@ -1117,10 +1168,9 @@ Here are some small examples in binary classification::
>>> average_precision_score(y_true, y_scores)
0.83...



Multiclass and multilabel classification
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

In a multiclass and multilabel classification task, the notions of precision,
recall, and F-measures can be applied to each label independently.
There are a few ways to combine results across labels,
Expand Down Expand Up @@ -1994,6 +2044,59 @@ the same does a lower Brier score loss always mean better calibration"
and probability estimation." <https://drops.dagstuhl.de/opus/volltexte/2008/1382/>`_
Dagstuhl Seminar Proceedings. Schloss Dagstuhl-Leibniz-Zentrum für Informatik (2008).

.. _true_negatives_metrics:

Specificity and negative predictive value (NPV)
-----------------------------------------------

`Specificity <https://en.wikipedia.org/wiki/Sensitivity_and_specificity>`_
(also called selectivity or true negative rate) and
`NPV <https://en.wikipedia.org/wiki/Positive_and_negative_predictive_values>`_
are both ratios of true negatives to, respectively, actual negatives and
predicted negatives in a classification task.

Binary classification
^^^^^^^^^^^^^^^^^^^^^

In a binary classification task, specificity and NPV are defined simply as

..math::

\text{specificity} = \frac{TN}{N}} = \frac{TN}{TN + FP}}

\text{NPV} = \frac{TN}{TN + FN}}

Multiclass and multilabel classification
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

In a multiclass or multilabel classification task, the notions of specificity
and NPV can be applied to each label independently. There are a few ways
to combine results across labels, specified by the ``average`` argument
to the :func:`specificity_score` and :func:`npv_score` functions, as described
:ref:`above <average>`.

To make this more explicit, consider the following examples:
>>> from sklearn.metrics import specificity_score
>>> from sklearn.metrics import npv_score
>>> y_true = [2, 0, 2, 2, 0, 1]
>>> y_pred = [0, 0, 2, 2, 0, 2]
>>> specificity_score(y_true, y_pred, average=None)
array([0.75 , 1. , 0.66666667])
>>> npv_score(y_true, y_pred, average=None)
array([1. , 0.83333333, 0.66666667])
>>> specificity_score(y_true, y_pred, average='macro')
0.805...
>>> npv_score(y_true, y_pred, average='macro')
0.83...
>>> specificity_score(y_true, y_pred, average='micro')
0.83...
>>> npv_score(y_true, y_pred, average='micro')
0.83...
>>> specificity_score(y_true, y_pred, average='weighted')
0.75
>>> npv_score(y_true, y_pred, average='weighted')
0.805...

.. _class_likelihood_ratios:

Class likelihood ratios
Expand Down
6 changes: 6 additions & 0 deletions sklearn/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
log_loss,
matthews_corrcoef,
multilabel_confusion_matrix,
npv_score,
precision_recall_fscore_support,
precision_score,
recall_score,
specificity_score,
tpr_fpr_tnr_fnr_score,
zero_one_loss,
)
from ._dist_metrics import DistanceMetric
Expand Down Expand Up @@ -157,6 +160,7 @@
"nan_euclidean_distances",
"ndcg_score",
"normalized_mutual_info_score",
"npv_score",
"pair_confusion_matrix",
"pairwise_distances",
"pairwise_distances_argmin",
Expand All @@ -175,7 +179,9 @@
"root_mean_squared_log_error",
"silhouette_samples",
"silhouette_score",
"specificity_score",
"top_k_accuracy_score",
"tpr_fpr_tnr_fnr_score",
"v_measure_score",
"zero_one_loss",
]
Loading