Skip to content

Commit a47e4e9

Browse files
committed
mock.MockClassifier renamed to mock.MockEstimator, modAL.acquisition.PI() tested
1 parent 814b68e commit a47e4e9

File tree

2 files changed

+52
-30
lines changed

2 files changed

+52
-30
lines changed

tests/core_tests.py

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from sklearn.ensemble import RandomForestClassifier
1818
from sklearn.metrics import confusion_matrix
1919
from scipy.stats import entropy
20+
from scipy.special import ndtr
2021

2122

2223
Test = namedtuple('Test', ['input', 'output'])
@@ -34,8 +35,8 @@ def test_check_class_labels(self):
3435
for n_learners in range(1, 10):
3536
labels = np.random.randint(10, size=n_labels)
3637
different_labels = np.random.randint(10, 20, size=np.random.randint(1, 10))
37-
learner_list_1 = [mock.MockClassifier(classes_=labels) for _ in range(n_learners)]
38-
learner_list_2 = [mock.MockClassifier(classes_=different_labels) for _ in range(np.random.randint(1, 5))]
38+
learner_list_1 = [mock.MockEstimator(classes_=labels) for _ in range(n_learners)]
39+
learner_list_2 = [mock.MockEstimator(classes_=different_labels) for _ in range(np.random.randint(1, 5))]
3940
shuffled_learners = random.sample(learner_list_1 + learner_list_2, len(learner_list_1 + learner_list_2))
4041
self.assertTrue(modAL.utils.validation.check_class_labels(*learner_list_1))
4142
self.assertFalse(modAL.utils.validation.check_class_labels(*shuffled_learners))
@@ -114,7 +115,7 @@ def test_make_query_strategy(self):
114115
X = np.random.rand(n_samples, 3)
115116

116117
learner = modAL.models.ActiveLearner(
117-
estimator=mock.MockClassifier(predict_proba_return=proba)
118+
estimator=mock.MockEstimator(predict_proba_return=proba)
118119
)
119120

120121
query_1 = query_strategy(learner, X)
@@ -124,13 +125,34 @@ def test_make_query_strategy(self):
124125
np.testing.assert_almost_equal(query_1[1], query_2[1])
125126

126127

128+
class TestAcquisitionFunctions(unittest.TestCase):
129+
def test_PI(self):
130+
for n_samples in range(1, 100):
131+
mean = np.random.rand(n_samples, )
132+
std = np.random.rand(n_samples, )
133+
tradeoff = np.random.rand()
134+
max_val = np.random.rand()
135+
136+
mock_estimator = mock.MockEstimator(
137+
predict_return=(mean, std)
138+
)
139+
140+
optimizer = modAL.models.BayesianOptimizer(estimator=mock_estimator)
141+
optimizer._set_max([max_val])
142+
143+
np.testing.assert_almost_equal(
144+
ndtr((mean - max_val - tradeoff)/std),
145+
modAL.acquisition.PI(optimizer, np.random.rand(n_samples, 2), tradeoff)
146+
)
147+
148+
127149
class TestUncertainties(unittest.TestCase):
128150

