Skip to content

Commit 35d7af9

Browse files
author
chkoar
committed
Relax reconstructor checks. Add test for simple lists.
1 parent 92dab47 commit 35d7af9

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

imblearn/utils/_validation.py

+5-14
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,15 @@ def _gets_props(self, array):
4949

5050
def _transfrom(self, array, props):
5151
type_ = props["type"].lower()
52-
msg = "Could not convert to {}".format(type_)
5352
if type_ == "list":
5453
ret = array.tolist()
5554
elif type_ == "dataframe":
56-
try:
57-
import pandas as pd
58-
ret = pd.DataFrame(array, columns=props["columns"])
59-
ret = ret.astype(props["dtypes"])
60-
except Exception:
61-
warnings.warn(msg)
55+
import pandas as pd
56+
ret = pd.DataFrame(array, columns=props["columns"])
57+
ret = ret.astype(props["dtypes"])
6258
elif type_ == "series":
63-
try:
64-
import pandas as pd
65-
ret = pd.Series(array,
66-
dtype=props["dtypes"],
67-
name=props["name"])
68-
except Exception:
69-
warnings.warn(msg)
59+
import pandas as pd
60+
ret = pd.Series(array, dtype=props["dtypes"], name=props["name"])
7061
else:
7162
ret = array
7263
return ret

imblearn/utils/estimator_checks.py

+31-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ def check_samplers_pandas(name, Sampler):
258258
X_res_df, y_res_df = sampler.fit_resample(X_df, y_df)
259259
X_res, y_res = sampler.fit_resample(X, y)
260260

261-
# check that we return the same type for dataframes or seires types
261+
# check that we return the same type for dataframes or series types
262262
assert isinstance(X_res_df, pd.DataFrame)
263263
assert isinstance(y_res_df, pd.DataFrame)
264264
assert isinstance(y_res_s, pd.Series)
@@ -272,6 +272,36 @@ def check_samplers_pandas(name, Sampler):
272272
assert_allclose(y_res_s.to_numpy(), y_res)
273273

274274

275+
def check_samplers_list(name, Sampler):
276+
# Check that the can samplers handle simple lists
277+
X, y = make_classification(
278+
n_samples=1000,
279+
n_classes=3,
280+
n_informative=4,
281+
weights=[0.2, 0.3, 0.5],
282+
random_state=0,
283+
)
284+
X_list = X.tolist()
285+
y_list = y.tolist()
286+
sampler = Sampler()
287+
if isinstance(Sampler(), NearMiss):
288+
samplers = [Sampler(version=version) for version in (1, 2, 3)]
289+
290+
else:
291+
samplers = [Sampler()]
292+
293+
for sampler in samplers:
294+
set_random_state(sampler)
295+
X_res, y_res = sampler.fit_resample(X, y)
296+
X_res_list, y_res_list = sampler.fit_resample(X_list, y_list)
297+
298+
assert isinstance(X_res_list, list)
299+
assert isinstance(y_res_list, list)
300+
301+
assert_allclose(X_res, X_res_list)
302+
assert_allclose(y_res, y_res_list)
303+
304+
275305
def check_samplers_multiclass_ova(name, Sampler):
276306
# Check that multiclass target lead to the same results than OVA encoding
277307
X, y = make_classification(

0 commit comments

Comments
 (0)