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_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_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_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)