def test_label_encoder_integration_sklearn_ensembles(): pool_classifiers, X_dsel, y_dsel, X_test, y_test = setup_classifiers( encode_labels=['no', 'yes']) knorau = KNORAU(pool_classifiers) knorau.fit(X_dsel, y_dsel) assert np.isclose(knorau.score(X_test, y_test), 0.9787234042553191)
def test_label_encoder_integration_sklearn_ensembles_not_encoding(): rng = np.random.RandomState(123456) X_dsel, X_test, X_train, y_dsel, y_test, y_train = load_dataset( ['yes', 'no'], rng) # Train a pool of using adaboost which has label encoding problems. pool_classifiers = AdaBoostClassifier(n_estimators=10, random_state=rng) pool_classifiers.fit(X_train, y_train) knorau = KNORAU(pool_classifiers) knorau.fit(X_dsel, y_dsel) assert np.isclose(knorau.score(X_test, y_test), 0.9627659574468085)
def test_label_encoder_integration_list_classifiers(): rng = np.random.RandomState(123456) X_dsel, X_test, X_train, y_dsel, y_test, y_train = load_dataset( encode_labels=['no', 'yes'], rng=rng) pool_classifiers = [LogisticRegression(), SVC(probability=True)] [clf.fit(X_train, y_train) for clf in pool_classifiers] knorau = KNORAU(pool_classifiers) knorau.fit(X_dsel, y_dsel) this_score = knorau.score(X_test, y_test) assert np.isclose(this_score, 0.9787234042553191)
def predict(self, X): """Hard decision.""" # Input validation X = check_array(X) if X.shape[1] != self.X_.shape[1]: raise ValueError("number of features does not match") if self.des == "KNORAU": des = KNORAU(pool_classifiers=self.estimators_, random_state=42) des.fit(self.X_, self.y_) prediction = des.predict(X) elif self.des == "DESIRE": des = DESIRE(ensemble=self.estimators_, random_state=42, mode="whole", w=self.w) des.fit(self.X_, self.y_) prediction = des.predict(X) elif self.des == "DESIREC": des = DESIRE(ensemble=self.estimators_, random_state=42, mode="correct", w=self.w) des.fit(self.X_, self.y_) prediction = des.predict(X) elif self.des == "DESIREW": des = DESIRE(ensemble=self.estimators_, random_state=42, mode="wrong", w=self.w) des.fit(self.X_, self.y_) prediction = des.predict(X) else: esm = self.ensemble_support_matrix(X) average_support = np.mean(esm, axis=0) prediction = np.argmax(average_support, axis=1) return prediction
def test_knorau(knn_methods): pool_classifiers, X_dsel, y_dsel, X_test, y_test = setup_classifiers() knorau = KNORAU(pool_classifiers, knn_classifier=knn_methods) knorau.fit(X_dsel, y_dsel) assert np.isclose(knorau.score(X_test, y_test), 0.9787234042553191)
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) ola.fit(X_dsel, y_dsel) mcb.fit(X_dsel, y_dsel) # Fitting the tacking models stacked_lr.fit(X_dsel, y_dsel) stacked_dt.fit(X_dsel, y_dsel) # Calculate classification accuracy of each technique print('Evaluating DS techniques:') print('Classification accuracy of Majority voting the pool: ', model_voting.score(X_test, y_test)) print('Classification accuracy of KNORA-U: ', knorau.score(X_test, y_test))