forked from biolab/orange3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_naive_bayes.py
335 lines (288 loc) · 13.4 KB
/
test_naive_bayes.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# Test methods with long descriptive names can omit docstrings
# pylint: disable=missing-docstring
import unittest
from unittest.mock import Mock
import warnings
import numpy as np
import scipy.sparse as sp
from Orange.classification import NaiveBayesLearner
from Orange.data import Table, Domain, DiscreteVariable, ContinuousVariable
from Orange.evaluation import CrossValidation, CA
# This class is used to force predict_storage to fall back to the slower
# procedure instead of calling `predict`
from Orange.tests import test_filename
class NotATable(Table): # pylint: disable=too-many-ancestors,abstract-method
pass
class TestNaiveBayesLearner(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.data = data = Table('titanic')
cls.learner = NaiveBayesLearner()
cls.table = data[::20]
def setUp(self):
self.model = self.learner(self.data)
def test_NaiveBayes(self):
results = CrossValidation(self.table, [self.learner], k=10)
ca = CA(results)
self.assertGreater(ca, 0.7)
self.assertLess(ca, 0.9)
results = CrossValidation(Table("iris"), [self.learner], k=10)
ca = CA(results)
self.assertGreater(ca, 0.7)
def test_degenerate(self):
d = Domain((ContinuousVariable(name="A"),
ContinuousVariable(name="B"),
ContinuousVariable(name="C")),
DiscreteVariable(name="CLASS", values=["M", "F"]))
t = Table(d, [[0, 1, 0, 0], [0, 1, 0, 1], [0, 1, 0, 1]])
nb = NaiveBayesLearner()
model = nb(t)
self.assertEqual(model.domain.attributes, ())
self.assertEqual(model(t[0]), 1)
self.assertTrue(all(model(t) == 1))
def test_allnan_cv(self):
# GH 2740
data = Table(test_filename('datasets/lenses.tab'))
results = CrossValidation(data, [self.learner])
self.assertFalse(any(results.failed))
def test_prediction_routing(self):
data = self.data
predict = self.model.predict = Mock(return_value=(data.Y, None))
self.model(data)
predict.assert_called()
predict.reset_mock()
self.model(data.X)
predict.assert_called()
predict.reset_mock()
self.model.predict_storage(data)
predict.assert_called()
predict.reset_mock()
self.model.predict_storage(data[0])
predict.assert_called()
def test_compare_results_of_predict_and_predict_storage(self):
data2 = NotATable("titanic")
self.model = self.learner(self.data[:50])
predict = self.model.predict = Mock(side_effect=self.model.predict)
values, probs = self.model.predict_storage(self.data[50:])
predict.assert_called()
predict.reset_mock()
values2, probs2 = self.model.predict_storage(data2[50:])
predict.assert_not_called()
np.testing.assert_equal(values, values2)
np.testing.assert_equal(probs, probs2)
def test_predictions(self):
self._test_predictions(sparse=None)
self._test_predictions_with_absent_class(sparse=None)
def test_predictions_csr_matrix(self):
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", ".*the matrix subclass.*", PendingDeprecationWarning)
self._test_predictions(sparse=sp.csr_matrix)
self._test_predictions_with_absent_class(sparse=sp.csr_matrix)
def test_predictions_csc_matrix(self):
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", ".*the matrix subclass.*", PendingDeprecationWarning)
self._test_predictions(sparse=sp.csc_matrix)
self._test_predictions_with_absent_class(sparse=sp.csc_matrix)
def _test_predictions(self, sparse):
x = np.array([
[1, 0, 0],
[0, np.nan, 0],
[0, 1, 0],
[0, 0, 0],
[1, 2, 0],
[1, 1, 0],
[1, 2, 0],
[0, 1, 0]])
if sparse is not None:
x = sparse(x)
y = np.array([0, 0, 0, 1, 1, 1, 2, 2])
domain = Domain(
[DiscreteVariable("a", values="ab"),
DiscreteVariable("b", values="abc"),
DiscreteVariable("c", values="a")],
DiscreteVariable("y", values="abc"))
data = Table.from_numpy(domain, x, y)
model = self.learner(data)
np.testing.assert_almost_equal(
model.class_prob,
[4/11, 4/11, 3/11]
)
np.testing.assert_almost_equal(
np.exp(model.log_cont_prob[0]) * model.class_prob[:, None],
[[3/7, 2/7], [2/7, 3/7], [2/7, 2/7]])
np.testing.assert_almost_equal(
np.exp(model.log_cont_prob[1]) * model.class_prob[:, None],
[[2/5, 1/3, 1/5], [2/5, 1/3, 2/5], [1/5, 1/3, 2/5]])
np.testing.assert_almost_equal(
np.exp(model.log_cont_prob[2]) * model.class_prob[:, None],
[[4/11], [4/11], [3/11]])
test_x = np.array([[a, b, 0] for a in [0, 1] for b in [0, 1, 2]])
# Classifiers reject csc matrices in the base class
# Naive bayesian classifier supports them if predict_storage is
# called directly, which we do below
if sparse is not None and sparse is not sp.csc_matrix:
test_x = sparse(test_x)
test_y = np.full((6, ), np.nan)
# The following was computed manually, too
exp_probs = np.array([
[0.47368421052632, 0.31578947368421, 0.21052631578947],
[0.39130434782609, 0.26086956521739, 0.34782608695652],
[0.24324324324324, 0.32432432432432, 0.43243243243243],
[0.31578947368421, 0.47368421052632, 0.21052631578947],
[0.26086956521739, 0.39130434782609, 0.34782608695652],
[0.15000000000000, 0.45000000000000, 0.40000000000000]
])
# Test the faster algorithm for Table (numpy matrices)
test_data = Table.from_numpy(domain, test_x, test_y)
probs = model(test_data, ret=model.Probs)
np.testing.assert_almost_equal(exp_probs, probs)
values = model(test_data)
np.testing.assert_equal(values, np.argmax(exp_probs, axis=1))
values, probs = model(test_data, ret=model.ValueProbs)
np.testing.assert_almost_equal(exp_probs, probs)
np.testing.assert_equal(values, np.argmax(exp_probs, axis=1))
# Test the slower algorithm for non-Table data (iteration in Python)
test_data = NotATable.from_numpy(domain, test_x, test_y)
probs = model(test_data, ret=model.Probs)
np.testing.assert_almost_equal(exp_probs, probs)
values = model(test_data)
np.testing.assert_equal(values, np.argmax(exp_probs, axis=1))
values, probs = model(test_data, ret=model.ValueProbs)
np.testing.assert_almost_equal(exp_probs, probs)
np.testing.assert_equal(values, np.argmax(exp_probs, axis=1))
# Test prediction directly on numpy
probs = model(test_x, ret=model.Probs)
np.testing.assert_almost_equal(exp_probs, probs)
values = model(test_x)
np.testing.assert_equal(values, np.argmax(exp_probs, axis=1))
values, probs = model(test_x, ret=model.ValueProbs)
np.testing.assert_almost_equal(exp_probs, probs)
np.testing.assert_equal(values, np.argmax(exp_probs, axis=1))
# Test prediction on instances
for inst, exp_prob in zip(test_data, exp_probs):
np.testing.assert_almost_equal(
model(inst, ret=model.Probs),
exp_prob)
self.assertEqual(model(inst), np.argmax(exp_prob))
value, prob = model(inst, ret=model.ValueProbs)
np.testing.assert_almost_equal(prob, exp_prob)
self.assertEqual(value, np.argmax(exp_prob))
# Test prediction by directly calling predict. This is needed to test
# csc_matrix, but doesn't hurt others
if sparse is sp.csc_matrix:
test_x = sparse(test_x)
values, probs = model.predict(test_x)
np.testing.assert_almost_equal(exp_probs, probs)
np.testing.assert_equal(values, np.argmax(exp_probs, axis=1))
def _test_predictions_with_absent_class(self, sparse):
"""Empty classes should not affect predictions"""
x = np.array([
[1, 0, 0],
[0, np.nan, 0],
[0, 1, 0],
[0, 0, 0],
[1, 2, 0],
[1, 1, 0],
[1, 2, 0],
[0, 1, 0]])
if sparse is not None:
x = sparse(x)
y = np.array([0, 0, 0, 2, 2, 2, 3, 3])
domain = Domain(
[DiscreteVariable("a", values="ab"),
DiscreteVariable("b", values="abc"),
DiscreteVariable("c", values="a")],
DiscreteVariable("y", values="abcd"))
data = Table.from_numpy(domain, x, y)
model = self.learner(data)
np.testing.assert_almost_equal(
model.class_prob,
[4/11, 0, 4/11, 3/11]
)
np.testing.assert_almost_equal(
np.exp(model.log_cont_prob[0]) * model.class_prob[:, None],
[[3/7, 2/7], [0, 0], [2/7, 3/7], [2/7, 2/7]])
np.testing.assert_almost_equal(
np.exp(model.log_cont_prob[1]) * model.class_prob[:, None],
[[2/5, 1/3, 1/5], [0, 0, 0], [2/5, 1/3, 2/5], [1/5, 1/3, 2/5]])
np.testing.assert_almost_equal(
np.exp(model.log_cont_prob[2]) * model.class_prob[:, None],
[[4/11], [0], [4/11], [3/11]])
test_x = np.array([[a, b, 0] for a in [0, 1] for b in [0, 1, 2]])
# Classifiers reject csc matrices in the base class
# Naive bayesian classifier supports them if predict_storage is
# called directly, which we do below
if sparse is not None and sparse is not sp.csc_matrix:
test_x = sparse(test_x)
test_y = np.full((6, ), np.nan)
# The following was computed manually, too
exp_probs = np.array([
[0.47368421052632, 0, 0.31578947368421, 0.21052631578947],
[0.39130434782609, 0, 0.26086956521739, 0.34782608695652],
[0.24324324324324, 0, 0.32432432432432, 0.43243243243243],
[0.31578947368421, 0, 0.47368421052632, 0.21052631578947],
[0.26086956521739, 0, 0.39130434782609, 0.34782608695652],
[0.15000000000000, 0, 0.45000000000000, 0.40000000000000]
])
# Test the faster algorithm for Table (numpy matrices)
test_data = Table.from_numpy(domain, test_x, test_y)
probs = model(test_data, ret=model.Probs)
np.testing.assert_almost_equal(exp_probs, probs)
values = model(test_data)
np.testing.assert_equal(values, np.argmax(exp_probs, axis=1))
values, probs = model(test_data, ret=model.ValueProbs)
np.testing.assert_almost_equal(exp_probs, probs)
np.testing.assert_equal(values, np.argmax(exp_probs, axis=1))
# Test the slower algorithm for non-Table data (iteration in Python)
test_data = NotATable.from_numpy(domain, test_x, test_y)
probs = model(test_data, ret=model.Probs)
np.testing.assert_almost_equal(exp_probs, probs)
values = model(test_data)
np.testing.assert_equal(values, np.argmax(exp_probs, axis=1))
values, probs = model(test_data, ret=model.ValueProbs)
np.testing.assert_almost_equal(exp_probs, probs)
np.testing.assert_equal(values, np.argmax(exp_probs, axis=1))
# Test prediction directly on numpy
probs = model(test_x, ret=model.Probs)
np.testing.assert_almost_equal(exp_probs, probs)
values = model(test_x)
np.testing.assert_equal(values, np.argmax(exp_probs, axis=1))
values, probs = model(test_x, ret=model.ValueProbs)
np.testing.assert_almost_equal(exp_probs, probs)
np.testing.assert_equal(values, np.argmax(exp_probs, axis=1))
# Test prediction on instances
for inst, exp_prob in zip(test_data, exp_probs):
np.testing.assert_almost_equal(
model(inst, ret=model.Probs),
exp_prob)
self.assertEqual(model(inst), np.argmax(exp_prob))
value, prob = model(inst, ret=model.ValueProbs)
np.testing.assert_almost_equal(prob, exp_prob)
self.assertEqual(value, np.argmax(exp_prob))
# Test prediction by directly calling predict. This is needed to test
# csc_matrix, but doesn't hurt others
if sparse is sp.csc_matrix:
test_x = sparse(test_x)
values, probs = model.predict(test_x)
np.testing.assert_almost_equal(exp_probs, probs)
np.testing.assert_equal(values, np.argmax(exp_probs, axis=1))
def test_no_attributes(self):
y = np.array([0, 0, 0, 1, 1, 1, 2, 2])
domain = Domain([], DiscreteVariable("y", values="abc"))
data = Table.from_numpy(domain, np.zeros((len(y), 0)), y.T)
model = self.learner(data)
np.testing.assert_almost_equal(
model.predict_storage(np.zeros((5, 0)))[1],
[[4/11, 4/11, 3/11]] * 5
)
def test_no_targets(self):
x = np.array([[0], [1], [2]])
y = np.full(3, np.nan)
domain = Domain([DiscreteVariable("x", values="abc")],
DiscreteVariable("y", values="abc"))
data = Table.from_numpy(domain, x, y)
self.assertRaises(ValueError, self.learner, data)
if __name__ == "__main__":
unittest.main()