forked from dice-group/Ontolearn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.py
143 lines (103 loc) · 4.24 KB
/
metrics.py
1
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# -----------------------------------------------------------------------------
# MIT License
#
# Copyright (c) 2024 Ontolearn Team
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# -----------------------------------------------------------------------------
"""Quality metrics for concept learners."""
from typing import Final, Tuple
from .abstracts import AbstractScorer
class Recall(AbstractScorer):
"""Recall quality function.
Attribute:
name: name of the metric = 'Recall'.
"""
__slots__ = ()
name: Final = 'Recall'
def score2(self, tp: int, fn: int, fp: int, tn: int) -> Tuple[bool, float]:
try:
recall = tp / (tp + fn)
return True, round(recall, 5)
except ZeroDivisionError:
return False, 0
class Precision(AbstractScorer):
"""Precision quality function.
Attribute:
name: name of the metric = 'Precision'.
"""
__slots__ = ()
name: Final = 'Precision'
def score2(self, tp: int, fn: int, fp: int, tn: int) -> Tuple[bool, float]:
try:
precision = tp / (tp + fp)
return True, round(precision, 5)
except ZeroDivisionError:
return False, 0
class F1(AbstractScorer):
"""F1-score quality function.
Attribute:
name: name of the metric = 'F1'.
"""
__slots__ = ()
name: Final = 'F1'
def score2(self, tp: int, fn: int, fp: int, tn: int) -> Tuple[bool, float]:
try:
recall = tp / (tp + fn)
except ZeroDivisionError:
return False, 0
try:
precision = tp / (tp + fp)
except ZeroDivisionError:
return False, 0
if precision == 0 or recall == 0:
return False, 0
f_1 = 2 * ((precision * recall) / (precision + recall))
return True, round(f_1, 5)
class Accuracy(AbstractScorer):
"""
Accuracy quality function.
Accuracy is acc = (tp + tn) / (tp + tn + fp+ fn).
However, Concept learning papers (e.g. Learning OWL Class expression) appear to invent their own accuracy metrics.
In OCEL => Accuracy of a concept = 1 - ( \\|E^+ \\ R(C)\\|+ \\|E^- AND R(C)\\|) / \\|E\\|).
In CELOE => Accuracy of a concept C = 1 - ( \\|R(A) \\ R(C)\\| + \\|R(C) \\ R(A)\\|)/n.
1) R(.) is the retrieval function, A is the class to describe and C in CELOE.
2) E^+ and E^- are the positive and negative examples probided. E = E^+ OR E^- .
Attribute:
name: name of the metric = 'Accuracy'.
"""
__slots__ = ()
name: Final = 'Accuracy'
def score2(self, tp: int, fn: int, fp: int, tn: int) -> Tuple[bool, float]:
acc = (tp + tn) / (tp + tn + fp + fn)
# acc = 1 - ((fp + fn) / len(self.pos) + len(self.neg)) # from Learning OWL Class Expressions.
return True, round(acc, 5)
class WeightedAccuracy(AbstractScorer):
"""
WeightedAccuracy quality function.
Attribute:
name: name of the metric = 'WeightedAccuracy'.
"""
__slots__ = ()
name: Final = 'WeightedAccuracy'
def score2(self, tp: int, fn: int, fp: int, tn: int) -> Tuple[bool, float]:
ap = tp + fn
an = fp + tn
wacc = ((tp/ap) + (tn/an)) / ((tp/ap) + (tn/an) + (fp/an) + (fn/ap))
return True, round(wacc, 5)