def test_predict_proba_instance(): query = np.array([-1, 1]) n_classifiers = 3 pool_classifiers = create_pool_classifiers() dcs_test = BaseDCS(pool_classifiers) dcs_test.n_classes_ = 2 competences = np.random.rand(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_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 = BaseDCS(pool_classifiers, selection_method='all') selected_clf = dcs_test.select(competences) assert np.array_equal(selected_clf, expected)
def test_selection_method_type(selection_method, create_pool_classifiers): X = np.random.rand(10, 2) y = np.ones(10) with pytest.raises(TypeError): dcs = BaseDCS(create_pool_classifiers, selection_method=selection_method) dcs.fit(X, y)
def test_classify_with_ds_diff_sizes(): query = np.ones((10, 2)) predictions = np.ones((5, 3)) dcs_test = BaseDCS(create_pool_classifiers()) with pytest.raises(ValueError): dcs_test.classify_with_ds(query, predictions)
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 = BaseDCS(pool_classifiers, selection_method='best') selected_clf = dcs_test.select(competences) assert np.array_equal(selected_clf, expected)
def test_valid_selection_method(selection_method): X = np.random.rand(10, 2) y = np.ones(10) with pytest.raises(ValueError): dcs = BaseDCS(create_pool_classifiers(), selection_method=selection_method) dcs.fit(X, y)
def test_select_diff(competences, expected): rng = np.random.RandomState(123456) dcs_test = BaseDCS(selection_method='diff', diff_thresh=0.15, random_state=rng) selected_clf = dcs_test.select(np.array(competences)) assert np.allclose(selected_clf, expected)
def test_proba_with_ds_diff_sizes(): query = np.ones((10, 2)) predictions = np.ones((5, 3)) probabilities = np.ones((5, 3, 2)) dcs_test = BaseDCS() with pytest.raises(ValueError): dcs_test.predict_proba_with_ds(query, predictions, probabilities)
def test_select_random(competences, expected): rng = np.random.RandomState(123456) pool_classifiers = create_pool_classifiers() dcs_test = BaseDCS(pool_classifiers, selection_method='random', random_state=rng) dcs_test.fit(X_dsel_ex1, y_dsel_ex1) selected_clf = dcs_test.select(np.array(competences)) assert np.allclose(selected_clf, expected)
def test_valid_diff_threshold_value(diff_thresh): X = np.random.rand(10, 2) y = np.ones(10) with pytest.raises(ValueError): dcs = BaseDCS(create_pool_classifiers(), selection_method='diff', diff_thresh=diff_thresh) dcs.fit(X, y)
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) dcs_test = BaseDCS(selection_method='random', random_state=rng) 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 = BaseDCS(pool_classifiers, selection_method='random', random_state=rng) dcs_test.fit(X_dsel_ex1, y_dsel_ex1) 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 = BaseDCS(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_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) dcs_test = BaseDCS(selection_method='diff', diff_thresh=0.15, random_state=rng) selected_clf = dcs_test.select(competences) assert np.array_equal(selected_clf, 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 = BaseDCS(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(): query = np.array([-1, 1]) n_classifiers = 3 pool_classifiers = create_pool_classifiers() dcs_test = BaseDCS(pool_classifiers) competences = np.random.rand(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_batch(): n_samples = 3 n_classifiers = 3 query = np.ones((n_samples, 2)) pool_classifiers = create_pool_classifiers() dcs_test = BaseDCS(pool_classifiers) competences = np.random.rand(n_samples, 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 = BaseDCS(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_select_all(competences, expected): pool_classifiers = create_pool_classifiers() dcs_test = BaseDCS(pool_classifiers, selection_method='all') selected_clf = dcs_test.select(np.array(competences)) assert np.allclose(selected_clf, expected)