@@ -277,6 +277,39 @@ def test_smotenc_categorical_encoder():
277277 assert getattr (smote .categorical_encoder_ , "sparse_output" ) is False
278278
279279
280+ @pytest .mark .parametrize ("drop" , ["first" , "if_binary" ])
281+ def test_smotenc_categorical_encoder_dropped_columns (drop ):
282+ """Check that a clear error is raised when the categorical encoder does not
283+ keep one column per category (e.g. ``OneHotEncoder(drop=...)``).
284+
285+ Non-regression test for:
286+ https://github.com/scikit-learn-contrib/imbalanced-learn/issues/1035
287+ """
288+ rng = np .random .RandomState (0 )
289+ n_samples = 200
290+ X = np .hstack (
291+ [
292+ rng .randn (n_samples , 2 ),
293+ rng .randint (0 , 2 , size = (n_samples , 1 )), # binary categorical
294+ rng .randint (0 , 4 , size = (n_samples , 1 )),
295+ rng .randint (0 , 3 , size = (n_samples , 1 )),
296+ ]
297+ ).astype (object )
298+ y = np .array ([1 ] * 40 + [0 ] * (n_samples - 40 ))
299+ rng .shuffle (y )
300+
301+ encoder = OneHotEncoder (drop = drop , handle_unknown = "ignore" )
302+ smote = SMOTENC (
303+ categorical_features = [2 , 3 , 4 ],
304+ categorical_encoder = encoder ,
305+ sampling_strategy = "minority" ,
306+ random_state = 0 ,
307+ )
308+ err_msg = "SMOTENC requires a one-hot encoding with one column per category"
309+ with pytest .raises (ValueError , match = err_msg ):
310+ smote .fit_resample (X , y )
311+
312+
280313def test_smotenc_bool_categorical ():
281314 """Check that we don't try to early convert the full input data to numeric when
282315 handling a pandas dataframe.
0 commit comments