Beispiel #1
0
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 = DCS(create_pool_classifiers())

    with pytest.raises(ValueError):
        dcs_test.predict_proba_with_ds(query, predictions, probabilities)
Beispiel #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)
Beispiel #3
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()