def test_estimate_competence_ratio_batch(): n_samples = 10 query = np.ones((n_samples, 2)) x = np.array([0, 1, 2, 3, 4, 5, 6]).reshape(-1, 1) y = np.array([0, 0, 0, 0, 1, 1, 1]) clf1 = create_base_classifier(np.array([1, 0, 1, 0, 0, 0, 0])) clf2 = create_base_classifier(np.array([1, 0, 0, 0, 1, 0, 0])) clf3 = create_base_classifier(np.array([0, 0, 1, 0, 1, 1, 0])) pool_classifiers = [clf1, clf2, clf3] target = DESKNN(pool_classifiers, k=7, pct_accuracy=1, pct_diversity=1, metric='ratio') target.fit(x, y) neighbors = np.tile([0, 1, 2, 3, 4, 5, 6], (n_samples, 1)) competences, diversity = target.estimate_competence(query, neighbors) assert np.allclose(competences, [2. / 7, 4. / 7, 5. / 7]) assert np.allclose(diversity, [2.166, 3.666, 4.500], atol=0.01)
def test_estimate_competence(): """ Test case: Correct labels: 0000111 classifier 1: 1010000 (2/7 correct) classifier 2: 1000100 (4/7 correct) classifier 2: 0010110 (5/7 correct) Diversity: compute number of common errors (on both classifiers) and divide by 7: clf1 x clf2: 3/7 clf1 x clf3: 2/7 clf2 x clf3: 1/7 clf1 diversity = (3+2)/7 = -5/7 (negative because we use the negative of double error) clf2 diversity = (3+1)/7 = -4/7 clf3 diversity = (2+1)/7 = -3/7 """ query = np.ones((1, 2)) x = np.array([0, 1, 2, 3, 4, 5, 6]).reshape(-1, 1) y = np.array([0, 0, 0, 0, 1, 1, 1]) clf1 = create_base_classifier(np.array([1, 0, 1, 0, 0, 0, 0])) clf2 = create_base_classifier(np.array([1, 0, 0, 0, 1, 0, 0])) clf3 = create_base_classifier(np.array([0, 0, 1, 0, 1, 1, 0])) pool_classifiers = [clf1, clf2, clf3] target = DESKNN(pool_classifiers, k=7, pct_accuracy=1, pct_diversity=1) target.fit(x, y) neighbors = np.array([[0, 1, 2, 3, 4, 5, 6]]) competences, diversity = target.estimate_competence(query, neighbors) assert np.allclose(competences, [2. / 7, 4. / 7, 5. / 7]) assert np.allclose(diversity, [-5. / 7, -4. / 7, -3. / 7])
def test_estimate_competence_Q(): query = np.ones((1, 2)) x = np.array([0, 1, 2, 3, 4, 5, 6]).reshape(-1, 1) y = np.array([0, 0, 0, 0, 1, 1, 1]) clf1 = create_base_classifier(np.array([1, 0, 1, 0, 0, 0, 0])) clf2 = create_base_classifier(np.array([1, 0, 0, 0, 1, 0, 0])) clf3 = create_base_classifier(np.array([0, 0, 1, 0, 1, 1, 0])) pool_classifiers = [clf1, clf2, clf3] target = DESKNN(pool_classifiers, k=7, pct_accuracy=1, pct_diversity=1, metric='Q') target.fit(x, y) target.DFP_mask = np.ones(target.n_classifiers) target._get_region_competence = lambda x: ( None, np.array([[0, 1, 2, 3, 4, 5, 6]])) competences, diversity = target.estimate_competence(query) assert np.allclose(competences, [2. / 7, 4. / 7, 5. / 7]) assert np.allclose(diversity, [2, 1.2, 1.2])
def test_desknn_proba(): pool_classifiers, X_dsel, y_dsel, X_test, y_test = setup_classifiers() desknn = DESKNN(pool_classifiers, DFP=True) desknn.fit(X_dsel, y_dsel) probas = desknn.predict_proba(X_test) expected = np.load('deslib/tests/expected_values/desknn_probas_DFP.npy') assert np.allclose(probas, expected)
def test_J_higher_than_N(): X = np.random.rand(10, 2) y = np.ones(10) with pytest.raises(ValueError): desknn = DESKNN([create_base_classifier(1)] * 100, pct_accuracy=0.3, pct_diversity=0.5) desknn.fit(X, y)
def test_desknn_proba(knn_methods): pool_classifiers, X_dsel, y_dsel, X_test, y_test = setup_classifiers() desknn = DESKNN(pool_classifiers, knn_classifier=knn_methods) desknn.fit(X_dsel, y_dsel) probas = desknn.predict_proba(X_test) expected = np.load( 'deslib/tests/expected_values/desknn_proba_integration.npy') assert np.allclose(probas, expected)
def test_estimate_competence_Q(): x = np.array([0, 1, 2, 3, 4, 5, 6]).reshape(-1, 1) y = np.array([0, 0, 0, 0, 1, 1, 1]) clf1 = create_base_classifier(np.array([1, 0, 1, 0, 0, 0, 0])) clf2 = create_base_classifier(np.array([1, 0, 0, 0, 1, 0, 0])) clf3 = create_base_classifier(np.array([0, 0, 1, 0, 1, 1, 0])) pool_classifiers = [clf1, clf2, clf3] target = DESKNN(pool_classifiers, k=7, pct_accuracy=1, pct_diversity=1, metric='Q') target.fit(x, y) neighbors = np.array([[0, 1, 2, 3, 4, 5, 6]]) competences, diversity = target.estimate_competence(neighbors) assert np.allclose(competences, [2. / 7, 4. / 7, 5. / 7]) assert np.allclose(diversity, [2, 1.2, 1.2])
def test_estimate_competence_ratio(): x = np.array([0, 1, 2, 3, 4, 5, 6]).reshape(-1, 1) y = np.array([0, 0, 0, 0, 1, 1, 1]) clf1 = create_base_classifier(np.array([1, 0, 1, 0, 0, 0, 0])) clf2 = create_base_classifier(np.array([1, 0, 0, 0, 1, 0, 0])) clf3 = create_base_classifier(np.array([0, 0, 1, 0, 1, 1, 0])) pool_classifiers = [clf1, clf2, clf3] target = DESKNN(pool_classifiers, k=7, pct_accuracy=1, pct_diversity=1, metric='Ratio') target.fit(x, y) target.DFP_mask = np.ones(target.n_classifiers) target._get_region_competence = lambda x: (None, [0, 1, 2, 3, 4, 5, 6]) competences, diversity = target.estimate_competence(2) assert np.allclose(competences, [2./7, 4./7, 5./7]) assert np.allclose(diversity, [2.166, 3.666, 4.500], atol=0.01)
def test_desknn(): pool_classifiers, X_dsel, y_dsel, X_test, y_test = setup_classifiers() desknn = DESKNN(pool_classifiers, DFP=True) desknn.fit(X_dsel, y_dsel) assert np.isclose(desknn.score(X_test, y_test), 0.89393939393939392)
def test_desknn(knn_methods): pool_classifiers, X_dsel, y_dsel, X_test, y_test = setup_classifiers() desknn = DESKNN(pool_classifiers, knn_classifier=knn_methods) desknn.fit(X_dsel, y_dsel) assert np.isclose(desknn.score(X_test, y_test), 0.97340425531914898)
def test_pect_zero(): X = np.random.rand(10, 2) y = np.ones(10) with pytest.raises(ValueError): desknn = DESKNN(pct_accuracy=0.0, pct_diversity=0.0) desknn.fit(X, y)
def test_J_higher_than_N(): X = np.random.rand(10, 2) y = np.ones(10) with pytest.raises(ValueError): desknn = DESKNN(pct_accuracy=0.3, pct_diversity=0.5) desknn.fit(X, y)
def test_input_diversity_parameter(): X = np.random.rand(10, 2) y = np.ones(10) with pytest.raises(ValueError): desknn = DESKNN(metric='abc') desknn.fit(X, y)
def test_input_diversity_parameter(): X = np.random.rand(10, 2) y = np.ones(10) with pytest.raises(ValueError): desknn = DESKNN([create_base_classifier(1)] * 100, metric='abc') desknn.fit(X, y)