129151
def test_classifier_uncertainty(self):
130152
test_cases = (Test(p * np.ones(shape=(k, l)), (1 - p) * np.ones(shape=(k, )))
131153
for k in range(1, 100) for l in range(1, 10) for p in np.linspace(0, 1, 11))
132154
for case in test_cases:
133-
mock_classifier = mock.MockClassifier(predict_proba_return=case.input)
155+
mock_classifier = mock.MockEstimator(predict_proba_return=case.input)
134156
np.testing.assert_almost_equal(
135157
modAL.uncertainty.classifier_uncertainty(mock_classifier, np.random.rand(10)),
136158
case.output
@@ -143,7 +165,7 @@ def test_classifier_margin(self):
143165
p * np.ones(shape=(l, ))*int(k!=1))
144166
for k in range(1, 10) for l in range(1, 100) for p in np.linspace(0, 1, 11))
145167
for case in chain(test_cases_1, test_cases_2):
146-
mock_classifier = mock.MockClassifier(predict_proba_return=case.input)
168+
mock_classifier = mock.MockEstimator(predict_proba_return=case.input)
147169
np.testing.assert_almost_equal(
148170
modAL.uncertainty.classifier_margin(mock_classifier, np.random.rand(10)),
149171
case.output
@@ -156,7 +178,7 @@ def test_classifier_entropy(self):
156178
for sample_idx in range(n_samples):
157179
proba[sample_idx, np.random.choice(range(n_classes))] = 1.0
158180

159-
classifier = mock.MockClassifier(predict_proba_return=proba)
181+
classifier = mock.MockEstimator(predict_proba_return=proba)
160182
np.testing.assert_equal(
161183
modAL.uncertainty.classifier_entropy(classifier, np.random.rand(n_samples, 1)),
162184
np.zeros(shape=(n_samples, ))
@@ -169,7 +191,7 @@ def test_uncertainty_sampling(self):
169191
for true_query_idx in range(n_samples):
170192
predict_proba = np.random.rand(n_samples, n_classes)
171193
predict_proba[true_query_idx] = max_proba
172-
classifier = mock.MockClassifier(predict_proba_return=predict_proba)
194+
classifier = mock.MockEstimator(predict_proba_return=predict_proba)
173195
query_idx, query_instance = modAL.uncertainty.uncertainty_sampling(
174196
classifier, np.random.rand(n_samples, n_classes)
175197
)
@@ -182,7 +204,7 @@ def test_margin_sampling(self):
182204
for true_query_idx in range(n_samples):
183205
predict_proba = np.random.rand(n_samples, n_classes)
184206
predict_proba[true_query_idx] = max_proba
185-
classifier = mock.MockClassifier(predict_proba_return=predict_proba)
207+
classifier = mock.MockEstimator(predict_proba_return=predict_proba)
186208
query_idx, query_instance = modAL.uncertainty.uncertainty_sampling(
187209
classifier, np.random.rand(n_samples, n_classes)
188210
)
@@ -196,7 +218,7 @@ def test_entropy_sampling(self):
196218
predict_proba = np.zeros(shape=(n_samples, n_classes))
197219
predict_proba[:, 0] = 1.0
198220
predict_proba[true_query_idx] = max_proba
199-
classifier = mock.MockClassifier(predict_proba_return=predict_proba)
221+
classifier = mock.MockEstimator(predict_proba_return=predict_proba)
200222
query_idx, query_instance = modAL.uncertainty.uncertainty_sampling(
201223
classifier, np.random.rand(n_samples, n_classes)
202224
)
@@ -314,7 +336,7 @@ def test_add_training_data(self):
314336
X_new = np.random.rand(n_new_samples, n_features)
315337
y_new = np.random.randint(0, 2, size=(n_new_samples,))
316338
learner = modAL.models.ActiveLearner(
317-
estimator=mock.MockClassifier(),
339+
estimator=mock.MockEstimator(),
318340
X_training=X_initial, y_training=y_initial
319341
)
320342
learner._add_training_data(X_new, y_new)
@@ -330,7 +352,7 @@ def test_add_training_data(self):
330352
y_initial = np.random.randint(0, 2, size=(n_samples, n_features+1))
331353
y_new = np.random.randint(0, 2, size=(n_new_samples, n_features+1))
332354
learner = modAL.models.ActiveLearner(
333-
estimator=mock.MockClassifier(),
355+
estimator=mock.MockEstimator(),
334356
X_training=X_initial, y_training=y_initial
335357
)
336358
learner._add_training_data(X_new, y_new)
@@ -354,7 +376,7 @@ def test_predict(self):
354376
for n_features in range(1, 10):
355377
X = np.random.rand(n_samples, n_features)
356378
predict_return = np.random.randint(0, 2, size=(n_samples, ))
357-
mock_classifier = mock.MockClassifier(predict_return=predict_return)
379+
mock_classifier = mock.MockEstimator(predict_return=predict_return)
358380
learner = modAL.models.ActiveLearner(
359381
estimator=mock_classifier
360382
)
@@ -368,7 +390,7 @@ def test_predict_proba(self):
368390
for n_features in range(1, 10):
369391
X = np.random.rand(n_samples, n_features)
370392
predict_proba_return = np.random.randint(0, 2, size=(n_samples,))
371-
mock_classifier = mock.MockClassifier(predict_proba_return=predict_proba_return)
393+
mock_classifier = mock.MockEstimator(predict_proba_return=predict_proba_return)
372394
learner = modAL.models.ActiveLearner(
373395
estimator=mock_classifier
374396
)
@@ -395,7 +417,7 @@ def test_query(self):
395417
def test_score(self):
396418
test_cases = (np.random.rand() for _ in range(10))
397419
for score_return in test_cases:
398-
mock_classifier = mock.MockClassifier(score_return=score_return)
420+
mock_classifier = mock.MockEstimator(score_return=score_return)
399421
learner = modAL.models.ActiveLearner(mock_classifier, mock.MockFunction(None))
400422
np.testing.assert_almost_equal(
401423
learner.score(np.random.rand(5, 2), np.random.rand(5, )),
@@ -413,7 +435,7 @@ def test_teach(self):
413435

414436
learner = modAL.models.ActiveLearner(
415437
X_training=X_training, y_training=y_training,
416-
estimator=mock.MockClassifier()
438+
estimator=mock.MockEstimator()
417439
)
418440

419441
learner.teach(X, y, bootstrap=bootstrap, only_new=only_new)
@@ -436,7 +458,7 @@ def test_sklearn(self):
436458
class TestBayesianOptimizer(unittest.TestCase):
437459
def test_set_max(self):
438460
# case 1: the estimator is not fitted yet
439-
regressor = mock.MockClassifier()
461+
regressor = mock.MockEstimator()
440462
learner = modAL.models.BayesianOptimizer(estimator=regressor)
441463
self.assertEqual(-np.inf, learner.max_val)
442464

@@ -446,7 +468,7 @@ def test_set_max(self):
446468
y = np.random.rand(n_samples, )
447469
max_val = np.max(y)
448470

449-
regressor = mock.MockClassifier()
471+
regressor = mock.MockEstimator()
450472
learner = modAL.models.BayesianOptimizer(
451473
estimator=regressor,
452474
X_training=X, y_training=y
@@ -458,7 +480,7 @@ def test_set_new_max(self):
458480
# case 1: the learner is not fitted yet
459481
for n_samples in range(1, 10):
460482
y = np.random.rand(n_samples)
461-
regressor = mock.MockClassifier()
483+
regressor = mock.MockEstimator()
462484
learner = modAL.models.BayesianOptimizer(estimator=regressor)
463485
learner._set_max(y)
464486
self.assertEqual(learner.max_val, np.max(y))
@@ -468,7 +490,7 @@ def test_set_new_max(self):
468490
X = np.random.rand(n_samples, 2)
469491
y = np.random.rand(n_samples)
470492

471-
regressor = mock.MockClassifier()
493+
regressor = mock.MockEstimator()
472494
learner = modAL.models.BayesianOptimizer(
473495
estimator=regressor,
474496
X_training=X, y_training=y
@@ -484,7 +506,7 @@ def test_set_new_max(self):
484506
X = np.random.rand(n_samples, 2)
485507
y = np.random.rand(n_samples)
486508

487-
regressor = mock.MockClassifier()
509+
regressor = mock.MockEstimator()
488510
learner = modAL.models.BayesianOptimizer(
489511
estimator=regressor,
490512
X_training=X, y_training=y
@@ -499,7 +521,7 @@ def test_teach(self):
499521
# case 1. optimizer is uninitialized
500522
for n_samples in range(1, 100):
501523
for n_features in range(1, 100):
502-
regressor = mock.MockClassifier()
524+
regressor = mock.MockEstimator()
503525
learner = modAL.models.BayesianOptimizer(estimator=regressor)
504526

505527
X = np.random.rand(n_samples, 2)
@@ -512,7 +534,7 @@ def test_teach(self):
512534
X = np.random.rand(n_samples, 2)
513535
y = np.random.rand(n_samples)
514536

515-
regressor = mock.MockClassifier()
537+
regressor = mock.MockEstimator()
516538
learner = modAL.models.BayesianOptimizer(
517539
estimator=regressor,
518540
X_training=X, y_training=y
@@ -524,7 +546,7 @@ class TestCommittee(unittest.TestCase):
524546

525547
def test_set_classes(self):
526548
for n_classes in range(1, 10):
527-
learner_list = [modAL.models.ActiveLearner(estimator=mock.MockClassifier(classes_=np.asarray([idx])))
549+
learner_list = [modAL.models.ActiveLearner(estimator=mock.MockEstimator(classes_=np.asarray([idx])))
528550
for idx in range(n_classes)]
529551
committee = modAL.models.Committee(learner_list=learner_list)
530552
np.testing.assert_equal(
@@ -538,7 +560,7 @@ def test_predict(self):
538560
prediction = np.random.randint(10, size=(n_instances, n_learners))
539561
committee = modAL.models.Committee(
540562
learner_list=[mock.MockActiveLearner(
541-
mock.MockClassifier(classes_=np.asarray([0])),
563+
mock.MockEstimator(classes_=np.asarray([0])),
542564
predict_return=prediction[:, learner_idx]
543565
)
544566
for learner_idx in range(n_learners)]
@@ -556,7 +578,7 @@ def test_predict_proba(self):
556578
# assembling the mock learners
557579
learner_list = [mock.MockActiveLearner(
558580
predict_proba_return=vote_proba_output[:, learner_idx, :],
559-
predictor=mock.MockClassifier(classes_=list(range(n_classes)))
581+
predictor=mock.MockEstimator(classes_=list(range(n_classes)))
560582
) for learner_idx in range(n_learners)]
561583
committee = modAL.models.Committee(learner_list=learner_list)
562584
np.testing.assert_almost_equal(
@@ -571,7 +593,7 @@ def test_vote(self):
571593
# assembling the Committee
572594
learner_list = [mock.MockActiveLearner(
573595
predict_return=vote_output[:, member_idx],
574-
predictor=mock.MockClassifier(classes_=[0])
596+
predictor=mock.MockEstimator(classes_=[0])
575597
)
576598
for member_idx in range(n_members)]
577599
committee = modAL.models.Committee(learner_list=learner_list)
@@ -588,7 +610,7 @@ def test_vote_proba(self):
588610
# assembling the mock learners
589611
learner_list = [mock.MockActiveLearner(
590612
predict_proba_return=vote_proba_output[:, learner_idx, :],
591-
predictor=mock.MockClassifier(classes_=list(range(n_classes)))
613+
predictor=mock.MockEstimator(classes_=list(range(n_classes)))
592614
) for learner_idx in range(n_learners)]
593615
committee = modAL.models.Committee(learner_list=learner_list)
594616
np.testing.assert_almost_equal(
@@ -607,11 +629,11 @@ def test_teach(self):
607629

608630
learner_1 = modAL.models.ActiveLearner(
609631
X_training=X_training, y_training=y_training,
610-
estimator=mock.MockClassifier(classes_=[0, 1])
632+
estimator=mock.MockEstimator(classes_=[0, 1])
611633
)
612634
learner_2 = modAL.models.ActiveLearner(
613635
X_training=X_training, y_training=y_training,
614-
estimator=mock.MockClassifier(classes_=[0, 1])
636+
estimator=mock.MockEstimator(classes_=[0, 1])
615637
)
616638

617639
committee = modAL.models.Committee(

tests/mock.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def __call__(self, *args):
99
return self.return_val
1010

1111

12-
class MockClassifier:
12+
class MockEstimator:
1313
"""
1414
Mock classifier object for testing.
1515
"""

0 commit comments

Comments
 (0)