def test_estimate_competence(index, expected): ola_test = OLA(create_pool_classifiers()) ola_test.processed_dsel = dsel_processed_ex1 ola_test.neighbors = neighbors_ex1[index, :] ola_test.distances = distances_ex1[index, :] ola_test.DFP_mask = [1, 1, 1] query = np.array([1, 1]) competences = ola_test.estimate_competence(query.reshape(1, -1)) assert np.isclose(competences, expected).all()
def test_estimate_competence_batch(example_estimate_competence): _, _, neighbors, distances, dsel_processed, _ = example_estimate_competence expected = np.array([[0.57142857, 0.71428571, 0.71428571], [0.71428571, 0.85714286, 0.71428571], [0.57142857, 0.71428571, 0.57142857]]) ola_test = OLA() ola_test.DSEL_processed_ = dsel_processed ola_test.DFP_mask = np.ones((3, 3)) competences = ola_test.estimate_competence(neighbors, distances=distances) assert np.allclose(competences, expected)
def test_estimate_competence_batch(): expected = np.array([[0.57142857, 0.71428571, 0.71428571], [0.71428571, 0.85714286, 0.71428571], [0.57142857, 0.71428571, 0.57142857]]) ola_test = OLA(create_pool_classifiers()) ola_test.DSEL_processed_ = dsel_processed_ex1 neighbors = neighbors_ex1 distances = distances_ex1 ola_test.DFP_mask = np.ones((3, 3)) query = np.array([[1, 1], [1, 1], [1, 1]]) competences = ola_test.estimate_competence(query, neighbors, distances=distances) assert np.allclose(competences, expected)
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