def test_knop(): pool_classifiers, X_dsel, y_dsel, X_test, y_test = setup_classifiers() kne = KNOP(pool_classifiers, DFP=True) kne.fit(X_dsel, y_dsel) assert np.isclose(kne.score(X_test, y_test), 0.9030303030303031)
def test_knop(knne, expected): pool_classifiers, X_dsel, y_dsel, X_test, y_test = setup_classifiers() kne = KNOP(pool_classifiers, DFP=True, knne=knne) kne.fit(X_dsel, y_dsel) assert np.isclose(kne.score(X_test, y_test), expected)
def test_weights_zero(): knop_test = KNOP(create_pool_classifiers()) knop_test.fit(X_dsel_ex1, y_dsel_ex1) competences = np.zeros((1, 3)) result = knop_test.select(competences) assert np.all(result)
def test_select(): knop_test = KNOP(create_pool_classifiers()) competences = np.ones(3) competences[0] = 0 expected = np.atleast_2d([False, True, True]) selected = knop_test.select(competences) assert np.array_equal(expected, selected)
def test_knop_proba(): pool_classifiers, X_dsel, y_dsel, X_test, y_test = setup_classifiers() knop = KNOP(pool_classifiers) knop.fit(X_dsel, y_dsel) probas = knop.predict_proba(X_test) expected = np.load('deslib/tests/expected_values/knop_proba_integration.npy') assert np.allclose(probas, expected)
def test_not_predict_proba(): X = X_dsel_ex1 y = y_dsel_ex1 clf1 = Perceptron() clf1.fit(X, y) with pytest.raises(ValueError): knop = KNOP([clf1, clf1]) knop.fit(X, y)
def test_not_predict_proba(create_X_y): X, y = create_X_y clf1 = Perceptron() clf1.fit(X, y) with pytest.raises(ValueError): knop = KNOP([clf1, clf1]) knop.fit(X, y)
def test_fit(): knop_test = KNOP(create_pool_classifiers()) knop_test.fit(X_dsel_ex1, y_dsel_ex1) expected_scores = np.ones( (15, 6)) * np.array([0.5, 0.5, 1.0, 0.0, 0.33, 0.67]) assert np.array_equal(expected_scores, knop_test.dsel_scores) # Assert the roc_algorithm is fitted to the scores (decision space) rather than the features (feature space) assert np.array_equal(knop_test.roc_algorithm._fit_X, knop_test.dsel_scores)
def test_fit(): knop_test = KNOP(create_pool_classifiers()) knop_test.fit(X_dsel_ex1, y_dsel_ex1) expected_scores = np.array([[0.5, 0.5], [1.0, 0.0], [0.33, 0.67]]) expected_scores = np.tile(expected_scores, (15, 1, 1)) assert np.array_equal(expected_scores, knop_test.dsel_scores_) # Assert the roc_algorithm_ is fitted to the scores (decision space) rather than the features (feature space) expected_roc_data = knop_test.dsel_scores_[:, :, 0] assert np.array_equal(knop_test.op_knn_._fit_X, expected_roc_data)
def test_fit(example_estimate_competence, create_pool_classifiers): X, y = example_estimate_competence[0:2] knop_test = KNOP(create_pool_classifiers) knop_test.fit(X, y) expected_scores = np.array([[0.5, 0.5], [1.0, 0.0], [0.33, 0.67]]) expected_scores = np.tile(expected_scores, (15, 1, 1)) assert np.array_equal(expected_scores, knop_test.dsel_scores_) # Assert the roc_algorithm_ is fitted to the scores (decision space) # rather than the features (feature space) expected_roc_data = knop_test.dsel_scores_[:, :, 0] assert np.array_equal(knop_test.op_knn_._fit_X, expected_roc_data)
def test_estimate_competence(index, expected): query = np.atleast_2d([1, 1]) knop_test = KNOP(create_pool_classifiers()) knop_test.fit(X_dsel_ex1, y_dsel_ex1) knop_test.DFP_mask = np.ones(knop_test.n_classifiers) knop_test.neighbors = neighbors_ex1[index, :] knop_test.distances = distances_ex1[index, :] competences = knop_test.estimate_competence(query) assert np.isclose(competences, expected, atol=0.01).all()
def test_classify(index, expected): query = np.atleast_2d([1, 1]) knop_test = KNOP(create_pool_classifiers()) knop_test.fit(X_dsel_ex1, y_dsel_ex1) knop_test.DFP_mask = np.ones(knop_test.n_classifiers) knop_test.neighbors = neighbors_ex1[index, :] knop_test.distances = distances_ex1[index, :] prediction = knop_test.classify_instance(query) assert prediction == expected
def test_estimate_competence_batch(): query = np.ones((3, 2)) expected = np.array([[4.0, 3.0, 4.0], [5.0, 2.0, 5.0], [2.0, 5.0, 2.0]]) knop_test = KNOP(create_pool_classifiers()) knop_test.fit(X_dsel_ex1, y_dsel_ex1) knop_test.DFP_mask = np.ones(knop_test.n_classifiers_) knop_test.neighbors = neighbors_ex1 knop_test._get_similar_out_profiles = Mock(return_value=(None, neighbors_ex1)) knop_test.distances = distances_ex1 probabilities = np.zeros((3, 6)) # not used in this test competences = knop_test.estimate_competence_from_proba( query, probabilities) assert np.allclose(competences, expected, atol=0.01)
def test_classify(index, expected): query = np.atleast_2d([1, 1]) knop_test = KNOP(create_pool_classifiers()) knop_test.fit(X_dsel_ex1, y_dsel_ex1) knop_test.DFP_mask = np.ones(knop_test.n_classifiers) knop_test.neighbors = neighbors_ex1[index, :] knop_test.distances = distances_ex1[index, :] predictions = [] for clf in knop_test.pool_classifiers: predictions.append(clf.predict(query)[0]) predicted_label = knop_test.classify_instance(query, np.array(predictions)) assert predicted_label == expected
def test_estimate_competence_batch(example_estimate_competence, create_pool_classifiers): X, y, neighbors, distances, _, _ = example_estimate_competence query = np.ones((3, 2)) expected = np.array([[4.0, 3.0, 4.0], [5.0, 2.0, 5.0], [2.0, 5.0, 2.0]]) knop_test = KNOP(create_pool_classifiers) knop_test.fit(X, y) knop_test.neighbors = neighbors knop_test.distances = distances knop_test._get_similar_out_profiles = Mock(return_value=(None, neighbors)) probabilities = np.zeros((3, 6)) competences = knop_test.estimate_competence_from_proba( query, probabilities) assert np.allclose(competences, expected, atol=0.01)
def test_weights_zero(): query = np.atleast_2d([1, 1]) knop_test = KNOP(create_pool_classifiers()) knop_test.fit(X_dsel_ex1, y_dsel_ex1) knop_test.estimate_competence = MagicMock(return_value=np.zeros(3)) result = knop_test.select(query) assert np.array_equal(result, np.array([0, 1, 0]))
def test_estimate_competence(index, expected): query = np.atleast_2d([1, 1]) knop_test = KNOP(create_pool_classifiers()) knop_test.fit(X_dsel_ex1, y_dsel_ex1) knop_test.DFP_mask = np.ones(knop_test.n_classifiers_) knop_test.neighbors = neighbors_ex1[index, :] knop_test._get_similar_out_profiles = Mock( return_value=(None, np.atleast_2d(neighbors_ex1[index, :]))) knop_test.distances = distances_ex1[index, :] probabilities = [] for clf in knop_test.pool_classifiers: probabilities.append(clf.predict_proba(query)) probabilities = np.array(probabilities).transpose((1, 0, 2)) competences = knop_test.estimate_competence_from_proba( query, probabilities) assert np.allclose(competences, expected, atol=0.01)
model_perceptron = CalibratedClassifierCV(Perceptron(max_iter=100)).fit( X_train, y_train) model_linear_svm = CalibratedClassifierCV(LinearSVC()).fit( X_train, y_train) 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))
def test_check_estimator(): check_estimator(KNOP())
def test_weights_zero(): knop_test = KNOP() competences = np.zeros((1, 3)) result = knop_test.select(competences) assert np.all(result)
def test_knop(): pool_classifiers, X_dsel, y_dsel, X_test, y_test = setup_classifiers() knop = KNOP(pool_classifiers) knop.fit(X_dsel, y_dsel) assert np.isclose(knop.score(X_test, y_test), 0.8106060606060606)
def test_knop(knn_methods): pool_classifiers, X_dsel, y_dsel, X_test, y_test = setup_classifiers() knop = KNOP(pool_classifiers, knn_classifier=knn_methods) knop.fit(X_dsel, y_dsel) assert np.isclose(knop.score(X_test, y_test), 0.97340425531914898)