Esempio n. 1
0
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)
Esempio n. 2
0
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)
Esempio n. 3
0
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
Esempio n. 4
0
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)
Esempio n. 5
0
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)
Esempio n. 6
0
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)
Esempio n. 7
0
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()
Esempio n. 8
0
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)
Esempio n. 9
0
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
Esempio n. 10
0
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)
Esempio n. 11
0
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
Esempio n. 12
0
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)
Esempio n. 13
0
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
Esempio n. 14
0
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)
Esempio n. 15
0
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)
Esempio n. 16
0
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()
Esempio n. 17
0
def test_valid_selection_method(selection_method):
    with pytest.raises(ValueError):
        DCS(create_pool_classifiers(), selection_method=selection_method)
Esempio n. 18
0
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
Esempio n. 19
0
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)
Esempio n. 20
0
def test_valid_diff_threshold_value(diff_thresh):
    with pytest.raises(ValueError):
        DCS(create_pool_classifiers(),
            selection_method='diff',
            diff_thresh=diff_thresh)
Esempio n. 21
0
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
Esempio n. 22
0
def test_selection_method_type(selection_method):
    with pytest.raises(TypeError):
        DCS(create_pool_classifiers(), selection_method=selection_method)