def test_ola_proba(knn_methods): pool_classifiers, X_dsel, y_dsel, X_test, y_test = setup_classifiers() ola = OLA(pool_classifiers, knn_classifier=knn_methods) ola.fit(X_dsel, y_dsel) probas = ola.predict_proba(X_test) expected = np.load( 'deslib/tests/expected_values/ola_proba_integration.npy') assert np.allclose(probas, expected)
def test_ola_subspaces(): rng = np.random.RandomState(123456) X_dsel, X_test, X_train, y_dsel, y_test, y_train = load_dataset(None, rng) pool = BaggingClassifier(LogisticRegression(), bootstrap_features=True, max_features=0.5, random_state=rng).fit(X_train, y_train) ola = OLA(pool) ola.fit(X_dsel, y_dsel) assert np.isclose(ola.score(X_test, y_test), 0.9680851063829787)
def _create_hard_estimator(self, k): clf = None if self.selection_method == 'ola': clf = OLA(self.pool_classifiers, DFP=self.dfp, k=k) elif self.selection_method == 'lca': clf = LCA(self.pool_classifiers, DFP=self.dfp, k=k) else: raise ValueError("The chosen selection method is not available") return clf
def initialize_ds(pool_classifiers, X, y, k=5): knorau = KNORAU(pool_classifiers, k=k) kne = KNORAE(pool_classifiers, k=k) desknn = DESKNN(pool_classifiers, k=k) ola = OLA(pool_classifiers, k=k) lca = LCA(pool_classifiers, k=k) mla = MLA(pool_classifiers, k=k) mcb = MCB(pool_classifiers, k=k) rank = Rank(pool_classifiers, k=k) knop = KNOP(pool_classifiers, k=k) meta = METADES(pool_classifiers, k=k) list_ds = [knorau, kne, ola, lca, mla, desknn, mcb, rank, knop, meta] names = [ 'KNORA-U', 'KNORA-E', 'OLA', 'LCA', 'MLA', 'DESKNN', 'MCB', 'RANK', 'KNOP', 'META-DES' ] # fit the ds techniques for ds in list_ds: ds.fit(X, y) return list_ds, names
def test_ola(knn_methods): pool_classifiers, X_dsel, y_dsel, X_test, y_test = setup_classifiers() ola = OLA(pool_classifiers, knn_classifier=knn_methods) ola.fit(X_dsel, y_dsel) assert np.isclose(ola.score(X_test, y_test), 0.9787234042553191)
] voting_classifiers = [("perceptron", model_perceptron), ("svc", model_svc), ("bayes", model_bayes), ("tree", model_tree), ("knn", model_knn)] model_voting = VotingClassifier(estimators=voting_classifiers).fit( X_train, y_train) # Initializing the techniques knorau = KNORAU(pool_classifiers) kne = KNORAE(pool_classifiers) desp = DESP(pool_classifiers) metades = METADES(pool_classifiers, mode='hybrid') # DCS techniques ola = OLA(pool_classifiers) mcb = MCB(pool_classifiers) ############################################################################## # Adding stacked classifier as baseline comparison. Stacked classifier can # be found in the static module. In this experiment we consider two types # of stacking: one using logistic regression as meta-classifier # (default configuration) and the other using a Decision Tree. stacked_lr = StackedClassifier(pool_classifiers, random_state=rng) stacked_dt = StackedClassifier(pool_classifiers, random_state=rng, meta_classifier=DecisionTreeClassifier()) # Fitting the DS techniques knorau.fit(X_dsel, y_dsel) kne.fit(X_dsel, y_dsel) desp.fit(X_dsel, y_dsel)
X_test = scaler.transform(X_test) # Training a pool of classifiers using the bagging technique. pool_classifiers = BaggingClassifier(DecisionTreeClassifier(random_state=rng), random_state=rng) pool_classifiers.fit(X_train, y_train) ############################################################################### # Setting DS method to use the switch mechanism # ---------------------------------------------- # In order to activate the functionality to switch between DS and KNN according # to the instance hardness level we need to set the DS techniques to use this # information. This is done by setting the hyperparameter `with_IH` to True. # In this example we consider four different values for te threshold mcb = MCB(pool_classifiers, with_IH=True, random_state=rng) ola = OLA(pool_classifiers, with_IH=True, random_state=rng) rank = Rank(pool_classifiers, with_IH=True, random_state=rng) des_p = DESP(pool_classifiers, with_IH=True, random_state=rng) kne = KNORAE(pool_classifiers, with_IH=True, random_state=rng) knu = KNORAU(pool_classifiers, with_IH=True, random_state=rng) list_ih_values = [0.0, 1. / 7., 2. / 7., 3. / 7.] list_ds_methods = [ method.fit(X_train, y_train) for method in [mcb, ola, rank, des_p, kne, knu] ] names = ['MCB', 'OLA', 'Mod. Rank', 'DES-P', 'KNORA-E', 'KNORA-U'] # Plot accuracy x IH fig, ax = plt.subplots() for ds_method, name in zip(list_ds_methods, names):
pool_classifiers = BaggingClassifier(base_estimator=DecisionTreeClassifier(), n_estimators=100, random_state=rng) pool_classifiers.fit(X_train, y_train) # Setting up static methods. stacked = StackedClassifier(pool_classifiers) static_selection = StaticSelection(pool_classifiers) single_best = SingleBest(pool_classifiers) # Initialize a DS technique. Here we specify the size of # the region of competence (5 neighbors) knorau = KNORAU(pool_classifiers, random_state=rng) kne = KNORAE(pool_classifiers, random_state=rng) desp = DESP(pool_classifiers, random_state=rng) ola = OLA(pool_classifiers, random_state=rng) mcb = MCB(pool_classifiers, random_state=rng) knop = KNOP(pool_classifiers, random_state=rng) meta = METADES(pool_classifiers, random_state=rng) names = [ 'Single Best', 'Static Selection', 'Stacked', 'KNORA-U', 'KNORA-E', 'DES-P', 'OLA', 'MCB', 'KNOP', 'META-DES' ] methods = [ single_best, static_selection, stacked, knorau, kne, desp, ola, mcb, knop, meta ] # Fit the DS techniques
X_train, X_dsel, y_train, y_dsel = train_test_split(X_train, y_train, test_size=0.5, random_state=rng) # Considering a pool composed of 10 base classifiers pool_classifiers = RandomForestClassifier(n_estimators=10, random_state=rng, max_depth=10) pool_classifiers.fit(X_train, y_train) ds_names = ['A Priori', 'A Posteriori', 'OLA', 'LCA', 'DES-P', 'META-DES'] # DS techniques without DFP apriori = APriori(pool_classifiers, random_state=rng) aposteriori = APosteriori(pool_classifiers, random_state=rng) ola = OLA(pool_classifiers) lca = LCA(pool_classifiers) desp = DESP(pool_classifiers) meta = METADES(pool_classifiers) # FIRE-DS techniques (with DFP) fire_apriori = APriori(pool_classifiers, DFP=True, random_state=rng) fire_aposteriori = APosteriori(pool_classifiers, DFP=True, random_state=rng) fire_ola = OLA(pool_classifiers, DFP=True) fire_lca = LCA(pool_classifiers, DFP=True) fire_desp = DESP(pool_classifiers, DFP=True) fire_meta = METADES(pool_classifiers, DFP=True) list_ds = [apriori, aposteriori, ola, lca, desp, meta] list_fire_ds = [ fire_apriori, fire_aposteriori, fire_ola, fire_lca, fire_desp, fire_meta