confusion_matrix 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE.rdoc +22 -22
- data/README.rdoc +88 -88
- data/lib/confusion_matrix.rb +383 -451
- data/test/matrix_test.rb +160 -160
- metadata +10 -15
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7c054f9ff8a3aed4fc0cef535cb2506aec2d1928ac5b944ec1d6687f6691fce1
|
|
4
|
+
data.tar.gz: 56a11a2175d65f9a54dc8041f7b7f974abb6e63b3d201b6ed9d81f76b6957765
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9003d2e3f983cf3403680384ea1addc416e518afec979f3def0913ad60d51b760627eaefd8d52459723683042fc3cf57b120e624c95d023e0e87dbb2022f7ad6
|
|
7
|
+
data.tar.gz: 8c218ba99d31d456934fbbb0ae866b703503388f3a668f4d89eb2f05a380bda77f820c0b4b08e253f8ae6eca4e16e7857f6735717d9c06cd0a5ec29dd4ca49d8
|
data/LICENSE.rdoc
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
= MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2020-23, Peter Lane
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
22
|
-
|
|
1
|
+
= MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020-23, Peter Lane
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
data/README.rdoc
CHANGED
|
@@ -1,88 +1,88 @@
|
|
|
1
|
-
= Confusion Matrix
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
are
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
cm
|
|
66
|
-
cm.add_for(:pos, :
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
puts "
|
|
72
|
-
puts "
|
|
73
|
-
puts
|
|
74
|
-
puts
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
1
|
+
= Confusion Matrix
|
|
2
|
+
|
|
3
|
+
Available from:
|
|
4
|
+
- RubyGems: https://rubygems.org/gems/confusion_matrix/
|
|
5
|
+
- Source: https://codeberg.org/peterlane/confusion-matrix/
|
|
6
|
+
|
|
7
|
+
== Description
|
|
8
|
+
|
|
9
|
+
A confusion matrix represents "the relative frequencies with which each of a
|
|
10
|
+
number of stimuli is mistaken for each of the others by a person in a task
|
|
11
|
+
requiring recognition or identification of stimuli" (R. Colman,
|
|
12
|
+
<i>A Dictionary of Psychology</i>, 2008). Each row represents the predicted label of an instance, and
|
|
13
|
+
each column represents the observed label of that instance. Numbers at each
|
|
14
|
+
(row, column) reflect the total number of instances of predicted label "row"
|
|
15
|
+
which were observed as having label "column".
|
|
16
|
+
|
|
17
|
+
A two-label example is:
|
|
18
|
+
|
|
19
|
+
Observed Observed |
|
|
20
|
+
Positive Negative | Predicted
|
|
21
|
+
------------------------------+------------
|
|
22
|
+
a b | Positive
|
|
23
|
+
c d | Negative
|
|
24
|
+
|
|
25
|
+
Here the value:
|
|
26
|
+
|
|
27
|
+
a:: is the number of true positives (those predicted positive and observed positive)
|
|
28
|
+
b:: is the number of false negatives (those predicted positive but observed negative)
|
|
29
|
+
c:: is the number of false positives (those predicted negative but observed positive)
|
|
30
|
+
d:: is the number of true negatives (those predicted negative and observed negative)
|
|
31
|
+
|
|
32
|
+
From this matrix we can calculate statistics like:
|
|
33
|
+
|
|
34
|
+
true positive rate:: a/(a+b)
|
|
35
|
+
positive recall:: a/(a+c)
|
|
36
|
+
|
|
37
|
+
The implementation supports confusion matrices with more than two
|
|
38
|
+
labels, and hence most statistics are calculated with reference to a
|
|
39
|
+
named label. When more than two labels are in use, the statistics
|
|
40
|
+
are calculated as if the named label were positive and all the other
|
|
41
|
+
labels are grouped as if negative.
|
|
42
|
+
|
|
43
|
+
For example, in a three-label example:
|
|
44
|
+
|
|
45
|
+
Observed Observed Observed |
|
|
46
|
+
Red Blue Green | Predicted
|
|
47
|
+
--------------------------------------------+------------
|
|
48
|
+
a b c | Red
|
|
49
|
+
d e f | Blue
|
|
50
|
+
g h i | Green
|
|
51
|
+
|
|
52
|
+
We can calculate:
|
|
53
|
+
|
|
54
|
+
true red rate:: a/(a+b+c)
|
|
55
|
+
red recall:: a/(a+d+g)
|
|
56
|
+
|
|
57
|
+
== Example
|
|
58
|
+
|
|
59
|
+
The following example creates a two-label confusion matrix,
|
|
60
|
+
prints a few statistics and displays the matrix as a table.
|
|
61
|
+
|
|
62
|
+
require 'confusion_matrix'
|
|
63
|
+
|
|
64
|
+
cm = ConfusionMatrix.new :pos, :neg
|
|
65
|
+
cm.add_for(:pos, :pos, 10)
|
|
66
|
+
3.times { cm.add_for(:pos, :neg) }
|
|
67
|
+
20.times { cm.add_for(:neg, :neg) }
|
|
68
|
+
5.times { cm.add_for(:neg, :pos) }
|
|
69
|
+
|
|
70
|
+
puts "Precision: #{cm.precision}"
|
|
71
|
+
puts "Recall: #{cm.recall}"
|
|
72
|
+
puts "MCC: #{cm.matthews_correlation}"
|
|
73
|
+
puts
|
|
74
|
+
puts(cm.to_s)
|
|
75
|
+
|
|
76
|
+
Output:
|
|
77
|
+
|
|
78
|
+
Precision: 0.6666666666666666
|
|
79
|
+
Recall: 0.7692307692307693
|
|
80
|
+
MCC: 0.5524850114241865
|
|
81
|
+
|
|
82
|
+
Observed |
|
|
83
|
+
pos neg | Predicted
|
|
84
|
+
---------+----------
|
|
85
|
+
10 3 | pos
|
|
86
|
+
5 20 | neg
|
|
87
|
+
|
|
88
|
+
|