|
7 | 7 | from scipy import sparse
|
8 | 8 | import pytest
|
9 | 9 |
|
| 10 | +from sklearn.exceptions import NotFittedError |
10 | 11 | from sklearn.utils.testing import assert_array_equal
|
11 | 12 | from sklearn.utils.testing import assert_equal
|
12 | 13 | from sklearn.utils.testing import assert_raises
|
@@ -250,6 +251,28 @@ def test_one_hot_encoder_handle_unknown():
|
250 | 251 | assert_raises(ValueError, oh.fit, X)
|
251 | 252 |
|
252 | 253 |
|
| 254 | +def test_one_hot_encoder_not_fitted(): |
| 255 | + X = np.array([['a'], ['b']]) |
| 256 | + enc = OneHotEncoder(categories=['a', 'b']) |
| 257 | + msg = ("This OneHotEncoder instance is not fitted yet. " |
| 258 | + "Call 'fit' with appropriate arguments before using this method.") |
| 259 | + with pytest.raises(NotFittedError, match=msg): |
| 260 | + enc.transform(X) |
| 261 | + |
| 262 | + |
| 263 | +def test_one_hot_encoder_no_categorical_features(): |
| 264 | + X = np.array([[3, 2, 1], [0, 1, 1]], dtype='float64') |
| 265 | + |
| 266 | + cat = [False, False, False] |
| 267 | + enc = OneHotEncoder(categorical_features=cat) |
| 268 | + with ignore_warnings(category=(DeprecationWarning, FutureWarning)): |
| 269 | + X_tr = enc.fit_transform(X) |
| 270 | + expected_features = np.array(list(), dtype='object') |
| 271 | + assert_array_equal(X, X_tr) |
| 272 | + assert_array_equal(enc.get_feature_names(), expected_features) |
| 273 | + assert enc.categories_ == [] |
| 274 | + |
| 275 | + |
253 | 276 | @pytest.mark.parametrize("output_dtype", [np.int32, np.float32, np.float64])
|
254 | 277 | @pytest.mark.parametrize("input_dtype", [np.int32, np.float32, np.float64])
|
255 | 278 | def test_one_hot_encoder_dtype(input_dtype, output_dtype):
|
|
0 commit comments