Skip to content

ENH accept non finite values in random samplers #643

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
Nov 16, 2019
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
5 changes: 5 additions & 0 deletions doc/whats_new/v0.6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ Enhancement
to check or not the input ``X`` and ``y``.
:pr:`637` by :user:`Guillaume Lemaitre <glemaitre>`.

- :class:`imblearn.under_sampling.RandomUnderSampler`,
:class:`imblearn.over_sampling.RandomOverSampler` can resample when non
finite values are present in ``X``.
:pr:`643` by `Guillaume Lemaitre <glemaitre>`.

Deprecation
...........

Expand Down
9 changes: 7 additions & 2 deletions imblearn/over_sampling/_random_over_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ def _check_X_y(X, y):
y, binarize_y = check_target_type(y, indicate_one_vs_all=True)
if not hasattr(X, "loc"):
# Do not convert dataframe
X = check_array(X, accept_sparse=["csr", "csc"], dtype=None)
X = check_array(X, accept_sparse=["csr", "csc"], dtype=None,
force_all_finite=False)
y = check_array(
y, accept_sparse=["csr", "csc"], dtype=None, ensure_2d=False
)
Expand Down Expand Up @@ -108,4 +109,8 @@ def _fit_resample(self, X, y):
)

def _more_tags(self):
return {"X_types": ["2darray", "string"], "sample_indices": True}
return {
"X_types": ["2darray", "string"],
"sample_indices": True,
"allow_nan": True,
}
20 changes: 20 additions & 0 deletions imblearn/over_sampling/tests/test_random_over_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,23 @@ def test_random_over_sampling_heterogeneous_data():
assert y_res.shape[0] == 4
assert X_res.dtype == object
assert X_res[-1, 0] in X_hetero[:, 0]


def test_random_over_sampling_nan_inf():
# check that we can oversample even with missing or infinite data
# regression tests for #605
rng = np.random.RandomState(42)
n_not_finite = X.shape[0] // 3
row_indices = rng.choice(np.arange(X.shape[0]), size=n_not_finite)
col_indices = rng.randint(0, X.shape[1], size=n_not_finite)
not_finite_values = rng.choice([np.nan, np.inf], size=n_not_finite)

X_ = X.copy()
X_[row_indices, col_indices] = not_finite_values

ros = RandomOverSampler(random_state=0)
X_res, y_res = ros.fit_resample(X_, Y)

assert y_res.shape == (14,)
assert X_res.shape == (14, 2)
assert np.any(~np.isfinite(X_res))
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ def _check_X_y(X, y):
y, binarize_y = check_target_type(y, indicate_one_vs_all=True)
if not hasattr(X, "loc"):
# Do not convert dataframe
X = check_array(X, accept_sparse=["csr", "csc"], dtype=None)
X = check_array(X, accept_sparse=["csr", "csc"], dtype=None,
force_all_finite=False)
y = check_array(
y, accept_sparse=["csr", "csc"], dtype=None, ensure_2d=False
)
Expand Down Expand Up @@ -121,4 +122,8 @@ def _fit_resample(self, X, y):
return _safe_indexing(X, idx_under), _safe_indexing(y, idx_under)

def _more_tags(self):
return {"X_types": ["2darray", "string"], "sample_indices": True}
return {
"X_types": ["2darray", "string"],
"sample_indices": True,
"allow_nan": True,
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,23 @@ def test_random_under_sampling_heterogeneous_data():
assert X_res.shape[0] == 2
assert y_res.shape[0] == 2
assert X_res.dtype == object


def test_random_under_sampling_nan_inf():
# check that we can undersample even with missing or infinite data
# regression tests for #605
rng = np.random.RandomState(42)
n_not_finite = X.shape[0] // 3
row_indices = rng.choice(np.arange(X.shape[0]), size=n_not_finite)
col_indices = rng.randint(0, X.shape[1], size=n_not_finite)
not_finite_values = rng.choice([np.nan, np.inf], size=n_not_finite)

X_ = X.copy()
X_[row_indices, col_indices] = not_finite_values

rus = RandomUnderSampler(random_state=0)
X_res, y_res = rus.fit_resample(X_, Y)

assert y_res.shape == (6,)
assert X_res.shape == (6, 2)
assert np.any(~np.isfinite(X_res))