def test_estimate_competence_kuncheva_ex(): query = np.atleast_2d([1, 1]) mla_test = MLA([create_base_classifier(return_value=1)] * 2, k=k_ex_kuncheva) mla_test.n_classifiers_ = 2 mla_test.DSEL_processed_ = np.repeat(dsel_processed_kuncheva, 2, axis=1) mla_test.DSEL_scores = dsel_scores_ex_kuncheva mla_test.DSEL_target_ = y_dsel_ex_kuncheva_dependent mla_test.n_classes_ = n_classes_ex_kuncheva neighbors = neighbors_ex_kuncheva.reshape(1, -1) distances = distances_ex_kuncheva.reshape(1, -1) predictions = [] for clf in mla_test.pool_classifiers: predictions.append(clf.predict(query)[0]) predictions = np.array(predictions) competences = mla_test.estimate_competence(query, neighbors, distances=distances, predictions=predictions) assert np.allclose(competences, [0.95, 0.95], atol=0.01)
def test_estimate_competence_batch(): query = np.array([[1, 1], [1, 1], [1, 1]]) expected = np.array([[0.750, 0.666, 0.750], [0.800, 1.000, 0.800], [1.000, 0.600, 0.500]]) mla_test = MLA(create_pool_classifiers()) mla_test.n_classifiers_ = 3 mla_test.DSEL_processed_ = dsel_processed_ex1 mla_test.DSEL_scores = dsel_scores_all_ones mla_test.DSEL_target_ = y_dsel_ex1 mla_test.n_classes_ = 2 neighbors = neighbors_ex1 distances = distances_all_ones predictions = [] for clf in mla_test.pool_classifiers: predictions.append(clf.predict(query)[0]) predictions = np.array(predictions) competences = mla_test.estimate_competence(query, neighbors, distances=distances, predictions=predictions) assert np.allclose(competences, expected, atol=0.01)
def test_estimate_competence_all_ones(index, example_all_ones): _, y, neighbors, distances, dsel_processed, dsel_scores = example_all_ones query = np.atleast_2d([1, 1]) mla_test = MLA() mla_test.n_classifiers_ = 3 mla_test.DSEL_processed_ = dsel_processed mla_test.DSEL_scores = dsel_scores mla_test.DSEL_target_ = y mla_test.n_classes_ = 2 neighbors = neighbors[index, :].reshape(1, -1) distances = distances[index, :].reshape(1, -1) expected = [1.0, 1.0, 1.0] predictions = np.array([[0, 1, 0]]) competences = mla_test.estimate_competence(query, neighbors, distances=distances, predictions=predictions) assert np.isclose(competences, expected).all()
def test_estimate_competence_all_ones(index): query = np.atleast_2d([1, 1]) mla_test = MLA(create_pool_classifiers()) mla_test.n_classifiers_ = 3 mla_test.DSEL_processed_ = dsel_processed_all_ones mla_test.DSEL_scores = dsel_scores_all_ones mla_test.DSEL_target_ = y_dsel_ex1 mla_test.n_classes_ = 2 neighbors = neighbors_ex1[index, :].reshape(1, -1) distances = distances_all_ones[index, :].reshape(1, -1) expected = [1.0, 1.0, 1.0] predictions = [] for clf in mla_test.pool_classifiers: predictions.append(clf.predict(query)) predictions = np.array(predictions) competences = mla_test.estimate_competence(query, neighbors, distances=distances, predictions=predictions) assert np.isclose(competences, expected).all()
def test_estimate_competence_kuncheva_ex(example_kuncheva): query = np.atleast_2d([1, 1]) example_kuncheva = example_kuncheva mla_test = MLA(k=example_kuncheva['k']) mla_test.n_classifiers_ = 2 mla_test.DSEL_processed_ = np.repeat(example_kuncheva['dsel_processed'], 2, axis=1) mla_test.dsel_scores_ = example_kuncheva['dsel_scores'] mla_test.DSEL_target_ = example_kuncheva['y_dependent'] mla_test.n_classes_ = example_kuncheva['n_classes'] neighbors = example_kuncheva['neighbors'].reshape(1, -1) distances = example_kuncheva['distances'].reshape(1, -1) predictions = np.array([[1, 1]]) competences = mla_test.estimate_competence(query, neighbors, distances=distances, predictions=predictions) assert np.allclose(competences, [0.95, 0.95], atol=0.01)
def test_estimate_competence(index, expected): query = np.array([1, 1]) mla_test = MLA(create_pool_classifiers()) mla_test.processed_dsel = dsel_processed_ex1 mla_test.dsel_scores = dsel_scores_all_ones mla_test.DSEL_target = y_dsel_ex1 mla_test.n_classes = 2 mla_test.neighbors = neighbors_ex1[index, :] mla_test.distances = distances_all_ones[index, :] mla_test.DFP_mask = [1, 1, 1] competences = mla_test.estimate_competence(query.reshape(1, -1)) assert np.isclose(competences, expected).all()
def test_estimate_competence_kuncheva_ex(): query = np.array([1, 1]) mla_test = MLA([create_base_classifier(return_value=1)], k=k_ex_kuncheva) mla_test.processed_dsel = dsel_processed_kuncheva mla_test.dsel_scores = dsel_scores_ex_kuncheva mla_test.DSEL_target = y_dsel_ex_kuncheva_dependent mla_test.n_classes = n_classes_ex_kuncheva mla_test.neighbors = neighbors_ex_kuncheva mla_test.distances = distances_ex_kuncheva mla_test.DFP_mask = [1] competences = mla_test.estimate_competence(query.reshape(1, -1)) assert np.isclose(competences, 0.95, atol=0.01)
def __init__( self, name: str, model_params: Dict[str, Any], classifier_paths: Iterable[Tuple[str, str]], ) -> None: super().__init__(name, model_params, classifier_paths) self._selector = MLA(self.classifiers, **model_params)
def test_estimate_competence_kuncheva_ex(): query = np.atleast_2d([1, 1]) mla_test = MLA([create_base_classifier(return_value=1)], k=k_ex_kuncheva) mla_test.processed_dsel = dsel_processed_kuncheva mla_test.dsel_scores = dsel_scores_ex_kuncheva mla_test.DSEL_target = y_dsel_ex_kuncheva_dependent mla_test.n_classes = n_classes_ex_kuncheva mla_test.neighbors = neighbors_ex_kuncheva mla_test.distances = distances_ex_kuncheva mla_test.DFP_mask = [1] predictions = [] for clf in mla_test.pool_classifiers: predictions.append(clf.predict(query)[0]) competences = mla_test.estimate_competence( query, predictions=np.array(predictions)) assert np.isclose(competences, 0.95, atol=0.01)
def test_estimate_competence(index, expected): query = np.atleast_2d([1, 1]) mla_test = MLA(create_pool_classifiers()) mla_test.processed_dsel = dsel_processed_ex1 mla_test.dsel_scores = dsel_scores_all_ones mla_test.DSEL_target = y_dsel_ex1 mla_test.n_classes = 2 mla_test.neighbors = neighbors_ex1[index, :] mla_test.distances = distances_all_ones[index, :] mla_test.DFP_mask = [1, 1, 1] predictions = [] for clf in mla_test.pool_classifiers: predictions.append(clf.predict(query)[0]) competences = mla_test.estimate_competence( query, predictions=np.array(predictions)) assert np.isclose(competences, expected).all()
def test_estimate_competence_diff_target(index): query = np.array([1, 1]) mla_test = MLA(create_pool_classifiers()) mla_test.processed_dsel = dsel_processed_ex1 mla_test.DSEL_target = np.ones(15, dtype=int) * 3 mla_test.neighbors = neighbors_ex1[index, :] mla_test.distances = distances_ex1[index, :] mla_test.DFP_mask = [1, 1, 1] expected = [0.0, 0.0, 0.0] competences = mla_test.estimate_competence(query.reshape(1, -1)) assert np.isclose(competences, expected).all()
def test_estimate_competence_diff_target(index): query = np.atleast_2d([1, 1]) mla_test = MLA(create_pool_classifiers()) mla_test.processed_dsel = dsel_processed_ex1 mla_test.DSEL_target = np.ones(15, dtype=int) * 3 mla_test.neighbors = neighbors_ex1[index, :] mla_test.distances = distances_ex1[index, :] mla_test.DFP_mask = [1, 1, 1] expected = [0.0, 0.0, 0.0] predictions = [] for clf in mla_test.pool_classifiers: predictions.append(clf.predict(query)[0]) competences = mla_test.estimate_competence( query, predictions=np.array(predictions)) assert np.isclose(competences, expected).all()
def test_estimate_competence_batch(example_estimate_competence): _, y, neighbors, _, dsel_processed, _ = example_estimate_competence expected = np.array([[0.750, 0.666, 0.750], [0.800, 1.000, 0.800], [1.000, 0.600, 0.500]]) mla_test = MLA() mla_test.n_classifiers_ = 3 mla_test.DSEL_processed_ = dsel_processed distances = np.ones((3, 7)) mla_test.DSEL_target_ = y mla_test.n_classes_ = 2 predictions = np.array([[0, 1, 0]]) competences = mla_test.estimate_competence(competence_region=neighbors, distances=distances, predictions=predictions) assert np.allclose(competences, expected, atol=0.01)
def test_estimate_competence_diff_target(index, example_estimate_competence): _, _, neighbors, distances, dsel_processed, _ = example_estimate_competence mla_test = MLA() mla_test.n_classifiers_ = 3 mla_test.DSEL_processed_ = dsel_processed mla_test.DSEL_target_ = np.ones(15, dtype=int) * 3 neighbors = neighbors[index, :].reshape(1, -1) distances = distances[index, :].reshape(1, -1) expected = [0.0, 0.0, 0.0] predictions = np.array([[0, 1, 0]]) competences = mla_test.estimate_competence(neighbors, distances=distances, predictions=predictions) assert np.isclose(competences, expected).all()
def test_estimate_competence_diff_target(index): query = np.atleast_2d([1, 1]) mla_test = MLA(create_pool_classifiers()) mla_test.n_classifiers_ = 3 mla_test.DSEL_processed_ = dsel_processed_ex1 mla_test.DSEL_target_ = np.ones(15, dtype=int) * 3 neighbors = neighbors_ex1[index, :].reshape(1, -1) distances = distances_ex1[index, :].reshape(1, -1) expected = [0.0, 0.0, 0.0] predictions = [] for clf in mla_test.pool_classifiers: predictions.append(clf.predict(query)[0]) predictions = np.array(predictions) competences = mla_test.estimate_competence(query, neighbors, distances=distances, predictions=predictions) assert np.isclose(competences, expected).all()
def test_check_estimator(): check_estimator(MLA())
def test_predict_proba(create_X_y): X, y = create_X_y clf1 = Perceptron() clf1.fit(X, y) MLA([clf1, clf1]).fit(X, y)
def test_MLA(): pool_classifiers, X_dsel, y_dsel, X_test, y_test = setup_classifiers() mla = MLA(pool_classifiers, DFP=True) mla.fit(X_dsel, y_dsel) assert np.isclose(mla.score(X_test, y_test), 0.88787878787878793)
def _generate_local_pool(self, query): """ Local pool generation. This procedure populates the "pool_classifiers" based on the query sample's neighborhood. Thus, for each query sample, a different pool is created. In each iteration, the training samples near the query sample are singled out and a subpool is generated using the Self-Generating Hyperplanes (SGH) method. Then, the DCS technique selects the best classifier in the generated subpool and it is added to the local pool. In the following iteration, the neighborhood is increased and another SGH-generated subpool is obtained over the new neighborhood, and again the DCS technique singles out the best in it, which is then added to the local pool. This process is repeated until the pool reaches "n_classifiers". Parameters ---------- query : array of shape = [n_features] The test sample. Returns ------- self References ---------- M. A. Souza, G. D. Cavalcanti, R. M. Cruz, R. Sabourin, On the characterization of the oracle for dynamic classi er selection, in: International Joint Conference on Neural Networks, IEEE, 2017, pp. 332-339. """ n_samples, _ = self.DSEL_data.shape self.pool_classifiers = [] n_err = 0 max_err = 2 * self.n_classifiers curr_k = self.k # Classifier count n = 0 while n < self.n_classifiers and n_err < max_err: subpool = SGH() included_samples = np.zeros((n_samples), int) if self.knne: idx_neighb = np.array([], dtype=int) # Obtain neighbors of each class individually for j in np.arange(0, self.n_classes): # Obtain neighbors from the classes in the RoC if np.any(self.classes[j] == self.DSEL_target[ self.neighbors[0][np.arange(0, curr_k)]]): nc = np.where(self.classes[j] == self.DSEL_target[ self.neighbors[0]]) idx_nc = self.neighbors[0][nc] idx_nc = idx_nc[np.arange( 0, np.minimum(curr_k, len(idx_nc)))] idx_neighb = np.concatenate((idx_neighb, idx_nc), axis=0) else: idx_neighb = np.asarray(self.neighbors)[0][np.arange( 0, curr_k)] # Indicate participating instances in the training of the subpool included_samples[idx_neighb] = 1 curr_classes = np.unique(self.DSEL_target[idx_neighb]) # If there are +1 classes in the local region if len(curr_classes) > 1: # Obtain SGH pool subpool.fit(self.DSEL_data, self.DSEL_target, included_samples) # Adjust chosen DCS technique parameters if self.ds_tech == 'ola': ds = OLA(subpool, k=len(idx_neighb)) # change for self.k elif self.ds_tech == 'lca': ds = LCA(subpool, k=len(idx_neighb)) elif self.ds_tech == 'mcb': ds = MCB(subpool, k=len(idx_neighb)) elif self.ds_tech == 'mla': ds = MLA(subpool, k=len(idx_neighb)) elif self.ds_tech == 'a_priori': ds = APriori(subpool, k=len(idx_neighb)) elif self.ds_tech == 'a_posteriori': ds = APosteriori(subpool, k=len(idx_neighb)) # Fit ds technique ds.fit(self.DSEL_data, self.DSEL_target) neighb = np.in1d( self.neighbors, idx_neighb) # True/False vector of selected neighbors # Set distances and neighbors of the query sample (already calculated) ds.distances = np.asarray([self.distances[0][neighb] ]) # Neighborhood ds.neighbors = np.asarray([self.neighbors[0][neighb] ]) # Neighborhood ds.DFP_mask = np.ones(ds.n_classifiers) # Estimate competence comp = ds.estimate_competence(query, ds._predict_base(query)) # Select best classifier in subpool sel_c = ds.select(comp) # Add to local pool self.pool_classifiers.append(copy.deepcopy(subpool[sel_c[0]])) n += 1 # else: # # Exception: fewer than 2 classes in the neighborhood # print('OPS! Next!') # Increase neighborhood size curr_k += 2 n_err += 1 return self
def test_MLA(knn_methods): pool_classifiers, X_dsel, y_dsel, X_test, y_test = setup_classifiers() mla = MLA(pool_classifiers, knn_classifier=knn_methods) mla.fit(X_dsel, y_dsel) assert np.isclose(mla.score(X_test, y_test), 0.96808510638297873)
def test_predict_proba(): X = X_dsel_ex1 y = y_dsel_ex1 clf1 = Perceptron() clf1.fit(X, y) MLA([clf1, clf1]).fit(X, y)
def test_MLA(knn_methods): pool_classifiers, X_dsel, y_dsel, X_test, y_test = setup_classifiers() mla = MLA(pool_classifiers, knn_classifier=knn_methods) mla.fit(X_dsel, y_dsel) assert np.isclose(mla.score(X_test, y_test), 0.973404255319149)