Exemplo n.º 1
0
def test_classify_with_ds_diff_sizes():
    query = np.ones((10, 2))
    predictions = np.ones((5, 3))
    des_test = DES(create_pool_classifiers())

    with pytest.raises(ValueError):
        des_test.classify_with_ds(query, predictions)
Exemplo n.º 2
0
def test_classify_instance_selection():
    query = np.array([-1, 1])
    pool_classifiers = create_pool_classifiers() + create_pool_classifiers()
    des_test = DES(pool_classifiers, mode='selection')
    selected_index = np.array([[True, True, False, False, False, True]])
    des_test.select = MagicMock(return_value=selected_index)

    predictions = []
    for clf in des_test.pool_classifiers:
        predictions.append(clf.predict(query)[0])

    predicted_label = des_test.classify_with_ds(query, np.array(predictions))
    assert predicted_label == 0.0
Exemplo n.º 3
0
def test_classify_instance_selection_batch():
    n_samples = 3
    query = np.ones((n_samples, 2))
    pool_classifiers = create_pool_classifiers() + create_pool_classifiers()
    des_test = DES(pool_classifiers, mode='selection')
    selected_index = np.array([[True, True, False, False, False, True] * n_samples])
    des_test.select = MagicMock(return_value=selected_index)

    predictions = []
    for clf in des_test.pool_classifiers:
        predictions.append(clf.predict(query)[0])

    predicted_label = des_test.classify_with_ds(query, np.tile(predictions, (n_samples, 1)))
    assert np.allclose(predicted_label, 0) and predicted_label.size == 3
Exemplo n.º 4
0
def test_classify_instance_weighting_batch():
    n_samples = 3
    query = np.ones((n_samples, 2))
    pool_classifiers = create_pool_classifiers() + create_pool_classifiers()
    des_test = DES(pool_classifiers, mode='weighting')
    des_test.classes = np.array([0, 1])
    des_test.n_classes = 2

    competences = np.tile([0.55, 1.0, 0.2, 0.60, 0.75, 0.3], (3, 1))
    des_test.estimate_competence = MagicMock(return_value=competences)

    predictions = []
    for clf in des_test.pool_classifiers:
        predictions.append(clf.predict(query)[0])
    predicted_label = des_test.classify_with_ds(query, np.tile(predictions, (3, 1)))
    assert np.allclose(predicted_label, 1) and predicted_label.size == 3
Exemplo n.º 5
0
def test_classify_instance_weighting():
    query = np.array([-1, 1])

    pool_classifiers = create_pool_classifiers() + create_pool_classifiers()
    des_test = DES(pool_classifiers, mode='weighting')
    des_test.classes = np.array([0, 1])
    des_test.n_classes = 2

    competences = np.array([0.55, 1.0, 0.2, 0.60, 0.75, 0.3])
    des_test.estimate_competence = MagicMock(return_value=competences)

    predictions = []
    for clf in des_test.pool_classifiers:
        predictions.append(clf.predict(query)[0])
    predicted_label = des_test.classify_with_ds(query, np.array(predictions))
    assert predicted_label == 1.0
Exemplo n.º 6
0
def test_classify_instance_hybrid_batch():
    query = np.ones((3, 2))
    expected = 1
    pool_classifiers = create_pool_classifiers() + create_pool_classifiers()
    des_test = DES(pool_classifiers, mode='hybrid')
    des_test.classes = np.array([0, 1])
    des_test.n_classes = 2

    selected_indices = np.tile([True, True, False, False, False, True], (3, 1))
    competences = np.tile([0.55, 1.0, 0.2, 0.60, 0.75, 0.3], (3, 1))
    des_test.estimate_competence = MagicMock(return_value=competences)
    des_test.select = MagicMock(return_value=selected_indices)

    predictions = []
    for clf in des_test.pool_classifiers:
        predictions.append(clf.predict(query)[0])

    predicted_label = des_test.classify_with_ds(query, np.tile(predictions, (3, 1)))
    assert np.allclose(predicted_label, expected)
Exemplo n.º 7
0
def test_classify_instance_hybrid():
    query = np.array([-1, 1])
    expected = 1

    pool_classifiers = create_pool_classifiers() + create_pool_classifiers()
    des_test = DES(pool_classifiers, mode='hybrid')
    des_test.classes = np.array([0, 1])
    des_test.n_classes = 2
    selected_indices = np.array([[True, True, False, False, False, True]])
    competences = np.array([[0.55, 1.0, 0.2, 0.60, 0.75, 0.3]])
    des_test.estimate_competence = MagicMock(return_value=competences)
    des_test.select = MagicMock(return_value=selected_indices)

    predictions = []
    for clf in des_test.pool_classifiers:
        predictions.append(clf.predict(query)[0])

    predicted_label = des_test.classify_with_ds(query, np.array(predictions))
    assert expected == predicted_label