ENH Add check for non binary variables in OneHotEncoder.
@@ -268,6 +268,22 @@ def test_one_hot_encoder_inverse_if_binary():
assert_array_equal(ohe.inverse_transform(X_tr), X)
+# check that resetting drop option without refitting does not throw an error
+@pytest.mark.parametrize('drop', ['if_binary', 'first', None])
+@pytest.mark.parametrize('reset_drop', ['if_binary', 'first', None])
+def test_one_hot_encoder_drop_reset(drop, reset_drop):
+ X = np.array([['Male', 1],
+ ['Female', 3],
+ ['Female', 2]], dtype=object)
+ ohe = OneHotEncoder(drop=drop, sparse=False)
+ ohe.fit(X)
+ X_tr = ohe.transform(X)
+ feature_names = ohe.get_feature_names()
+ ohe.set_params(drop=reset_drop)
+ assert_array_equal(ohe.inverse_transform(X_tr), X)
+ assert_allclose(ohe.transform(X), X_tr)
+ assert_array_equal(ohe.get_feature_names(), feature_names)
+
@pytest.mark.parametrize("method", ['fit', 'fit_transform'])
@pytest.mark.parametrize("X", [
[1, 2],
@@ -388,8 +404,9 @@ def test_one_hot_encoder_pandas():
@pytest.mark.parametrize("drop, expected_names",
[('first', ['x0_c', 'x2_b']),
+ ('if_binary', ['x0_c', 'x1_2', 'x2_b']),
(['c', 2, 'b'], ['x0_b', 'x2_a'])],
- ids=['first', 'manual'])
+ ids=['first', 'binary', 'manual'])
def test_one_hot_encoder_feature_names_drop(drop, expected_names):
X = [['c', 2, 'a'],
['b', 2, 'b']]
@@ -409,7 +426,7 @@ def test_one_hot_encoder_drop_equals_if_binary():
expected = np.array([[1., 0., 0., 1.],
[0., 1., 0., 0.],
[0., 0., 1., 1.]])
- expected_drop_idx = np.array([-1, 0])
+ expected_drop_idx = np.array([None, 0])
ohe = OneHotEncoder(drop='if_binary', sparse=False)
result = ohe.fit_transform(X)
@@ -423,7 +440,7 @@ def test_one_hot_encoder_drop_equals_if_binary():
expected = np.array([[1., 1.],
[0., 1.],
[0., 1.]])
- expected_drop_idx = np.array([0, -1])
+ expected_drop_idx = np.array([0, None])
ohe = OneHotEncoder(drop='if_binary', sparse=False)
result = ohe.fit_transform(X)
@@ -662,9 +679,9 @@ def test_categories(density, drop):
for drop_cat, drop_idx, cat_list in zip(drop,
ohe_test.drop_idx_,
ohe_test.categories_):
- assert cat_list[drop_idx] == drop_cat
+ assert cat_list[int(drop_idx)] == drop_cat
assert isinstance(ohe_test.drop_idx_, np.ndarray)
- assert ohe_test.drop_idx_.dtype == np.int_
+ assert ohe_test.drop_idx_.dtype == np.object
@pytest.mark.parametrize('Encoder', [OneHotEncoder, OrdinalEncoder])
