Beispiel #1
0
def test_preprocess_dsel_scores():
    ds_test = DS(create_pool_classifiers())
    ds_test.fit(X_dsel_ex1, y_dsel_ex1)
    dsel_scores = ds_test._preprocess_dsel_scores()
    expected = np.array([[0.5, 0.5], [1.0, 0.0], [0.33, 0.67]])
    expected = np.tile(expected, (15, 1, 1))
    assert np.array_equal(dsel_scores, expected)
Beispiel #2
0
def test_get_dsel_scores():
    ds_test = DS(create_pool_classifiers())
    ds_test.fit(X_dsel_ex1, y_dsel_ex1)
    ds_test.dsel_scores = dsel_scores_ex1
    assert np.array_equal(ds_test._get_scores_dsel(0, 0), np.array([1.0, 0.0]))
    assert np.array_equal(ds_test._get_scores_dsel(1, 0), np.array([0.5, 0.5]))
    assert np.array_equal(ds_test._get_scores_dsel(2, 0), np.array([0.8, 0.2]))
Beispiel #3
0
def test_input_X_1D():

    X = np.ones(10)
    ds_test = DS(create_pool_classifiers())
    ds_test.fit(X_dsel_ex1, y_dsel_ex1)
    with pytest.raises(Warning):
        ds_test.predict(X)
Beispiel #4
0
def test_instance_hardness_region_all_same():
    X = X_dsel_ex1
    y = y_dsel_ex1
    ds_test = DS(create_pool_classifiers())
    ds_test.fit(X, y)
    neighbors = [0, 1, 2, 6, 7, 8, 13]
    IH = ds_test._hardness_region_competence(neighbors)
    assert IH == 0.0
Beispiel #5
0
def test_frienemy_no_classifier_crosses():
    X = X_dsel_ex1
    y = y_dsel_ex1
    ds_test = DS(create_pool_classifiers())
    ds_test.fit(X, y)
    ds_test.neighbors = neighbors_ex1[0, :]
    mask = ds_test._frienemy_pruning()
    assert mask.shape == (1, 3) and np.allclose(mask, 1)
Beispiel #6
0
def test_predict_proba_all_agree():
    query = np.atleast_2d([1, 1])
    ds_test = DS(create_pool_classifiers())
    ds_test.fit(X_dsel_ex1, y_dsel_ex1)
    ds_test.dsel_scores = dsel_scores_ex1
    ds_test._all_classifier_agree = MagicMock(return_value=True)
    proba = ds_test.predict_proba(query)
    assert np.isclose(proba, np.atleast_2d([0.61, 0.39])).all()
Beispiel #7
0
def test_frienemy_no_classifier_crosses():
    X = X_dsel_ex1
    y = y_dsel_ex1
    ds_test = DS(create_pool_classifiers())
    ds_test.fit(X, y)
    ds_test.neighbors = neighbors_ex1[0, :]
    mask = ds_test._frienemy_pruning()
    assert mask.size == 3 and mask.all() == 1
Beispiel #8
0
def test_instance_hardness_region(index, expected):
    X = X_dsel_ex1
    y = y_dsel_ex1
    ds_test = DS(create_pool_classifiers())
    ds_test.fit(X, y)
    neighbors = neighbors_ex1[index, :]
    IH = ds_test._hardness_region_competence(neighbors)
    assert np.isclose(IH, expected, atol=0.01)
Beispiel #9
0
def test_predict_proba_instance_called(index):
    query = np.atleast_2d([1, 1])
    ds_test = DS(create_pool_classifiers(), with_IH=True, IH_rate=0.10)
    ds_test.fit(X_dsel_ex1, y_dsel_ex1)

    ds_test.neighbors = neighbors_ex1[index, :]
    ds_test.distances = distances_ex1[index, :]

    ds_test.predict_proba_instance = MagicMock(return_value=np.atleast_2d([0.25, 0.75]))
    proba = ds_test.predict_proba(query)
    assert np.isclose(proba, np.atleast_2d([0.25, 0.75])).all()
Beispiel #10
0
def test_predict_proba_all_agree():
    query = np.atleast_2d([1, 1])
    ds_test = DS(create_pool_classifiers())
    ds_test.fit(X_dsel_ex1, y_dsel_ex1)
    ds_test.dsel_scores = dsel_scores_ex1
    backup_all_agree = DS._all_classifier_agree
    DS._all_classifier_agree = MagicMock(return_value=np.array([True]))
    proba = ds_test.predict_proba(query)

    DS._all_classifier_agree = backup_all_agree
    assert np.allclose(proba, np.atleast_2d([0.61, 0.39]))
