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_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) target.DFP_mask = np.ones(target.n_classifiers) target._get_region_competence = lambda x: (None, np.tile([0, 1, 2, 3, 4, 5, 6], (n_samples, 1))) competences, diversity = target.estimate_competence(query) 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_batch(): """ 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 """ 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) target.fit(x, y) target.DFP_mask = np.ones(target.n_classifiers) target._get_region_competence = lambda x: (None, np.tile([0, 1, 2, 3, 4, 5, 6], (10, 1))) competences, diversity = target.estimate_competence(query) assert np.allclose(competences, [2. / 7, 4. / 7, 5. / 7]) assert np.allclose(diversity, [-5. / 7, -4. / 7, -3. / 7])