-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabc.py
More file actions
85 lines (63 loc) · 2.02 KB
/
abc.py
File metadata and controls
85 lines (63 loc) · 2.02 KB
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
import math
from abc import *
class Base(ABC):
def __init__(self, data, result):
self.data = data
self.result = result
@abstractmethod
def get_answer(self):
pass
@abstractmethod
def get_score(self):
pass
@abstractmethod
def get_loss(self):
pass
class A(Base):
#def __init__(self, data, result):
# self.data = data
# self.result = result
def get_answer(self):
return [int(x >= 0.5) for x in self.data]
def get_score(self):
ans = self.get_answer()
return sum([int(x == y) for (x, y) in zip(ans, self.result)]) \
/ len(ans)
def get_loss(self):
return sum(
[(x - y) * (x - y) for (x, y) in zip(self.data, self.result)])
class B(Base):
#def __init__(self, data, result):
# self.data = data
# self.result = result
def get_answer(self):
return [int(x >= 0.5) for x in self.data]
def get_loss(self):
return -sum([
y * math.log(x) + (1 - y) * math.log(1 - x)
for (x, y) in zip(self.data, self.result)
])
def get_pre(self):
ans = self.get_answer()
res = [int(x == 1 and y == 1) for (x, y) in zip(ans, self.result)]
return sum(res) / sum(ans)
def get_rec(self):
ans = self.get_answer()
res = [int(x == 1 and y == 1) for (x, y) in zip(ans, self.result)]
return sum(res) / sum(self.result)
def get_score(self):
pre = self.get_pre()
rec = self.get_rec()
return 2 * pre * rec / (pre + rec)
class C(Base):
#def __init__(self, data, result):
# self.data = data
# self.result = result
def get_answer(self):
return [int(x >= 0.5) for x in self.data]
def get_score(self):
ans = self.get_answer()
return sum([int(x == y) for (x, y) in zip(ans, self.result)]) \
/ len(ans)
def get_loss(self):
return sum([abs(x - y) for (x, y) in zip(self.data, self.result)])