Beispiel #11
0
def test_label_encoder_only_dsel_allagree():
    X_dsel_ex1 = np.array([[-1, 1], [-0.75, 0.5], [-1.5, 1.5]])
    y_dsel_ex1 = np.array(['cat', 'dog', 'plane'])

    query = np.atleast_2d([[1, 0], [-1, -1]])
    ds_test = DS(create_pool_classifiers_dog(), k=2)
    ds_test.fit(X_dsel_ex1, y_dsel_ex1)
    ds_test.neighbors = neighbors_ex1[0, :]
    ds_test.distances = distances_ex1[0, :]
    predictions = ds_test.predict(query)
    assert np.array_equal(predictions, ['dog', 'dog'])
Beispiel #12
0
def test_predict_proba_IH_knn(index):
    query = np.atleast_2d([1, 1])
    ds_test = DS(create_pool_classifiers(), with_IH=True, IH_rate=0.5)
    ds_test.fit(X_dsel_ex1, y_dsel_ex1)
    ds_test.dsel_scores = dsel_scores_ex1

    ds_test.neighbors = neighbors_ex1[index, :]
    ds_test.distances = distances_ex1[index, :]

    ds_test.roc_algorithm.predict_proba = MagicMock(return_value=np.atleast_2d([0.45, 0.55]))
    proba = ds_test.predict_proba(query)
    assert np.isclose(proba, np.atleast_2d([0.45, 0.55])).all()
Beispiel #13
0
def test_DFP_is_used():
    query = np.atleast_2d([1, 0])
    ds_test = DS(create_pool_classifiers(), DFP=True, safe_k=3)
    ds_test.fit(X_dsel_ex1, y_dsel_ex1)
    ds_test.processed_dsel = dsel_processed_ex1
    ds_test.DSEL_target = y_dsel_ex1
    ds_test.DSEL_data = X_dsel_ex1
    ds_test.neighbors = neighbors_ex1[0, :]
    ds_test.distances = distances_ex1[0, :]
    ds_test.classify_with_ds = MagicMock(return_value=0)
    ds_test.predict(query)
    assert np.array_equal(ds_test.DFP_mask, np.atleast_2d([1, 1, 0]))
Beispiel #14
0
def test_IH_is_used(index, expected):
    query = np.atleast_2d([1, 0])
    ds_test = DS(create_pool_classifiers(), with_IH=True, IH_rate=0.5)
    ds_test.fit(X_dsel_ex1, y_dsel_ex1)

    ds_test.processed_dsel = dsel_processed_ex1
    ds_test.DSEL_target = y_dsel_ex1
    ds_test.DSEL_data = X_dsel_ex1

    ds_test.neighbors = neighbors_ex1[index, :]
    ds_test.distances = distances_ex1[index, :]

    predicted = ds_test.predict(query)

    assert predicted == expected
Beispiel #15
0
def test_label_encoder_only_dsel():
    X_dsel_ex1 = np.array([[-1, 1], [-0.75, 0.5], [-1.5, 1.5]])
    y_dsel_ex1 = np.array(['cat', 'dog', 'plane'])

    query = np.atleast_2d([[1, 0], [-1, -1]])
    ds_test = DS(create_pool_classifiers_dog_cat_plane(), k=2)
    ds_test.fit(X_dsel_ex1, y_dsel_ex1)
    ds_test.neighbors = neighbors_ex1[0, :]
    ds_test.distances = distances_ex1[0, :]
    ds_test.classify_with_ds = Mock()
    ds_test.classify_with_ds.return_value = [
        1, 0
    ]  # changed here due to batch processing
    predictions = ds_test.predict(query)
    assert np.array_equal(predictions, ['dog', 'cat'])
Beispiel #16
0
def test_predict_proba_DFP():
    query = np.atleast_2d([1, 1])
    ds_test = DS(create_pool_classifiers(), DFP=True, safe_k=3)
    ds_test.fit(X_dsel_ex1, y_dsel_ex1)

    # change the state of the system
    ds_test.processed_dsel = dsel_processed_ex1
    ds_test.DSEL_target = y_dsel_ex1
    ds_test.DSEL_data = X_dsel_ex1
    ds_test.neighbors = neighbors_ex1[0, :]
    ds_test.distances = distances_ex1[0, :]

    ds_test.predict_proba_instance = MagicMock(return_value=np.atleast_2d([0.25, 0.75]))
    ds_test.predict_proba(query)
    assert np.array_equal(ds_test.DFP_mask, np.array([1, 1, 0]))
