def test_aposteriori(): pool_classifiers, X_dsel, y_dsel, X_test, y_test = setup_classifiers() rng = np.random.RandomState(123456) a_posteriori = APosteriori(pool_classifiers, rng=rng, DFP=True) a_posteriori.fit(X_dsel, y_dsel) assert np.isclose(a_posteriori.score(X_test, y_test), 0.90000000000000002)
def test_fit(create_X_y, create_pool_classifiers): X, y = create_X_y a_posteriori_test = APosteriori(create_pool_classifiers) a_posteriori_test.fit(X, y) expected = np.array([[0.5, 0.5], [1.0, 0.0], [0.33, 0.67]]) expected = np.tile(expected, (15, 1, 1)) assert np.array_equal(a_posteriori_test.dsel_scores_, expected)
def test_estimate_competence_kuncheva_ex_batch(): # considering a batch composed of 10 samples query = np.ones((10, 2)) a_posteriori_test = APosteriori([create_base_classifier(return_value=1)], k=k_ex_kuncheva) a_posteriori_test.fit(dsel_processed_kuncheva, y_dsel_ex_kuncheva_dependent) a_posteriori_test.DSEL_processed_ = dsel_processed_kuncheva a_posteriori_test.dsel_scores_ = dsel_scores_ex_kuncheva a_posteriori_test.n_classes_ = n_classes_ex_kuncheva # repeating the same matrix in a new axis to simulate a batch input. neighbors = np.tile(neighbors_ex_kuncheva, (10, 1)) distances = np.tile(distances_ex_kuncheva, (10, 1)) predictions = [] for clf in a_posteriori_test.pool_classifiers: predictions.append(clf.predict(query)[0]) competences = a_posteriori_test.estimate_competence(query, neighbors, distances, predictions=np.array( predictions)) assert np.allclose(competences, 0.95, atol=0.01)
def test_aposteriori(knn_methods): pool_classifiers, X_dsel, y_dsel, X_test, y_test = setup_classifiers() rng = np.random.RandomState(123456) a_posteriori = APosteriori(pool_classifiers, random_state=rng, knn_classifier=knn_methods) a_posteriori.fit(X_dsel, y_dsel) assert np.isclose(a_posteriori.score(X_test, y_test), 0.96276595744680848)
def test_aposteriori(): pool_classifiers, X_dsel, y_dsel, X_test, y_test = setup_classifiers() rng = np.random.RandomState(123456) a_posteriori = APosteriori(pool_classifiers, random_state=rng, DFP=True, with_IH=True, IH_rate=0.1) a_posteriori.fit(X_dsel, y_dsel) assert np.isclose(a_posteriori.score(X_test, y_test), 0.8303030303030303)
def test_estimate_competence_kuncheva_ex_batch(example_kuncheva): # considering a batch composed of 10 samples query = np.ones((10, 2)) a_posteriori_test = APosteriori(k=example_kuncheva['k']) a_posteriori_test.fit(example_kuncheva['dsel_processed'], example_kuncheva['y_dependent']) a_posteriori_test.DSEL_processed_ = example_kuncheva['dsel_processed'] a_posteriori_test.dsel_scores_ = example_kuncheva['dsel_scores'] a_posteriori_test.n_classes_ = example_kuncheva['n_classes'] # repeating the same matrix in a new axis to simulate a batch input. neighbors = np.tile(example_kuncheva['neighbors'], (10, 1)) distances = np.tile(example_kuncheva['distances'], (10, 1)) predictions = np.ones((1, 10)) competences = a_posteriori_test.estimate_competence( query, neighbors, distances, predictions=np.array(predictions)) assert np.allclose(competences, 0.95, atol=0.01)
def test_estimate_competence_all_ones(index): query = np.atleast_2d([1, 1]) a_posteriori_test = APosteriori(create_pool_classifiers()) a_posteriori_test.fit(X_dsel_ex1, y_dsel_ex1) a_posteriori_test.DSEL_processed_ = dsel_processed_ex1 a_posteriori_test.dsel_scores_ = dsel_scores_all_ones neighbors = neighbors_ex1[index, :].reshape(1, -1) distances = distances_all_ones[index, :].reshape(1, -1) expected = [1.0, 1.0, 1.0] predictions = [] for clf in a_posteriori_test.pool_classifiers: predictions.append(clf.predict(query)[0]) competences = a_posteriori_test.estimate_competence( query, neighbors, distances, predictions=np.array(predictions)) assert np.isclose(competences, expected).all()
def test_fit(): a_posteriori_test = APosteriori(create_pool_classifiers()) a_posteriori_test.fit(X_dsel_ex1, y_dsel_ex1) assert np.isclose(a_posteriori_test.dsel_scores, [0.5, 0.5, 1.0, 0.0, 0.33, 0.67]).all()
def test_fit(): a_posteriori_test = APosteriori(create_pool_classifiers()) a_posteriori_test.fit(X_dsel_ex1, y_dsel_ex1) expected = np.array([[0.5, 0.5], [1.0, 0.0], [0.33, 0.67]]) expected = np.tile(expected, (15, 1, 1)) assert np.array_equal(a_posteriori_test.dsel_scores, expected)
model_svc = SVC(probability=True).fit(X_train, y_train) model_bayes = GaussianNB().fit(X_train, y_train) model_tree = DecisionTreeClassifier().fit(X_train, y_train) model_knn = KNeighborsClassifier(n_neighbors=5).fit(X_train, y_train) pool_classifiers = [ model_perceptron, model_linear_svm, model_svc, model_bayes, model_tree, model_knn ] # Initializing the DS techniques knop = KNOP(pool_classifiers) rrc = RRC(pool_classifiers) lca = LCA(pool_classifiers) mcb = MCB(pool_classifiers) aposteriori = APosteriori(pool_classifiers) # Fitting the techniques knop.fit(X_dsel, y_dsel) rrc.fit(X_dsel, y_dsel) lca.fit(X_dsel, y_dsel) mcb.fit(X_dsel, y_dsel) aposteriori.fit(X_dsel, y_dsel) # Calculate classification accuracy of each technique print('Evaluating DS techniques:') print('Classification accuracy KNOP: ', knop.score(X_test, y_test)) print('Classification accuracy RRC: ', rrc.score(X_test, y_test)) print('Classification accuracy LCA: ', lca.score(X_test, y_test)) print('Classification accuracy A posteriori: ', aposteriori.score(X_test, y_test))
# 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 techniques without DFP apriori = APriori(pool_classifiers) aposteriori = APosteriori(pool_classifiers) ola = OLA(pool_classifiers) lca = LCA(pool_classifiers) desp = DESP(pool_classifiers) meta = METADES(pool_classifiers) apriori.fit(X_dsel, y_dsel) aposteriori.fit(X_dsel, y_dsel) ola.fit(X_dsel, y_dsel) lca.fit(X_dsel, y_dsel) desp.fit(X_dsel, y_dsel) meta.fit(X_dsel, y_dsel) print('Evaluating DS techniques:') print('Classification accuracy of OLA: ', ola.score(X_test, y_test)) print('Classification accuracy of LCA: ', lca.score(X_test, y_test)) print('Classification accuracy of A priori: ', apriori.score(X_test, y_test)) print('Classification accuracy of A posteriori: ', aposteriori.score(X_test, y_test)) print('Classification accuracy of DES-P: ', desp.score(X_test, y_test)) print('Classification accuracy of META-DES: ', meta.score(X_test, y_test)) # Testing fire: