Skip to content

FIX make sure to accept "minority" as a valid strategy in over-samplers #964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions doc/whats_new/v0.10.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
.. _changes_0_10:

Version 0.10.1
==============

**December 28, 2022**

Changelog
---------

Bug fixes
.........

- Fix a regression in over-sampler where the string `minority` was rejected as
an unvalid sampling strategy.
:pr:`964` by :user:`Prakhyath Bhandary <Prakhyath07>`.

Version 0.10.0
==============

Expand Down
2 changes: 1 addition & 1 deletion imblearn/over_sampling/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class BaseOverSampler(BaseSampler):
_parameter_constraints: dict = {
"sampling_strategy": [
Interval(numbers.Real, 0, 1, closed="right"),
StrOptions({"auto", "majority", "not minority", "not majority", "all"}),
StrOptions({"auto", "minority", "not minority", "not majority", "all"}),
Mapping,
callable,
],
Expand Down
18 changes: 18 additions & 0 deletions imblearn/over_sampling/tests/test_random_over_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import numpy as np
import pytest
from sklearn.datasets import make_classification
from sklearn.utils._testing import (
_convert_container,
assert_allclose,
Expand Down Expand Up @@ -255,3 +256,20 @@ def test_random_over_sampler_shrinkage_error(data, shrinkage, err_msg):
ros = RandomOverSampler(shrinkage=shrinkage)
with pytest.raises(ValueError, match=err_msg):
ros.fit_resample(X, y)


@pytest.mark.parametrize(
"sampling_strategy", ["auto", "minority", "not minority", "not majority", "all"]
)
def test_random_over_sampler_strings(sampling_strategy):
"""Check that we support all supposed strings as `sampling_strategy` in
a sampler inheriting from `BaseOverSampler`."""

X, y = make_classification(
n_samples=100,
n_clusters_per_class=1,
n_classes=3,
weights=[0.1, 0.3, 0.6],
random_state=0,
)
RandomOverSampler(sampling_strategy=sampling_strategy).fit_resample(X, y)
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import numpy as np
import pytest
from sklearn.datasets import make_classification
from sklearn.utils._testing import assert_array_equal

from imblearn.under_sampling import RandomUnderSampler
Expand Down Expand Up @@ -130,3 +131,20 @@ def test_random_under_sampling_nan_inf():
assert y_res.shape == (6,)
assert X_res.shape == (6, 2)
assert np.any(~np.isfinite(X_res))


@pytest.mark.parametrize(
"sampling_strategy", ["auto", "majority", "not minority", "not majority", "all"]
)
def test_random_under_sampler_strings(sampling_strategy):
"""Check that we support all supposed strings as `sampling_strategy` in
a sampler inheriting from `BaseUnderSampler`."""

X, y = make_classification(
n_samples=100,
n_clusters_per_class=1,
n_classes=3,
weights=[0.1, 0.3, 0.6],
random_state=0,
)
RandomUnderSampler(sampling_strategy=sampling_strategy).fit_resample(X, y)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# License: MIT

import numpy as np
import pytest
from sklearn.datasets import make_classification
from sklearn.utils._testing import assert_array_equal

from imblearn.under_sampling import TomekLinks
Expand Down Expand Up @@ -68,3 +70,20 @@ def test_tl_fit_resample():
y_gt = np.array([1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0])
assert_array_equal(X_resampled, X_gt)
assert_array_equal(y_resampled, y_gt)


@pytest.mark.parametrize(
"sampling_strategy", ["auto", "majority", "not minority", "not majority", "all"]
)
def test_tomek_links_strings(sampling_strategy):
"""Check that we support all supposed strings as `sampling_strategy` in
a sampler inheriting from `BaseCleaningSampler`."""

X, y = make_classification(
n_samples=100,
n_clusters_per_class=1,
n_classes=3,
weights=[0.1, 0.3, 0.6],
random_state=0,
)
TomekLinks(sampling_strategy=sampling_strategy).fit_resample(X, y)