Beispiel #17
0
def test_IH_is_used():
    expected = [0, 0, 1]
    query = np.ones((3, 2))
    ds_test = DS(create_pool_classifiers(), with_IH=True, IH_rate=0.5)
    ds_test.fit(X_dsel_ex1, y_dsel_ex1)

    ds_test.processed_dsel = dsel_processed_ex1
    ds_test.DSEL_target = y_dsel_ex1
    ds_test.DSEL_data = X_dsel_ex1

    ds_test.neighbors = neighbors_ex1
    ds_test.distances = distances_ex1

    predicted = ds_test.predict(query)

    assert np.array_equal(predicted, expected)
Beispiel #18
0
def test_label_encoder_base():
    from sklearn.linear_model import LogisticRegression

    X_dsel_ex1 = np.array([[-1, 1], [-0.75, 0.5], [-1.5, 1.5]])
    y_dsel_ex1 = np.array(['cat', 'dog', 'plane'])

    x = [[-2, -2], [2, 2]]
    y = ['cat', 'dog']
    pool_classifiers = [LogisticRegression().fit(x, y) for _ in range(2)]

    query = np.atleast_2d([[1, 0], [-1, -1]])
    ds_test = DS(pool_classifiers, k=2)
    ds_test.fit(X_dsel_ex1, y_dsel_ex1)
    predictions = ds_test.predict(query)

    assert np.equal(predictions, ['cat', 'dog'])
Beispiel #19
0
def test_output_profiles_transform():
    query = np.array([[1.0, 1.0]])
    ds_test = DS(create_pool_classifiers())
    ds_test.fit(X_dsel_ex1, y_dsel_ex1)
    profile = ds_test._output_profile_transform(query)
    assert np.array_equal(profile, np.array([0.5, 0.5, 1.0, 0.0, 0.33, 0.67]))
Beispiel #20
0
def test_preprocess_dsel_scores():
    ds_test = DS(create_pool_classifiers())
    ds_test.fit(X_dsel_ex1, y_dsel_ex1)
    dsel_scores = ds_test._preprocess_dsel_scores()
    expected = np.ones((15, 6)) * np.array([0.5, 0.5, 1.0, 0.0, 0.33, 0.67])
    assert np.array_equal(dsel_scores, expected)
Beispiel #21
0
def test_get_dsel_scores_all_samples():
    ds_test = DS(create_pool_classifiers())
    ds_test.fit(X_dsel_ex1, y_dsel_ex1)
    ds_test.dsel_scores = dsel_scores_ex1
    expected = np.ones((15, 2)) * 0.5
    assert np.array_equal(ds_test._get_scores_dsel(1), expected)
Beispiel #22
0
def test_get_dsel_scores_not_processed():
    ds_test = DS(create_pool_classifiers())
    ds_test.fit(X_dsel_ex1, y_dsel_ex1)
    with pytest.raises(NotFittedError):
        ds_test._get_scores_dsel(0)
Beispiel #23
0
def test_bad_input_X(X):
    ds_test = DS(create_pool_classifiers())
    ds_test.fit(X_dsel_ex1, y_dsel_ex1)
    with pytest.raises(ValueError):
        ds_test.predict(X)
Beispiel #24
0
def test_input_X_3D():
    X = np.ones((10, 10, 10))
    ds_test = DS(create_pool_classifiers())
    ds_test.fit(X_dsel_ex1, y_dsel_ex1)
    with pytest.raises(ValueError):
        ds_test.predict(X)
Beispiel #25
0
def test_different_input_shape():
    query = np.array([[1.0, 1.0, 2.0]])
    ds_test = DS(create_pool_classifiers())
    ds_test.fit(X_dsel_ex1, y_dsel_ex1)
    with pytest.raises(ValueError):
        ds_test.predict(query)
Beispiel #26
0
def test_input_shape_fit():
    X = X_dsel_ex1
    y = np.ones(20)
    ds_test = DS(create_pool_classifiers())
    with pytest.raises(ValueError):
        ds_test.fit(X, y)