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_batch(example_estimate_competence, create_pool_classifiers): _, y, nn, _, dsel_processed, dsel_scores = example_estimate_competence query = np.ones((3, 1)) meta_test = METADES(pool_classifiers=create_pool_classifiers) meta_test.n_classifiers_ = 3 n_meta_features = 21 meta_test.meta_classifier_ = GaussianNB # Set the state of the system which is set by the fit method. meta_test.DSEL_processed_ = dsel_processed meta_test.dsel_scores_ = dsel_scores meta_test.DSEL_target_ = y nn = nn meta_test._get_similar_out_profiles = MagicMock( return_value=(None, nn[:, 0:meta_test.Kp])) meta_test.compute_meta_features = MagicMock( return_value=np.ones((9, n_meta_features))) meta_test.meta_classifier_.predict_proba = MagicMock( return_value=np.tile([0.0, 0.8], (9, 1))) probabilities = [] for clf in meta_test.pool_classifiers: probabilities.append(clf.predict_proba(query)) probabilities = np.array(probabilities).transpose((1, 0, 2)) expected = np.ones((3, 3)) * 0.8 competences = meta_test.estimate_competence_from_proba( query, nn, probabilities) assert np.array_equal(competences, expected)
def test_estimate_competence_batch(): query = np.ones((3, 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.neighbors = neighbors_ex1 meta_test.distances = distances_ex1 meta_test.n_classes = 2 meta_test.meta_classifier = GaussianNB() meta_test._get_similar_out_profiles = MagicMock( return_value=(None, neighbors_ex1[:, 0:meta_test.Kp])) meta_test.compute_meta_features = MagicMock( return_value=np.ones((9, meta_test.n_meta_features))) meta_test.meta_classifier.predict_proba = MagicMock( return_value=np.tile([0.0, 0.8], (9, 1))) meta_test.DFP_mask = np.array([1, 0, 1]) probabilities = [] for clf in meta_test.pool_classifiers: probabilities.append(clf.predict_proba(query)) probabilities = np.array(probabilities).transpose((1, 0, 2)) expected = np.ones((3, 3)) * 0.8 competences = meta_test.estimate_competence_from_proba( query, probabilities) assert np.array_equal(competences, expected)
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
def test_compute_meta_features(example_estimate_competence, create_pool_classifiers): X, y, nn, _, dsel_processed, dsel_scores = example_estimate_competence query = np.ones((1, 2)) pool = create_pool_classifiers meta_test = METADES(pool_classifiers=[pool[0]]) meta_test.n_classifiers_ = 1 meta_test.k_ = 7 meta_test.Kp_ = 5 # Considering only one classifier in the pool (index = 0) meta_test.DSEL_processed_ = dsel_processed[:, 0].reshape(-1, 1) meta_test.dsel_scores_ = dsel_scores[:, 0, :].reshape(15, 1, 2) meta_test.DSEL_target_ = y meta_test.n_classes_ = 2 neighbors_op = nn[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, nn[0, :], 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))