def test_classify_with_ds_diff_sizes(): query = np.ones((10, 2)) predictions = np.ones((5, 3)) dcs_test = DCS(create_pool_classifiers()) with pytest.raises(ValueError): dcs_test.classify_with_ds(query, predictions)
def test_classify_instance_all(competences, expected): query = np.array([-1, 1]) pool_classifiers = create_pool_classifiers() dcs_test = DCS(pool_classifiers, selection_method='all') dcs_test.estimate_competence = MagicMock( return_value=np.array(competences)) predictions = [] for clf in dcs_test.pool_classifiers: predictions.append(clf.predict(query)[0]) predicted_label = dcs_test.classify_with_ds(query, np.array(predictions)) assert predicted_label == expected
def test_classify_instance(): query = np.array([-1, 1]) pool_classifiers = create_pool_classifiers() dcs_test = DCS(pool_classifiers) competences = np.random.rand(dcs_test.n_classifiers) dcs_test.estimate_competence = MagicMock(return_value=competences) expected = pool_classifiers[np.argmax(competences)].predict(query)[0] predictions = [] for clf in dcs_test.pool_classifiers: predictions.append(clf.predict(query)[0]) predicted_label = dcs_test.classify_with_ds(query, np.array(predictions)) assert predicted_label == expected
def test_classify_instance_all_batch(): competences = np.array([[0.6, 0.2, 0.6], [0.5, 0.8, 0.5]]) expected = [0, 1] n_samples = 2 query = np.ones((n_samples, 2)) pool_classifiers = create_pool_classifiers() dcs_test = DCS(pool_classifiers, selection_method='all') dcs_test.estimate_competence = MagicMock( return_value=np.array(competences)) predictions = [] for clf in dcs_test.pool_classifiers: predictions.append(clf.predict(query)[0]) predicted_label = dcs_test.classify_with_ds( query, np.tile(predictions, (n_samples, 1))) assert np.array_equal(predicted_label, expected)
def test_classify_instance_batch(): n_samples = 3 query = np.ones((n_samples, 2)) pool_classifiers = create_pool_classifiers() dcs_test = DCS(pool_classifiers) competences = np.random.rand(n_samples, dcs_test.n_classifiers) dcs_test.estimate_competence = MagicMock(return_value=competences) expected = [] for ind in range(n_samples): expected.append(pool_classifiers[np.argmax( competences[ind, :])].predict(query)[0]) predictions = [] for clf in dcs_test.pool_classifiers: predictions.append(clf.predict(query)[0]) predicted_label = dcs_test.classify_with_ds(query, np.tile(predictions, (3, 1))) assert np.array_equal(predicted_label, expected)