예제 #1
0
def test_predict_proba_selection():
    query = np.array([-1, 1])
    pool_classifiers = create_pool_classifiers() + create_pool_classifiers()
    des_test = DES(pool_classifiers, mode='selection')
    selected_indices = np.array([0, 1, 5])
    selected_classifiers = np.zeros((1,6), dtype=bool)
    selected_classifiers[0, selected_indices] = 1
    des_test.select = MagicMock(return_value=selected_classifiers)

    des_test.n_classes = 2
    expected = np.array([0.61, 0.39])

    predictions = []
    probabilities = []
    for clf in des_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 = des_test.predict_proba_with_ds(query, predictions, probabilities)
    assert np.isclose(predicted_proba, expected, atol=0.01).all()
예제 #2
0
def test_predict_proba_hybrid():
    query = np.array([-1, 1])
    pool_classifiers = create_pool_classifiers() + create_pool_classifiers()
    des_test = DES(pool_classifiers, mode='hybrid')
    des_test.n_classes = 2

    selected_indices = [0, 1, 5]
    competences = np.array([[0.55, 1.0, 0.2, 0.60, 0.75, 0.3]])

    expected = np.array([0.5744, 0.4256])

    des_test.estimate_competence = MagicMock(return_value=competences)

    selected_classifiers = np.zeros((1, 6), dtype=bool)
    selected_classifiers[0, selected_indices] = 1
    des_test.select = MagicMock(return_value=selected_classifiers)

    predictions = []
    probabilities = []
    for clf in des_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 = des_test.predict_proba_with_ds(query, predictions, probabilities)
    assert np.isclose(predicted_proba, expected, atol=0.01).all()
예제 #3
0
def test_predict_proba_selection():
    query = np.array([-1, 1])
    pool_classifiers = create_pool_classifiers() + create_pool_classifiers()
    des_test = DES(pool_classifiers, mode='selection')
    selected_indices = [0, 1, 5]
    des_test.select = MagicMock(return_value=selected_indices)

    des_test.n_classes = 2

    expected = np.array([0.61, 0.39])
    predicted_proba = des_test.predict_proba_instance(query)
    assert np.isclose(predicted_proba, expected, atol=0.01).all()
예제 #4
0
def test_classify_instance_weighting():
    query = np.atleast_2d([-1, 1])

    pool_classifiers = create_pool_classifiers() + create_pool_classifiers()
    des_test = DES(pool_classifiers, mode='weighting')
    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_instance(query, np.array(predictions))
    assert predicted_label == 1.0
예제 #5
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
예제 #6
0
def test_classify_instance_selection():
    query = np.atleast_2d([-1, 1])
    pool_classifiers = create_pool_classifiers() + create_pool_classifiers()
    des_test = DES(pool_classifiers, mode='selection')
    # competences = [0.55, 1.0, 0.2, 0.65, 0.75, 0.8]
    selected_index = [0, 1, 5]
    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_instance(query, np.array(predictions))
    assert predicted_label == 0.0
예제 #7
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
예제 #8
0
def test_predict_proba_hybrid():
    query = np.array([-1, 1])
    pool_classifiers = create_pool_classifiers() + create_pool_classifiers()
    des_test = DES(pool_classifiers, mode='hybrid')
    des_test.n_classes = 2

    selected_indices = [0, 1, 5]
    competences = np.array([0.55, 1.0, 0.2, 0.60, 0.75, 0.3])

    expected = np.array([0.5744, 0.4256])

    des_test.estimate_competence = MagicMock(return_value=competences)
    des_test.select = MagicMock(return_value=selected_indices)

    predicted_proba = des_test.predict_proba_instance(query)
    assert np.isclose(predicted_proba, expected, atol=0.01).all()
예제 #9
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)
예제 #10
0
def test_diversity_metric_ratio():
    test = DESClustering(create_pool_classifiers() * 10, metric='ratio')
    # Mocking this method to avoid preprocessing the cluster information that
    # is not required in this test.
    test._preprocess_clusters = MagicMock(return_value=1)
    test.fit(X_dsel_ex1, y_dsel_ex1)
    assert test.diversity_func_ == ratio_errors
예제 #11
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
예제 #12
0
def test_classify_instance():
    query = np.atleast_2d([1, -1])
    clustering_test = DESClustering(create_pool_classifiers() * 4, k=2)
    clustering_test.select = MagicMock(return_value=[0, 1, 2, 3, 5, 6, 7, 9])

    predicted = clustering_test.classify_instance(query)
    assert predicted == 0
