def faiss_KNORAE_knn(XTrain, YTrain, k, XTest, YTest): start = time.clock() knorae_sk = KNORAE(k=k, knn_classifier='faiss') knorae_sk.fit(XTrain, YTrain) score = knorae_sk.score(XTest, YTest) print("faiss_knn_knorae run_time: {}".format(time.clock() - start)) print("faiss_knn_knorae score: {}".format(score))
def run_knorae(pool_classifiers, X_DSEL, y_DSEL, X_test, y_test, knn_type): knorae = KNORAE(pool_classifiers=pool_classifiers, knn_classifier=knn_type) knorae.fit(X_DSEL, y_DSEL) start = time.clock() score = knorae.score(X_test, y_test) end = time.clock() - start return score, end
def test_kne(): pool_classifiers, X_dsel, y_dsel, X_test, y_test = setup_classifiers() kne = KNORAE(pool_classifiers, DFP=True) kne.fit(X_dsel, y_dsel) assert np.isclose(kne.score(X_test, y_test), 0.9)
def test_kne(knn_methods): pool_classifiers, X_dsel, y_dsel, X_test, y_test = setup_classifiers() kne = KNORAE(pool_classifiers, knn_classifier=knn_methods) kne.fit(X_dsel, y_dsel) assert np.isclose(kne.score(X_test, y_test), 0.973404255319148)
# can estimate probabilities pool_classifiers = RandomForestClassifier(n_estimators=10, max_depth=5) pool_classifiers.fit(X_train, y_train) # Initialize a DS technique. Here we specify the size of the region of competence (5 neighbors) knorau = KNORAU(pool_classifiers) kne = KNORAE(pool_classifiers, k=5) desp = DESP(pool_classifiers, k=5) ola = OLA(pool_classifiers, k=5) mcb = MCB(pool_classifiers, k=5) meta = METADES(pool_classifiers, k=5) # Fit the DS techniques knorau.fit(X_dsel, y_dsel) kne.fit(X_dsel, y_dsel) desp.fit(X_dsel, y_dsel) meta.fit(X_dsel, y_dsel) ola.fit(X_dsel, y_dsel) mcb.fit(X_dsel, y_dsel) # Calculate classification accuracy of each technique print('Classification accuracy RF: ', RF.score(X_test, y_test)) print('Evaluating DS techniques:') print('Classification accuracy KNORAU: ', knorau.score(X_test, y_test)) print('Classification accuracy KNORA-Eliminate: ', kne.score(X_test, y_test)) print('Classification accuracy DESP: ', desp.score(X_test, y_test)) print('Classification accuracy OLA: ', ola.score(X_test, y_test)) print('Classification accuracy MCB: ', mcb.score(X_test, y_test)) print('Classification accuracy META-DES: ', meta.score(X_test, y_test))
'Stacked LR', 'Stacked Decision Tree' ] classifiers = [svm, mlp, forest, boosting, stacked_lr, stacked_dt] for clf, ax, title in zip(classifiers, sub.flatten(), titles): plot_classifier_decision(ax, clf, X_test, mode='filled', alpha=0.4) plot_dataset(X_test, y_test, ax=ax) ax.set_xlim(np.min(X[:, 0]), np.max(X[:, 0])) ax.set_ylim(np.min(X[:, 1]), np.max(X[:, 1])) ax.set_title(title, fontsize=15) plt.show() plt.tight_layout() ############################################################################### # Evaluation on the test set # -------------------------- # # Finally, let's evaluate the baselines and the Dynamic Selection methods on # the test set: print('KNORAE score = {}'.format(knora_e.score(X_test, y_test))) print('DESP score = {}'.format(desp.score(X_test, y_test))) print('OLA score = {}'.format(ola.score(X_test, y_test))) print('Rank score = {}'.format(rank.score(X_test, y_test))) print('SVM score = {}'.format(svm.score(X_test, y_test))) print('MLP score = {}'.format(mlp.score(X_test, y_test))) print('RF score = {}'.format(forest.score(X_test, y_test))) print('Boosting score = {}'.format(boosting.score(X_test, y_test))) print('Stacking LR score = {}'.format(stacked_lr.score(X_test, y_test))) print('Staking Decision Tree = {}'.format(stacked_dt.score(X_test, y_test)))
knorau.fit(X_dsel, y_dsel) kne.fit(X_dsel, y_dsel) desp.fit(X_dsel, y_dsel) meta.fit(X_dsel, y_dsel) ola.fit(X_dsel, y_dsel) mcb.fit(X_dsel, y_dsel) ############################################################################### # Plotting the results # ----------------------- # Let's now evaluate the methods on the test set. rf_score = RF.score(X_test, y_test) stacked_score = stacked.score(X_test, y_test) knorau_score = knorau.score(X_test, y_test) kne_score = kne.score(X_test, y_test) desp_score = desp.score(X_test, y_test) ola_score = ola.score(X_test, y_test) mcb_score = mcb.score(X_test, y_test) meta_score = meta.score(X_test, y_test) print('Classification accuracy RF: ', rf_score) print('Classification accuracy Stacked: ', stacked_score) print('Evaluating DS techniques:') print('Classification accuracy KNORA-U: ', knorau_score) print('Classification accuracy KNORA-E: ', kne_score) print('Classification accuracy DESP: ', desp_score) print('Classification accuracy OLA: ', ola_score) print('Classification accuracy MCB: ', mcb_score) print('Classification accuracy META-DES: ', meta_score) cmap = get_cmap('Dark2')
apriori = APriori(calibrated_pool, random_state=rng) meta = METADES(calibrated_pool) knorau.fit(X_dsel, y_dsel) kne.fit(X_dsel, y_dsel) desp.fit(X_dsel, y_dsel) ola.fit(X_dsel, y_dsel) mcb.fit(X_dsel, y_dsel) apriori.fit(X_dsel, y_dsel) meta.fit(X_dsel, y_dsel) ############################################################################### # Evaluating the methods # ----------------------- # Let's now evaluate the methods on the test set. We also use the performance # of Bagging (pool of classifiers without any selection) as a baseline # comparison. We can see that the majority of DS methods achieve higher # classification accuracy. print('Evaluating DS techniques:') print('Classification accuracy KNORA-Union: ', knorau.score(X_test, y_test)) print('Classification accuracy KNORA-Eliminate: ', kne.score(X_test, y_test)) print('Classification accuracy DESP: ', desp.score(X_test, y_test)) print('Classification accuracy OLA: ', ola.score(X_test, y_test)) print('Classification accuracy A priori: ', apriori.score(X_test, y_test)) print('Classification accuracy MCB: ', mcb.score(X_test, y_test)) print('Classification accuracy META-DES: ', meta.score(X_test, y_test)) print('Classification accuracy Bagging: ', pool_classifiers.score(X_test, y_test))
def main(): ############################################################################### # Preparing the dataset # --------------------- # In this part we load the breast cancer dataset from scikit-learn and # preprocess it in order to pass to the DS models. An important point here is # to normalize the data so that it has zero mean and unit variance, which is # a common requirement for many machine learning algorithms. # This step can be easily done using the StandardScaler class. rng = np.random.RandomState(123) data = load_breast_cancer() X = data.data y = data.target # split the data into training and test data X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=rng) # Scale the variables to have 0 mean and unit variance scaler = StandardScaler() X_train = scaler.fit_transform(X_train) X_test = scaler.transform(X_test) # Split the data into training and DSEL for DS techniques X_train, X_dsel, y_train, y_dsel = train_test_split(X_train, y_train, test_size=0.5, random_state=rng) # Train a pool of 100 base classifiers pool_classifiers = BaggingClassifier(Perceptron(max_iter=10), n_estimators=100, random_state=rng) pool_classifiers.fit(X_train, y_train) # Initialize the DS techniques knorau = KNORAU(pool_classifiers) kne = KNORAE(pool_classifiers) desp = DESP(pool_classifiers) ola = OLA(pool_classifiers) mcb = MCB(pool_classifiers) ############################################################################### # Calibrating base classifiers # ----------------------------- # Some dynamic selection techniques requires that the base classifiers estimate # probabilities in order to estimate its competence level. Since the Perceptron # model is not a probabilistic classifier (does not implements the # predict_proba method, it needs to be calibrated for # probability estimation before being used by such DS techniques. This step can # be conducted using the CalibrateClassifierCV class from scikit-learn. Note # that in this example we pass a prefited pool of classifiers to the # calibration method in order to use exactly the same pool used in the other # DS methods. calibrated_pool = [] for clf in pool_classifiers: calibrated = CalibratedClassifierCV(base_estimator=clf, cv='prefit') calibrated.fit(X_dsel, y_dsel) calibrated_pool.append(calibrated) apriori = APriori(calibrated_pool) meta = METADES(calibrated_pool) knorau.fit(X_dsel, y_dsel) kne.fit(X_dsel, y_dsel) desp.fit(X_dsel, y_dsel) ola.fit(X_dsel, y_dsel) mcb.fit(X_dsel, y_dsel) apriori.fit(X_dsel, y_dsel) meta.fit(X_dsel, y_dsel) ############################################################################### # Evaluating the methods # ----------------------- # Let's now evaluate the methods on the test set. We also use the performance # of Bagging (pool of classifiers without any selection) as a baseline # comparison. We can see that the majority of DS methods achieve higher # classification accuracy. print('Evaluating DS techniques:') print('Classification accuracy KNORA-Union: ', knorau.score(X_test, y_test)) print('Classification accuracy KNORA-Eliminate: ', kne.score(X_test, y_test)) print('Classification accuracy DESP: ', desp.score(X_test, y_test)) print('Classification accuracy OLA: ', ola.score(X_test, y_test)) print('Classification accuracy A priori: ', apriori.score(X_test, y_test)) print('Classification accuracy MCB: ', mcb.score(X_test, y_test)) print('Classification accuracy META-DES: ', meta.score(X_test, y_test)) print('Classification accuracy Bagging: ', pool_classifiers.score(X_test, y_test))