def test_estimate_competence(): query = np.ones((1, 2)) meta_test = METADES(create_pool_classifiers()) # Set the state of the system which is set by the fit method. meta_test.processed_dsel = dsel_processed_ex1 meta_test.dsel_scores = dsel_scores_ex1 meta_test.DSEL_target = y_dsel_ex1 meta_test.n_classes = 2 meta_test.meta_classifier = GaussianNB() meta_test.neighbors = neighbors_ex1[0, :] meta_test.distances = distances_ex1[0, :] meta_test._get_similar_out_profiles = MagicMock( return_value=(None, neighbors_ex1[0, 0:meta_test.Kp])) meta_test.meta_classifier.predict_proba = MagicMock( return_value=np.array([[0.2, 0.8], [1.0, 0.0], [0.2, 0.8]])) probabilities = [] for clf in meta_test.pool_classifiers: probabilities.append(clf.predict_proba(query)) probabilities = np.array(probabilities).transpose((1, 0, 2)) expected = np.array([[0.8, 0.0, 0.8]]) competences = meta_test.estimate_competence_from_proba( query, probabilities) assert np.array_equal(competences, expected)
def test_compute_meta_features(): query = np.ones((1, 2)) pool = create_pool_classifiers() meta_test = METADES([pool[0]]) # Considering only one classifier in the pool (index = 0) meta_test.processed_dsel = dsel_processed_ex1[:, 0].reshape(-1, 1) meta_test.dsel_scores = dsel_scores_ex1[:, 0, :].reshape( 15, 1, 2) # 15 samples, 1 base classifier, 2 classes meta_test.DSEL_target = y_dsel_ex1 meta_test.n_classes = 2 neighbors = neighbors_ex1[0, :] neighbors_op = neighbors_ex1[2, 0:meta_test.Kp] # Expected values for each meta feature based on the data of ex1. expected_f1 = [1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0] expected_f2 = [1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0] expected_f3 = [4.0 / 7.0] expected_f4 = [0.0, 1.0, 1.0, 1.0, 0.0] expected_f5 = [0.5] scores = np.empty( (query.shape[0], meta_test.n_classifiers, meta_test.n_classes)) for index, clf in enumerate(meta_test.pool_classifiers): scores[:, index, :] = clf.predict_proba(query) meta_features = meta_test.compute_meta_features(scores, neighbors, neighbors_op) expected = np.asarray(expected_f1 + expected_f2 + expected_f3 + expected_f4 + expected_f5) assert np.array_equal(meta_features, expected.reshape(1, -1))
def test_estimate_competence(): query = np.atleast_2d([1, 1]) meta_test = METADES(create_pool_classifiers()) # Set the state of the system which is set by the fit method. meta_test.processed_dsel = dsel_processed_ex1 meta_test.dsel_scores = dsel_scores_ex1 meta_test.DSEL_target = y_dsel_ex1 meta_test.n_classes = 3 meta_test.meta_classifier = GaussianNB() meta_test.neighbors = neighbors_ex1 meta_test.distances = distances_ex1 meta_test._get_similar_out_profiles = MagicMock( return_value=[0, neighbors_ex1[2, 0:meta_test.Kp]]) meta_test.meta_classifier.predict_proba = MagicMock( return_value=np.array([[0.0, 0.8]])) meta_test.DFP_mask = np.array([1, 0, 1]) competences = meta_test.estimate_competence(query) assert np.allclose(competences, [0.8, 0.0, 0.8])
def test_compute_meta_features(): query = np.atleast_2d([1, 2]) pool = create_pool_classifiers() meta_test = METADES(pool) meta_test.processed_dsel = dsel_processed_ex1 meta_test.dsel_scores = dsel_scores_ex1 meta_test.DSEL_target = y_dsel_ex1 meta_test.n_classes = 3 neighbors = neighbors_ex1[0, :] neighbors_op = neighbors_ex1[2, 0:meta_test.Kp] # Expected values for each meta feature based on the data of ex1. expected_f1 = [1, 0, 1, 1, 1, 0, 0] expected_f2 = [1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0] expected_f3 = [4.0 / 7.0] expected_f4 = [0, 1, 1, 1, 0] expected_f5 = [0.5] meta_features = meta_test.compute_meta_features(query, neighbors, neighbors_op, pool[0], 0) expected = expected_f1 + expected_f2 + expected_f3 + expected_f4 + expected_f5 assert meta_features == expected