def test_mcb(knn_methods): pool_classifiers, X_dsel, y_dsel, X_test, y_test = setup_classifiers() rng = np.random.RandomState(123456) mcb = MCB(pool_classifiers, random_state=rng, knn_classifier=knn_methods) mcb.fit(X_dsel, y_dsel) assert np.isclose(mcb.score(X_test, y_test), 0.9627659574468085)
def test_mcb_proba(knn_methods): pool_classifiers, X_dsel, y_dsel, X_test, y_test = setup_classifiers() rng = np.random.RandomState(123456) mcb = MCB(pool_classifiers, random_state=rng, knn_classifier=knn_methods) mcb.fit(X_dsel, y_dsel) probas = mcb.predict_proba(X_test) expected = np.load( 'deslib/tests/expected_values/mcb_proba_integration.npy') assert np.allclose(probas, expected)
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
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) metades.fit(X_dsel, y_dsel)
X_train = scaler.fit_transform(X_train) 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()
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 scores = []
data = fetch_openml(name='diabetes') X = data.data y = data.target X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=rng) # Normalizing the dataset to have 0 mean and unit variance. scaler = StandardScaler() X_train = scaler.fit_transform(X_train) X_test = scaler.transform(X_test) pool_classifiers = BaggingClassifier(Perceptron(max_iter=100), random_state=rng) pool_classifiers.fit(X_train, y_train) # Setting with_IH mcb = MCB(pool_classifiers) ola = OLA(pool_classifiers) des_p = DESP(pool_classifiers) knu = KNORAU(pool_classifiers) lca = LCA(pool_classifiers) kne = KNORAE(pool_classifiers) rank = Rank(pool_classifiers) list_ds_methods = [mcb, ola, des_p, knu, lca, kne, rank] names = ['MCB', 'OLA', 'DES-P', 'KNORA-U', 'LCA', 'KNORA-E', 'Rank'] k_value_list = range(3, 16) ############################################################################### # Plot accuracy x region of competence size. # ------------------------------------------- # We can see the this parameter can have a huge influence in the performance