Skip to content
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
3 changes: 2 additions & 1 deletion imblearn/over_sampling/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# License: MIT

import numbers
from collections.abc import Mapping

from ..base import BaseSampler
from ..utils._param_validation import Interval, StrOptions
Expand Down Expand Up @@ -61,7 +62,7 @@ class BaseOverSampler(BaseSampler):
"sampling_strategy": [
Interval(numbers.Real, 0, 1, closed="right"),
StrOptions({"auto", "majority", "not minority", "not majority", "all"}),
dict,
Mapping,
callable,
],
"random_state": ["random_state"],
Expand Down
22 changes: 21 additions & 1 deletion imblearn/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# Christos Aridas
# License: MIT

from collections import OrderedDict

import numpy as np
import pytest
from sklearn.base import clone
from sklearn.exceptions import ConvergenceWarning
Expand All @@ -12,7 +15,8 @@
parametrize_with_checks as parametrize_with_checks_sklearn,
)

from imblearn.under_sampling import NearMiss
from imblearn.over_sampling import RandomOverSampler
from imblearn.under_sampling import NearMiss, RandomUnderSampler
from imblearn.utils.estimator_checks import (
_set_checking_parameters,
check_param_validation,
Expand Down Expand Up @@ -73,3 +77,19 @@ def test_check_param_validation(estimator):
print(name)
_set_checking_parameters(estimator)
check_param_validation(name, estimator)


@pytest.mark.parametrize("Sampler", [RandomOverSampler, RandomUnderSampler])
def test_strategy_as_ordered_dict(Sampler):
"""Check that it is possible to pass an `OrderedDict` as strategy."""
rng = np.random.RandomState(42)
X, y = rng.randn(30, 2), np.array([0] * 10 + [1] * 20)
sampler = Sampler(random_state=42)
if isinstance(sampler, RandomOverSampler):
strategy = OrderedDict({0: 20, 1: 20})
else:
strategy = OrderedDict({0: 10, 1: 10})
sampler.set_params(sampling_strategy=strategy)
X_res, y_res = sampler.fit_resample(X, y)
assert X_res.shape[0] == sum(strategy.values())
assert y_res.shape[0] == sum(strategy.values())
3 changes: 2 additions & 1 deletion imblearn/under_sampling/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# License: MIT

import numbers
from collections.abc import Mapping

from ..base import BaseSampler
from ..utils._param_validation import Interval, StrOptions
Expand Down Expand Up @@ -61,7 +62,7 @@ class BaseUnderSampler(BaseSampler):
"sampling_strategy": [
Interval(numbers.Real, 0, 1, closed="right"),
StrOptions({"auto", "majority", "not minority", "not majority", "all"}),
dict,
Mapping,
callable,
],
}
Expand Down