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_predict_proba_instance(): query = np.array([-1, 1]) pool_classifiers = create_pool_classifiers() dcs_test = DCS(pool_classifiers) dcs_test.n_classes = 2 competences = np.random.rand(dcs_test.n_classifiers) dcs_test.estimate_competence = MagicMock(return_value=competences) expected = pool_classifiers[np.argmax(competences)].predict_proba(query) predictions = [] probabilities = [] for clf in dcs_test.pool_classifiers: predictions.append(clf.predict(query)[0]) probabilities.append(clf.predict_proba(query)[0]) query = np.atleast_2d(query) predictions = np.atleast_2d(predictions) probabilities = np.array(probabilities) probabilities = np.expand_dims(probabilities, axis=0) predicted_proba = dcs_test.predict_proba_with_ds(query, predictions, probabilities) assert np.array_equal(predicted_proba, expected)
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=competences) predicted_label = dcs_test.classify_instance(query) assert predicted_label == expected
def test_select_best_batch(): competences = np.array([[1.0, 0.5, 0.5], [0.8, 0.9, 1.0], [0.0, 0.0, 0.15]]) expected = [0, 2, 2] pool_classifiers = create_pool_classifiers() dcs_test = DCS(pool_classifiers, selection_method='best') selected_clf = dcs_test.select(competences) assert np.array_equal(selected_clf, expected)
def test_select_random_batch(): competences = np.array([[0.5, 0.5, 0.5], [0.8, 0.9, 1.0], [0.0, 0.10, 0.0]]) expected = np.array([1, 2, 1]) rng = np.random.RandomState(123456) pool_classifiers = create_pool_classifiers() dcs_test = DCS(pool_classifiers, selection_method='random', rng=rng) selected_clf = dcs_test.select(competences) assert np.array_equal(selected_clf, expected)
def test_select_diff(competences, expected): rng = np.random.RandomState(123456) pool_classifiers = create_pool_classifiers() dcs_test = DCS(pool_classifiers, selection_method='diff', diff_thresh=0.15, rng=rng) selected_clf = dcs_test.select(np.array(competences)) assert np.allclose(selected_clf, expected)
def test_predict_proba_instance_all(competences, expected): query = np.array([-1, 1]) pool_classifiers = create_pool_classifiers() dcs_test = DCS(pool_classifiers, selection_method='all') dcs_test.n_classes = 2 dcs_test.estimate_competence = MagicMock(return_value=competences) predicted_proba = dcs_test.predict_proba_instance(query) assert np.isclose(predicted_proba, expected).all()
def test_select_all_batch(): competences = np.array([[1.0, 1.0, 0.5], [0.8, 0.9, 0.9], [0.15, 0.15, 0.15], [0.0, 0.0, 0.0]]) expected = np.array([[True, True, False], [False, True, True], [True, True, True], [True, True, True]]) pool_classifiers = create_pool_classifiers() dcs_test = DCS(pool_classifiers, selection_method='all') selected_clf = dcs_test.select(competences) assert np.array_equal(selected_clf, 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] predicted_label = dcs_test.classify_instance(query) assert predicted_label == expected
def test_select_diff_batch(): competences = np.array([[1.0, 0.5, 0.5], [0.8, 0.9, 1.0], [0.0, 0.0, 0.15]]) expected = np.array([0, 2, 2]) rng = np.random.RandomState(123456) pool_classifiers = create_pool_classifiers() dcs_test = DCS(pool_classifiers, selection_method='diff', diff_thresh=0.15, rng=rng) selected_clf = dcs_test.select(competences) assert np.array_equal(selected_clf, expected)
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_predict_proba_instance(): query = np.array([-1, 1]) pool_classifiers = create_pool_classifiers() dcs_test = DCS(pool_classifiers) dcs_test.n_classes = 2 competences = np.random.rand(dcs_test.n_classifiers) dcs_test.estimate_competence = MagicMock(return_value=competences) expected = pool_classifiers[np.argmax(competences)].predict_proba(query) predicted_proba = dcs_test.predict_proba_instance(query) assert np.array_equal(predicted_proba, 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)
def test_predict_proba_instance_all(competences, expected): query = np.array([-1, 1]) pool_classifiers = create_pool_classifiers() dcs_test = DCS(pool_classifiers, selection_method='all') dcs_test.n_classes = 2 dcs_test.estimate_competence = MagicMock( return_value=np.array(competences)) predictions = [] probabilities = [] for clf in dcs_test.pool_classifiers: predictions.append(clf.predict(query)[0]) probabilities.append(clf.predict_proba(query)[0]) query = np.atleast_2d(query) predictions = np.atleast_2d(predictions) probabilities = np.array(probabilities) probabilities = np.expand_dims(probabilities, axis=0) predicted_proba = dcs_test.predict_proba_with_ds(query, predictions, probabilities) assert np.isclose(predicted_proba, expected).all()
def test_valid_selection_method(selection_method): with pytest.raises(ValueError): DCS(create_pool_classifiers(), selection_method=selection_method)
def test_select_all(competences, expected): pool_classifiers = create_pool_classifiers() dcs_test = DCS(pool_classifiers, selection_method='all') selected_clf = dcs_test.select(competences) assert selected_clf == expected
def test_select_all(competences, expected): pool_classifiers = create_pool_classifiers() dcs_test = DCS(pool_classifiers, selection_method='all') selected_clf = dcs_test.select(np.array(competences)) assert np.allclose(selected_clf, expected)
def test_valid_diff_threshold_value(diff_thresh): with pytest.raises(ValueError): DCS(create_pool_classifiers(), selection_method='diff', diff_thresh=diff_thresh)
def test_select_random(competences, expected): rng = np.random.RandomState(123456) pool_classifiers = create_pool_classifiers() dcs_test = DCS(pool_classifiers, selection_method='random', rng=rng) selected_clf = dcs_test.select(competences) assert selected_clf == expected
def test_selection_method_type(selection_method): with pytest.raises(TypeError): DCS(create_pool_classifiers(), selection_method=selection_method)