Spaces:
Running
Running
Achilleas Pasias
commited on
Commit
•
d7b729a
1
Parent(s):
ed3e3bb
Confusion Matrix
Browse files- README.md +104 -1
- app.py +6 -0
- confusion_matrix.py +149 -0
README.md
CHANGED
@@ -8,5 +8,108 @@ sdk_version: 3.17.0
|
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
|
|
|
|
|
|
11 |
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
11 |
+
tags:
|
12 |
+
- evaluate
|
13 |
+
- metric
|
14 |
|
15 |
+
description: >-
|
16 |
+
Accuracy is the proportion of correct predictions among the total number of cases processed. It can be computed with:
|
17 |
+
Accuracy = (TP + TN) / (TP + TN + FP + FN)
|
18 |
+
Where:
|
19 |
+
TP: True positive
|
20 |
+
TN: True negative
|
21 |
+
FP: False positive
|
22 |
+
FN: False negative
|
23 |
+
---
|
24 |
+
|
25 |
+
# Metric Card for Confusion Matrix
|
26 |
+
|
27 |
+
|
28 |
+
## Metric Description
|
29 |
+
|
30 |
+
Compute confusion matrix to evaluate the accuracy of a classification.
|
31 |
+
By definition a confusion matrix :math:`C` is such that :math:`C_{i, j}`
|
32 |
+
is equal to the number of observations known to be in group :math:`i` and
|
33 |
+
predicted to be in group :math:`j`.
|
34 |
+
|
35 |
+
Thus in binary classification, the count of true negatives is
|
36 |
+
:math:`C_{0,0}`, false negatives is :math:`C_{1,0}`, true positives is
|
37 |
+
:math:`C_{1,1}` and false positives is :math:`C_{0,1}`.
|
38 |
+
|
39 |
+
|
40 |
+
## How to Use
|
41 |
+
|
42 |
+
At minimum, this metric requires predictions and references as inputs.
|
43 |
+
|
44 |
+
```python
|
45 |
+
>>> cfm_metric = evaluate.load("confusion_matrix")
|
46 |
+
>>> results = cfm_metric.compute(references=[1, 2, 3, 2, 1, 1, 0, 2], predictions=[1, 0, 3, 2, 2, 1, 0, 3])
|
47 |
+
>>> print(results)
|
48 |
+
{'confusion_matrix': [[1, 0, 0, 0], [0, 2, 1, 0], [1, 0, 1, 1], [0, 0, 0, 1]]}
|
49 |
+
```
|
50 |
+
|
51 |
+
|
52 |
+
### Inputs
|
53 |
+
- **predictions** (`list` of `int`): Predicted labels.
|
54 |
+
- **references** (`list` of `int`): Ground truth labels.
|
55 |
+
- **normalize** (`str` or `None`): {`true`, `pred`, `all`}, default=None
|
56 |
+
Normalizes confusion matrix over the true (rows), predicted (columns)
|
57 |
+
conditions or all the population. If None, confusion matrix will not be
|
58 |
+
normalized
|
59 |
+
- **sample_weight** (`list` of `float`): Sample weights Defaults to None.
|
60 |
+
- **labels** (`list` of `float`): default=None
|
61 |
+
List of labels to index the matrix. This may be used to reorder
|
62 |
+
or select a subset of labels.
|
63 |
+
If ``None`` is given, those that appear at least once
|
64 |
+
in ``y_true`` or ``y_pred`` are used in sorted order.
|
65 |
+
|
66 |
+
### Output Values
|
67 |
+
- **confusion_matrix**(`list` of `int`): Confusion matrix. Minimum possible value is 0. Maximum possible value is 1.0, or the number of examples input, if `normalize` is set to `True`.. A higher score means higher accuracy.
|
68 |
+
Output Example(s):
|
69 |
+
```python
|
70 |
+
{'confusion_matrix': [[1, 0, 0, 0], [0, 2, 1, 0], [1, 0, 1, 1], [0, 0, 0, 1]]}
|
71 |
+
|
72 |
+
```
|
73 |
+
This metric outputs a dictionary, containing the confusion matrix.
|
74 |
+
|
75 |
+
### Examples
|
76 |
+
>>> from sklearn.metrics import confusion_matrix
|
77 |
+
>>> y_true = [2, 0, 2, 2, 0, 1]
|
78 |
+
>>> y_pred = [0, 0, 2, 2, 0, 2]
|
79 |
+
>>> confusion_matrix(y_true, y_pred)
|
80 |
+
array([[2, 0, 0],
|
81 |
+
[0, 0, 1],
|
82 |
+
[1, 0, 2]])
|
83 |
+
|
84 |
+
>>> y_true = ["cat", "ant", "cat", "cat", "ant", "bird"]
|
85 |
+
>>> y_pred = ["ant", "ant", "cat", "cat", "ant", "cat"]
|
86 |
+
>>> confusion_matrix(y_true, y_pred, labels=["ant", "bird", "cat"])
|
87 |
+
array([[2, 0, 0],
|
88 |
+
[0, 0, 1],
|
89 |
+
[1, 0, 2]])
|
90 |
+
|
91 |
+
In the binary case, we can extract true positives, etc as follows:
|
92 |
+
|
93 |
+
>>> tn, fp, fn, tp = confusion_matrix([0, 1, 0, 1], [1, 1, 1, 0]).ravel()
|
94 |
+
>>> (tn, fp, fn, tp)
|
95 |
+
(0, 2, 1, 1)
|
96 |
+
|
97 |
+
## Citation(s)
|
98 |
+
```bibtex
|
99 |
+
@article{scikit-learn,
|
100 |
+
title={Scikit-learn: Machine Learning in {P}ython},
|
101 |
+
author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.
|
102 |
+
and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.
|
103 |
+
and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and
|
104 |
+
Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},
|
105 |
+
journal={Journal of Machine Learning Research},
|
106 |
+
volume={12},
|
107 |
+
pages={2825--2830},
|
108 |
+
year={2011}
|
109 |
+
}
|
110 |
+
```
|
111 |
+
## Further References
|
112 |
+
Wikipedia entry for the Confusion matrix
|
113 |
+
<https://en.wikipedia.org/wiki/Confusion_matrix>`_
|
114 |
+
(Wikipedia and other references may use a different
|
115 |
+
convention for axes).
|
app.py
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import evaluate
|
2 |
+
from evaluate.utils import launch_gradio_widget
|
3 |
+
|
4 |
+
|
5 |
+
module = evaluate.load("confusion_matrix")
|
6 |
+
launch_gradio_widget(module)
|
confusion_matrix.py
ADDED
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Confusion Matrix metric."""
|
2 |
+
|
3 |
+
import datasets
|
4 |
+
import evaluate
|
5 |
+
from sklearn.metrics import confusion_matrix
|
6 |
+
|
7 |
+
_DESCRIPTION = """
|
8 |
+
Compute confusion matrix to evaluate the accuracy of a classification.
|
9 |
+
By definition a confusion matrix :math:`C` is such that :math:`C_{i, j}`
|
10 |
+
is equal to the number of observations known to be in group :math:`i` and
|
11 |
+
predicted to be in group :math:`j`.
|
12 |
+
|
13 |
+
Thus in binary classification, the count of true negatives is
|
14 |
+
:math:`C_{0,0}`, false negatives is :math:`C_{1,0}`, true positives is
|
15 |
+
:math:`C_{1,1}` and false positives is :math:`C_{0,1}`.
|
16 |
+
|
17 |
+
Read more in the :ref:`User Guide <confusion_matrix>`.
|
18 |
+
"""
|
19 |
+
|
20 |
+
|
21 |
+
_KWARGS_DESCRIPTION = """
|
22 |
+
Args:
|
23 |
+
|
24 |
+
y_true : array-like of shape (n_samples,)
|
25 |
+
Ground truth (correct) target values.
|
26 |
+
|
27 |
+
y_pred : array-like of shape (n_samples,)
|
28 |
+
Estimated targets as returned by a classifier.
|
29 |
+
|
30 |
+
labels : array-like of shape (n_classes), default=None
|
31 |
+
List of labels to index the matrix. This may be used to reorder
|
32 |
+
or select a subset of labels.
|
33 |
+
If ``None`` is given, those that appear at least once
|
34 |
+
in ``y_true`` or ``y_pred`` are used in sorted order.
|
35 |
+
|
36 |
+
sample_weight : array-like of shape (n_samples,), default=None
|
37 |
+
Sample weights.
|
38 |
+
|
39 |
+
.. versionadded:: 0.18
|
40 |
+
|
41 |
+
normalize : {'true', 'pred', 'all'}, default=None
|
42 |
+
Normalizes confusion matrix over the true (rows), predicted (columns)
|
43 |
+
conditions or all the population. If None, confusion matrix will not be
|
44 |
+
normalized.
|
45 |
+
|
46 |
+
Returns:
|
47 |
+
|
48 |
+
C : ndarray of shape (n_classes, n_classes)
|
49 |
+
Confusion matrix whose i-th row and j-th
|
50 |
+
column entry indicates the number of
|
51 |
+
samples with true label being i-th class
|
52 |
+
and predicted label being j-th class.
|
53 |
+
|
54 |
+
See Also:
|
55 |
+
|
56 |
+
ConfusionMatrixDisplay.from_estimator : Plot the confusion matrix
|
57 |
+
given an estimator, the data, and the label.
|
58 |
+
ConfusionMatrixDisplay.from_predictions : Plot the confusion matrix
|
59 |
+
given the true and predicted labels.
|
60 |
+
ConfusionMatrixDisplay : Confusion Matrix visualization.
|
61 |
+
|
62 |
+
References:
|
63 |
+
|
64 |
+
.. [1] `Wikipedia entry for the Confusion matrix
|
65 |
+
<https://en.wikipedia.org/wiki/Confusion_matrix>`_
|
66 |
+
(Wikipedia and other references may use a different
|
67 |
+
convention for axes).
|
68 |
+
|
69 |
+
Examples:
|
70 |
+
|
71 |
+
>>> from sklearn.metrics import confusion_matrix
|
72 |
+
>>> y_true = [2, 0, 2, 2, 0, 1]
|
73 |
+
>>> y_pred = [0, 0, 2, 2, 0, 2]
|
74 |
+
>>> confusion_matrix(y_true, y_pred)
|
75 |
+
array([[2, 0, 0],
|
76 |
+
[0, 0, 1],
|
77 |
+
[1, 0, 2]])
|
78 |
+
|
79 |
+
>>> y_true = ["cat", "ant", "cat", "cat", "ant", "bird"]
|
80 |
+
>>> y_pred = ["ant", "ant", "cat", "cat", "ant", "cat"]
|
81 |
+
>>> confusion_matrix(y_true, y_pred, labels=["ant", "bird", "cat"])
|
82 |
+
array([[2, 0, 0],
|
83 |
+
[0, 0, 1],
|
84 |
+
[1, 0, 2]])
|
85 |
+
|
86 |
+
In the binary case, we can extract true positives, etc as follows:
|
87 |
+
|
88 |
+
>>> tn, fp, fn, tp = confusion_matrix([0, 1, 0, 1], [1, 1, 1, 0]).ravel()
|
89 |
+
>>> (tn, fp, fn, tp)
|
90 |
+
(0, 2, 1, 1)
|
91 |
+
"""
|
92 |
+
|
93 |
+
|
94 |
+
_CITATION = """
|
95 |
+
@article{scikit-learn,
|
96 |
+
title={Scikit-learn: Machine Learning in {P}ython},
|
97 |
+
author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.
|
98 |
+
and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.
|
99 |
+
and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and
|
100 |
+
Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},
|
101 |
+
journal={Journal of Machine Learning Research},
|
102 |
+
volume={12},
|
103 |
+
pages={2825--2830},
|
104 |
+
year={2011}
|
105 |
+
}
|
106 |
+
"""
|
107 |
+
|
108 |
+
|
109 |
+
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
|
110 |
+
class ConfusionMatrix(evaluate.Metric):
|
111 |
+
def _info(self):
|
112 |
+
return evaluate.MetricInfo(
|
113 |
+
description=_DESCRIPTION,
|
114 |
+
citation=_CITATION,
|
115 |
+
inputs_description=_KWARGS_DESCRIPTION,
|
116 |
+
features=datasets.Features(
|
117 |
+
{
|
118 |
+
"predictions": datasets.Sequence(datasets.Value("int32")),
|
119 |
+
"references": datasets.Sequence(datasets.Value("int32")),
|
120 |
+
}
|
121 |
+
if self.config_name == "multilabel"
|
122 |
+
else {
|
123 |
+
"predictions": datasets.Value("int32"),
|
124 |
+
"references": datasets.Value("int32"),
|
125 |
+
}
|
126 |
+
),
|
127 |
+
reference_urls=[
|
128 |
+
"https://scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html"
|
129 |
+
],
|
130 |
+
)
|
131 |
+
|
132 |
+
def _compute(
|
133 |
+
self,
|
134 |
+
predictions,
|
135 |
+
references,
|
136 |
+
*,
|
137 |
+
labels=None,
|
138 |
+
sample_weight=None,
|
139 |
+
normalize=None
|
140 |
+
):
|
141 |
+
return {
|
142 |
+
"confusion_matrix": confusion_matrix(
|
143 |
+
y_true=references,
|
144 |
+
y_pred=predictions,
|
145 |
+
labels=labels,
|
146 |
+
sample_weight=sample_weight,
|
147 |
+
normalize=normalize,
|
148 |
+
).tolist()
|
149 |
+
}
|