-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclassificationmetrics_test.py
188 lines (160 loc) · 7.4 KB
/
classificationmetrics_test.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from math import log
import unittest
from classificationmetrics import ensureEqualLength, indicatorFunction, hingeLoss, binaryCrossEntropy, KullbackLeiblerDivergence, JensenShannonDivergence
class TestEnsureEqualLength(unittest.TestCase):
def test_unequal_length(self):
l1 = [1,0,1,1]
l2 = [0,1]
test = False
try:
result = ensureEqualLength(l1, l2)
except ValueError as inst:
self.assertEqual(inst.args[0], 'Input lists need to be of same length.')
test = True
self.assertTrue(test)
def test_equal_length(self):
l1 = [1,0,1,1]
l2 = [0,1,0,1]
test = False
try:
result = ensureEqualLength(l1, l2)
except ValueError as inst:
self.assertEqual(inst.args[0], 'Input lists need to be of same length.')
test = True
self.assertFalse(test)
class TestIndicator(unittest.TestCase):
def test_fractional_prediction(self):
l1 = [1,0,0.9,1]
l2 = [0,1,0,1]
test = False
try:
result = indicatorFunction(l1, l2)
except ValueError as inst:
self.assertEqual(inst.args[0], 'The predictions must either be 0 or 1.')
test = True
self.assertTrue(test)
def test_large_true_value(self):
l1 = [1,0,0,1]
l2 = [0,1,0,11]
test = False
try:
result = indicatorFunction(l1, l2)
except ValueError as inst:
self.assertEqual(inst.args[0], 'The true values must either be 0 or 1.')
test = True
self.assertTrue(test)
def test_prediction(self):
l1 = [1,0,0,1,0,1,0,0,1,1]
l2 = [0,1,0,1,0,0,0,0,0,1]
expected_result = 0.4
result = indicatorFunction(l1, l2)
self.assertAlmostEqual(expected_result, result)
class TestHingeLoss(unittest.TestCase):
def test_prediction(self):
l1 = [0.1,0,1,0.5,10,1,0.2,0,1.8,1]
l2 = [0.1,1,1,0.7,1,0.9,0.2,0,0.1,1.2]
expected_result = (1 - 0.01 + 1 + 0 + 1 - 0.35 + 0 + 1 - 0.9 + 1 - 0.04 + 1 + 1 - 0.18 + 0)/10
result = hingeLoss(l1, l2)
self.assertAlmostEqual(expected_result, result)
class TestBinaryCrossEntropy(unittest.TestCase):
def test_negative_prediction(self):
l1 = [0.1,0,1,-0.5,1.0]
l2 = [0.1,1,1,0.7,1]
test = False
try:
result = binaryCrossEntropy(l1, l2)
except ValueError as inst:
self.assertEqual(inst.args[0], 'The prediction and true values all need to be between 0 and 1.')
test = True
self.assertTrue(test)
def test_large_true_value(self):
l1 = [0.1,0,1,0.5,1.0]
l2 = [0.1,1,1,7,1]
test = False
try:
result = binaryCrossEntropy(l1, l2)
except ValueError as inst:
self.assertEqual(inst.args[0], 'The prediction and true values all need to be between 0 and 1.')
test = True
self.assertTrue(test)
def test_prediction(self):
l1 = [0.1, 0.1, 0.9, 0.5, 0.9, 0.45, 0.2, 0.1, 0.8, 0.11]
l2 = [0, 1, 1, 0.7, 1, 0.9, 0, 0, 0.1, 0.2]
expected_result = (0.105360516 + 2.30258509 + 0.105360516 + 0.693147181 + 0.105360516 + 0.778440627 + 0.223143551 + 0.105360516 + 1.47080848 + 0.534682036)/10
result = binaryCrossEntropy(l1, l2)
self.assertAlmostEqual(expected_result, result)
def test_zeroes_and_ones(self):
l1 = [0, 0.1, 0, 0.5, 1, 0.45, 0.2, 1, 0.8, 0.11]
l2 = [0, 1, 1, 0.7, 1, 0.9, 0, 0, 0.1, 0.2]
expected_result = (0 + 2.30258509 + 23.0258509 + 0.693147181 + 0 + 0.778440627 + 0.223143551 + 23.0258509 + 1.47080848 + 0.534682036)/10
result = binaryCrossEntropy(l1, l2)
self.assertAlmostEqual(expected_result, result)
class TestKullbackLeiblerDivergence(unittest.TestCase):
def test_negative_true_value(self):
l1 = [0.1,1,1,0.7,1]
l2 = [0.1,0,1,-0.1,1.0]
test = False
try:
result = KullbackLeiblerDivergence(l1, l2)
except ValueError as inst:
self.assertEqual(inst.args[0], 'The prediction and true values all need to be between 0 and 1.')
test = True
self.assertTrue(test)
def test_large_prediction(self):
l1 = [0.1,1,1,1.1,1]
l2 = [0.1,0,1,0.5,1.0]
test = False
try:
result = KullbackLeiblerDivergence(l1, l2)
except ValueError as inst:
self.assertEqual(inst.args[0], 'The prediction and true values all need to be between 0 and 1.')
test = True
self.assertTrue(test)
def test_prediction(self):
l1 = [0.1, 0.1, 0.9, 0.5, 0.9, 0.45, 0.2, 0.1, 0.8, 0.11]
l2 = [0.1, 0.7, 0.5, 0.1, 0.8, 0.63, 0.01, 0.15, 0.1, 0.2]
expected_result = (0.0 + 1.0325534177382862 + 0.5108256237659907 + 0.3680642071684971 + 0.04440300758688234 + 0.06530385821371285 + 0.18100496057056117 + 0.01223511445226829 + 1.1457255029306632 + 0.03427961210451759)/10
result = KullbackLeiblerDivergence(l1, l2)
self.assertAlmostEqual(expected_result, result)
def test_zeroes_and_ones(self):
l1 = [0, 0.1, 0, 0.5, 1, 0.45, 0.2, 1, 0.8, 0.11]
l2 = [0, 1, 1, 0.7, 1, 0.9, 0, 0, 0.1, 0.2]
expected_result = (0.0 + 2.302585092994046 + 23.025850929940457 + 0.08228287850505178 + 0.0 + 0.45335765328010824 + 0.22314355131420976 + 23.025850930040455 + 1.1457255029306632 + 0.03427961210451759)/10
result = KullbackLeiblerDivergence(l1, l2)
self.assertAlmostEqual(expected_result, result)
class TestJensenShannonDivergence(unittest.TestCase):
def test_negative_true_value(self):
l1 = [0.1,1,1,0.7,1]
l2 = [0.1,0,1,-0.1,1.0]
test = False
try:
result = JensenShannonDivergence(l1, l2)
except ValueError as inst:
self.assertEqual(inst.args[0], 'The prediction and true values all need to be between 0 and 1.')
test = True
self.assertTrue(test)
def test_large_prediction(self):
l1 = [0.1,1,1,1.1,1]
l2 = [0.1,0,1,0.5,1.0]
test = False
try:
result = JensenShannonDivergence(l1, l2)
except ValueError as inst:
self.assertEqual(inst.args[0], 'The prediction and true values all need to be between 0 and 1.')
test = True
self.assertTrue(test)
def test_prediction(self):
l1 = [0.1, 0.1, 0.9, 0.5, 0.9, 0.45, 0.2, 0.1, 0.8, 0.11]
l2 = [0.1, 0.7, 0.5, 0.1, 0.8, 0.63, 0.01, 0.15, 0.1, 0.2]
expected_result = (0.0 + 0.20503802928608553 + 0.10174922507919676 + 0.10174922507919676 + 0.009966389341172874 + 0.01639651126007382 + 0.057730235413083975 + 0.002874130657717279 + 0.2753961152487704 + 0.00782605551441497)/10
result = JensenShannonDivergence(l1, l2)
self.assertAlmostEqual(expected_result, result)
def test_zeroes_and_ones(self):
l1 = [0, 0.1, 0, 0.5, 1, 0.45, 0.2, 1, 0.8, 0.11]
l2 = [0, 1, 1, 0.7, 1, 0.9, 0, 0, 0.1, 0.2]
expected_result = (0.0 + 0.5255973270178643 + 0.6931471805599453 + 0.021005925701837062 + 0.0 + 0.12397013483349642 + 0.07488176162235435 + 0.6931471805599453 + 0.2753961152487704 + 0.00782605551441497)/10
result = JensenShannonDivergence(l1, l2)
self.assertAlmostEqual(expected_result, result)
if __name__ == '__main__':
# Run tests
unittest.main()