예제 #13
0
def test_classify_with_ds_single_sample():
    query = np.ones(2)
    predictions = np.array([0, 1, 0])

    desknn_test = DESClustering(create_pool_classifiers())
    desknn_test.select = MagicMock(return_value=np.array([[0, 2]]))
    result = desknn_test.classify_with_ds(query, predictions)
    assert np.allclose(result, 0)
예제 #14
0
def test_proba_with_ds_diff_sizes():
    query = np.ones((10, 2))
    predictions = np.ones((5, 3))
    probabilities = np.ones((5, 3, 2))
    des_test = BaseDES(create_pool_classifiers())

    with pytest.raises(ValueError):
        des_test.predict_proba_with_ds(query, predictions, probabilities)
예제 #15
0
def test_fit():
    X = X_dsel_ex1
    y = y_dsel_ex1
    pool_classifiers = create_pool_classifiers()
    single_best_test = SingleBest(pool_classifiers)
    single_best_test.fit(X, y)

    assert single_best_test.best_clf_index == 0 or single_best_test.best_clf_index == 2
예제 #16
0
def test_predict():
    X = X_dsel_ex1
    y = y_dsel_ex1
    oracle_test = Oracle(create_pool_classifiers())
    predicted_labels = oracle_test.predict(X, y)
    assert np.equal(predicted_labels, y).all()

    assert oracle_test.score(X, y) == 1.0
예제 #17
0
def test_select():
    query = np.atleast_2d([1, -1])
    clustering_test = DESClustering(create_pool_classifiers() * 2, k=2)
    clustering_test.roc_algorithm.predict = MagicMock(return_value=[0])
    clustering_test.indices = np.array([[0, 2], [1, 4]])
    assert np.array_equal(clustering_test.select(query), [0, 2])

    clustering_test.roc_algorithm.predict = MagicMock(return_value=[1])
    assert np.array_equal(clustering_test.select(query), [1, 4])
예제 #18
0
def test_predict():
    X = X_dsel_ex1
    y = y_dsel_ex1
    pool_classifiers = create_pool_classifiers()
    single_best_test = SingleBest(pool_classifiers)
    single_best_test.fit(X, y)

    predicted_labels = single_best_test.predict(X)
    assert np.equal(predicted_labels, 0).all()
예제 #19
0
def test_predict_proba():
    X = X_dsel_ex1
    y = y_dsel_ex1
    pool_classifiers = create_pool_classifiers()
    single_best_test = SingleBest(pool_classifiers)
    single_best_test.fit(X, y)

    predicted_proba = single_best_test.predict_proba(X)
    assert np.equal(predicted_proba, pool_classifiers[0].predict_proba(X)).all()
예제 #20
0
def test_predict_proba_weighting():
    query = np.array([-1, 1])
    pool_classifiers = create_pool_classifiers()
    des_test = DES(pool_classifiers, mode='weighting')
    competences = np.array([0.5, 1.0, 0.2])
    des_test.estimate_competence = MagicMock(return_value=competences)
    des_test.n_classes = 2
    expected = np.array([0.5769, 0.4231])
    predicted_proba = des_test.predict_proba_instance(query)
    assert np.isclose(predicted_proba, expected, atol=0.01).all()
예제 #21
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)
예제 #22
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
예제 #23
0
def test_classify_instance():
    query = np.atleast_2d([1, -1])
    clustering_test = DESClustering(create_pool_classifiers() * 4, k=2)
    clustering_test.select = MagicMock(return_value=[0, 1, 2, 3, 5, 6, 7, 9])

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

    predicted = clustering_test.classify_instance(query, np.array(predictions))
    assert predicted == 0
예제 #24
0
def test_classify_instance():
    query = np.ones((1, 2))
    clustering_test = DESClustering(create_pool_classifiers() * 4,
                                    clustering=KMeans(n_clusters=2))

    clustering_test.select = MagicMock(return_value=[0, 1, 2, 3, 5, 6, 7, 9])
    predictions = []
    for clf in clustering_test.pool_classifiers:
        predictions.append(clf.predict(query)[0])

    predicted = clustering_test.classify_with_ds(query, np.array(predictions))
    assert predicted == 0
예제 #25
0
def test_fit_homogeneous_clusters():

    clustering_test = DESClustering(create_pool_classifiers()*2, k=2, pct_accuracy=0.5, pct_diversity=0.33)
    clustering_test.roc_algorithm.fit_predict = MagicMock(return_value=return_cluster_index_ex1)

    clustering_test.DFP_mask = np.ones(clustering_test.n_classifiers)
    clustering_test.fit(X_dsel_ex1, y_dsel_ex1)

    assert clustering_test.accuracy_cluster[0, 1] == 0.0 and clustering_test.accuracy_cluster[0, [0, 2]].all() == 1.0
    assert clustering_test.accuracy_cluster[1, 1] == 1.0 and clustering_test.accuracy_cluster[1, [0, 2]].all() == 0.0
    for idx in clustering_test.indices[0, :]:
        assert idx in (0, 2, 3, 5)
