def test_different_input_shape(create_X_y): X, y = create_X_y query = np.array([[1.0, 1.0, 2.0]]) ds_test = BaseDS() ds_test.fit(X, y) with pytest.raises(ValueError): ds_test.predict(query)
def test_predict_value(query): pool_classifiers = create_classifiers_disagree() ds = BaseDS(pool_classifiers) X = np.random.rand(10, 2) y = np.ones(10) y[:5] = 0 ds.fit(X, y) with pytest.raises(ValueError): ds.predict(query)
def test_label_encoder_only_dsel_allagree(): X_dsel_ex1 = np.array([[-1, 1], [-0.75, 0.5], [-1.5, 1.5]]) y_dsel_ex1 = np.array(['cat', 'dog', 'plane']) query = np.atleast_2d([[1, 0], [-1, -1]]) ds_test = BaseDS(create_pool_classifiers_dog(), k=2) ds_test.fit(X_dsel_ex1, y_dsel_ex1) predictions = ds_test.predict(query) assert np.array_equal(predictions, ['dog', 'dog'])
def test_label_encoder_only_dsel(): X_dsel_ex1 = np.array([[-1, 1], [-0.75, 0.5], [-1.5, 1.5]]) y_dsel_ex1 = np.array(['cat', 'dog', 'plane']) query = np.atleast_2d([[1, 0], [-1, -1]]) ds_test = BaseDS(create_pool_classifiers_dog_cat_plane(), k=2) ds_test.fit(X_dsel_ex1, y_dsel_ex1) ds_test.classify_with_ds = Mock() ds_test.classify_with_ds.return_value = [1, 0] predictions = ds_test.predict(query) assert np.array_equal(predictions, ['dog', 'cat'])
def test_IH_is_used(example_estimate_competence, create_pool_classifiers): X, y, neighbors, distances, dsel_processed, _ = example_estimate_competence expected = [0, 0, 1] query = np.ones((3, 2)) ds_test = BaseDS(create_pool_classifiers, with_IH=True, IH_rate=0.5) ds_test.fit(X, y) ds_test.DSEL_processed_ = dsel_processed ds_test._get_region_competence = MagicMock(return_value=(distances, neighbors)) predicted = ds_test.predict(query) assert np.array_equal(predicted, expected)
def test_IH_is_used(): expected = [0, 0, 1] query = np.ones((3, 2)) ds_test = BaseDS(create_pool_classifiers(), with_IH=True, IH_rate=0.5) ds_test.fit(X_dsel_ex1, y_dsel_ex1) ds_test.DSEL_processed_ = dsel_processed_ex1 ds_test.DSEL_target_ = y_dsel_ex1 ds_test.DSEL_data_ = X_dsel_ex1 ds_test._get_region_competence = MagicMock(return_value=(distances_ex1, neighbors_ex1)) predicted = ds_test.predict(query) assert np.array_equal(predicted, expected)
def test_label_encoder_base(): from sklearn.linear_model import LogisticRegression X_dsel_ex1 = np.array([[-1, 1], [-0.75, 0.5], [-1.5, 1.5]]) y_dsel_ex1 = np.array(['cat', 'dog', 'plane']) x = [[-2, -2], [2, 2]] y = ['cat', 'dog'] pool_classifiers = [LogisticRegression().fit(x, y) for _ in range(2)] query = np.atleast_2d([[1, 0], [-1, -1]]) ds_test = BaseDS(pool_classifiers, k=2) ds_test.fit(X_dsel_ex1, y_dsel_ex1) predictions = ds_test.predict(query) assert np.array_equal(predictions, np.array(['dog', 'cat']))
def test_bad_input_X(X_test, create_X_y): X_train, y_train = create_X_y ds_test = BaseDS() ds_test.fit(X_train, y_train) with pytest.raises(ValueError): ds_test.predict(X_test)
def test_not_fitted_ds(): query = np.array([[1.0, 1.0]]) ds_test = BaseDS() with pytest.raises(NotFittedError): ds_test.predict(query)
def test_bad_input_X(X): ds_test = BaseDS(create_pool_classifiers()) ds_test.fit(X_dsel_ex1, y_dsel_ex1) with pytest.raises(ValueError): ds_test.predict(X)
def test_not_fitted_ds(): query = np.array([[1.0, 1.0]]) ds_test = BaseDS(create_pool_classifiers()) with pytest.raises(NotFittedError): ds_test.predict(query)
def test_different_input_shape(): query = np.array([[1.0, 1.0, 2.0]]) ds_test = BaseDS(create_pool_classifiers()) ds_test.fit(X_dsel_ex1, y_dsel_ex1) with pytest.raises(ValueError): ds_test.predict(query)