예제 #26
0
def test_select():
    query = np.atleast_2d([1, -1])
    clustering_test = DESClustering(create_pool_classifiers() * 2,
                                    clustering=KMeans(n_clusters=2))

    clustering_test.clustering_ = KMeans()
    clustering_test.clustering_.predict = MagicMock(return_value=[0])
    clustering_test.indices_ = np.array([[0, 2], [1, 4]])
    assert np.array_equal(clustering_test.select(query), [[0, 2]])

    clustering_test.clustering_.predict = MagicMock(return_value=[1])
    assert np.array_equal(clustering_test.select(query), [[1, 4]])
예제 #27
0
def test_fit_heterogeneous_clusters():

    clustering_test = DESClustering(create_pool_classifiers(), k=2, pct_accuracy=0.5, pct_diversity=0.33)
    clustering_test.roc_algorithm.fit_predict = MagicMock(return_value=return_cluster_index_ex2)

    clustering_test.DFP_mask = np.ones(clustering_test.n_classifiers)
    clustering_test.fit(X_dsel_ex1, y_dsel_ex1)

    # Index selected should be of any classifier that predicts the class label 0
    assert np.isclose(clustering_test.accuracy_cluster[:, 1], [0.428, 0.375], atol=0.01).all()
    assert np.isclose(clustering_test.accuracy_cluster[:, 0], [0.572, 0.625], atol=0.01).all()
    assert clustering_test.indices[0, 0] == 0 or clustering_test.indices[0, 0] == 2
    assert clustering_test.indices[1, 0] == 0 or clustering_test.indices[1, 0] == 2
예제 #28
0
def test_estimate_competence():

    query = np.atleast_2d([1, 1])
    clustering_test = DESClustering(create_pool_classifiers(), k=2, pct_accuracy=0.5, pct_diversity=0.33)
    clustering_test.roc_algorithm.fit_predict = MagicMock(return_value=return_cluster_index_ex2)

    clustering_test.DFP_mask = np.ones(clustering_test.n_classifiers)
    clustering_test.fit(X_dsel_ex1, y_dsel_ex1)
    clustering_test.roc_algorithm.predict = MagicMock(return_value=0)
    competences = clustering_test.estimate_competence(query)

    assert np.array_equal(competences, clustering_test.accuracy_cluster[0, :])

    clustering_test.roc_algorithm.predict = MagicMock(return_value=1)
    competences = clustering_test.estimate_competence(query)
    assert np.array_equal(competences, clustering_test.accuracy_cluster[1, :])
예제 #29
0
def test_fit_clusters_less_diverse():
    clustering_test = DESClustering(create_pool_classifiers() * 2,
                                    clustering=KMeans(n_clusters=2),
                                    pct_accuracy=0.5,
                                    pct_diversity=0.33,
                                    more_diverse=False)

    clustering_test.clustering.predict = MagicMock(
        return_value=return_cluster_index_ex1)
    clustering_test.fit(X_dsel_ex1, y_dsel_ex1)

    assert (clustering_test.accuracy_cluster_[0, 1] == 0.0
            and clustering_test.accuracy_cluster_[0, [0, 2]].all() == 1.0)
    assert (clustering_test.accuracy_cluster_[1, 1] == 1.0
            and clustering_test.accuracy_cluster_[1, [0, 2]].all() == 0.0)
    for idx in clustering_test.indices_[0, :]:
        assert idx in (1, 3, 4, 5)
예제 #30
0
def test_estimate_competence():
    query = np.atleast_2d([1, 1])
    clustering_test = DESClustering(create_pool_classifiers() * 2,
                                    clustering=KMeans(n_clusters=2),
                                    pct_accuracy=0.5,
                                    pct_diversity=0.33)

    # Keep the original predict method to change after
    clustering_test.clustering.predict = MagicMock(
        return_value=return_cluster_index_ex2)
    clustering_test.fit(X_dsel_ex1, y_dsel_ex1)

    clustering_test.clustering_.predict = MagicMock(return_value=0)
    competences = clustering_test.estimate_competence(query)

    assert np.array_equal(competences, clustering_test.accuracy_cluster_[0, :])

    clustering_test.clustering_.predict = MagicMock(return_value=1)
    competences = clustering_test.estimate_competence(query)
    assert np.array_equal(competences, clustering_test.accuracy_cluster_[1, :])