Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3e45aee

Browse filesBrowse files
authored
TST Remove assert warn from preprocessing tests (#19691)
1 parent 0892a98 commit 3e45aee
Copy full SHA for 3e45aee

File tree

4 files changed

+56
-43
lines changed
Filter options

4 files changed

+56
-43
lines changed

‎sklearn/preprocessing/tests/test_data.py

Copy file name to clipboardExpand all lines: sklearn/preprocessing/tests/test_data.py
+23-14Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
from sklearn.utils._testing import assert_array_almost_equal
2020
from sklearn.utils._testing import assert_array_equal
2121
from sklearn.utils._testing import assert_array_less
22-
from sklearn.utils._testing import assert_warns_message
23-
from sklearn.utils._testing import assert_no_warnings
2422
from sklearn.utils._testing import assert_allclose
2523
from sklearn.utils._testing import assert_allclose_dense_sparse
2624
from sklearn.utils._testing import skip_if_32bit
@@ -291,28 +289,37 @@ def test_standard_scaler_numerical_stability():
291289
x = np.full(8, np.log(1e-5), dtype=np.float64)
292290
# This does not raise a warning as the number of samples is too low
293291
# to trigger the problem in recent numpy
294-
x_scaled = assert_no_warnings(scale, x)
292+
with pytest.warns(None) as record:
293+
scale(x)
294+
assert len(record) == 0
295295
assert_array_almost_equal(scale(x), np.zeros(8))
296296

297297
# with 2 more samples, the std computation run into numerical issues:
298298
x = np.full(10, np.log(1e-5), dtype=np.float64)
299-
w = "standard deviation of the data is probably very close to 0"
300-
x_scaled = assert_warns_message(UserWarning, w, scale, x)
299+
warning_message = (
300+
"standard deviation of the data is probably very close to 0"
301+
)
302+
with pytest.warns(UserWarning, match=warning_message):
303+
x_scaled = scale(x)
301304
assert_array_almost_equal(x_scaled, np.zeros(10))
302305

303306
x = np.full(10, 1e-100, dtype=np.float64)
304-
x_small_scaled = assert_no_warnings(scale, x)
307+
with pytest.warns(None) as record:
308+
x_small_scaled = scale(x)
309+
assert len(record) == 0
305310
assert_array_almost_equal(x_small_scaled, np.zeros(10))
306311

307312
# Large values can cause (often recoverable) numerical stability issues:
308313
x_big = np.full(10, 1e100, dtype=np.float64)
309-
w = "Dataset may contain too large values"
310-
x_big_scaled = assert_warns_message(UserWarning, w, scale, x_big)
314+
warning_message = (
315+
"Dataset may contain too large values"
316+
)
317+
with pytest.warns(UserWarning, match=warning_message):
318+
x_big_scaled = scale(x_big)
311319
assert_array_almost_equal(x_big_scaled, np.zeros(10))
312320
assert_array_almost_equal(x_big_scaled, x_small_scaled)
313-
314-
x_big_centered = assert_warns_message(UserWarning, w, scale, x_big,
315-
with_std=False)
321+
with pytest.warns(UserWarning, match=warning_message):
322+
x_big_centered = scale(x_big, with_std=False)
316323
assert_array_almost_equal(x_big_centered, np.zeros(10))
317324
assert_array_almost_equal(x_big_centered, x_small_scaled)
318325

@@ -1239,9 +1246,11 @@ def test_quantile_transform_sparse_ignore_zeros():
12391246
n_quantiles=5)
12401247

12411248
# dense case -> warning raise
1242-
assert_warns_message(UserWarning, "'ignore_implicit_zeros' takes effect"
1243-
" only with sparse matrix. This parameter has no"
1244-
" effect.", transformer.fit, X)
1249+
warning_message = ("'ignore_implicit_zeros' takes effect"
1250+
" only with sparse matrix. This parameter has no"
1251+
" effect.")
1252+
with pytest.warns(UserWarning, match=warning_message):
1253+
transformer.fit(X)
12451254

12461255
X_expected = np.array([[0, 0],
12471256
[0, 0],

‎sklearn/preprocessing/tests/test_discretization.py

Copy file name to clipboardExpand all lines: sklearn/preprocessing/tests/test_discretization.py
+11-10Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from sklearn.utils._testing import (
1010
assert_array_almost_equal,
1111
assert_array_equal,
12-
assert_warns_message,
1312
assert_allclose_dense_sparse
1413
)
1514

@@ -109,9 +108,10 @@ def test_same_min_max(strategy):
109108
[1, 0],
110109
[1, 1]])
111110
est = KBinsDiscretizer(strategy=strategy, n_bins=3, encode='ordinal')
112-
assert_warns_message(UserWarning,
113-
"Feature 0 is constant and will be replaced "
114-
"with 0.", est.fit, X)
111+
warning_message = ("Feature 0 is constant and will be replaced "
112+
"with 0.")
113+
with pytest.warns(UserWarning, match=warning_message):
114+
est.fit(X)
115115
assert est.n_bins_[0] == 1
116116
# replace the feature with zeros
117117
Xt = est.transform(X)
@@ -257,9 +257,9 @@ def test_overwrite():
257257
def test_redundant_bins(strategy, expected_bin_edges):
258258
X = [[0], [0], [0], [0], [3], [3]]
259259
kbd = KBinsDiscretizer(n_bins=3, strategy=strategy)
260-
msg = ("Bins whose width are too small (i.e., <= 1e-8) in feature 0 "
261-
"are removed. Consider decreasing the number of bins.")
262-
assert_warns_message(UserWarning, msg, kbd.fit, X)
260+
warning_message = ("Consider decreasing the number of bins.")
261+
with pytest.warns(UserWarning, match=warning_message):
262+
kbd.fit(X)
263263
assert_array_almost_equal(kbd.bin_edges_[0], expected_bin_edges)
264264

265265

@@ -269,9 +269,10 @@ def test_percentile_numeric_stability():
269269
Xt = np.array([0, 0, 4]).reshape(-1, 1)
270270
kbd = KBinsDiscretizer(n_bins=10, encode='ordinal',
271271
strategy='quantile')
272-
msg = ("Bins whose width are too small (i.e., <= 1e-8) in feature 0 "
273-
"are removed. Consider decreasing the number of bins.")
274-
assert_warns_message(UserWarning, msg, kbd.fit, X)
272+
warning_message = ("Consider decreasing the number of bins.")
273+
with pytest.warns(UserWarning, match=warning_message):
274+
kbd.fit(X)
275+
275276
assert_array_almost_equal(kbd.bin_edges_[0], bin_edges)
276277
assert_array_almost_equal(kbd.transform(X), Xt)
277278

‎sklearn/preprocessing/tests/test_function_transformer.py

Copy file name to clipboardExpand all lines: sklearn/preprocessing/tests/test_function_transformer.py
+16-11Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
from sklearn.preprocessing import FunctionTransformer
66
from sklearn.utils._testing import (assert_array_equal,
7-
assert_allclose_dense_sparse)
8-
from sklearn.utils._testing import assert_warns_message, assert_no_warnings
7+
assert_allclose_dense_sparse)
98

109

1110
def _make_func(args_store, kwargs_store, func=lambda X, *a, **k: X):
@@ -127,29 +126,35 @@ def test_check_inverse():
127126
accept_sparse=accept_sparse,
128127
check_inverse=True,
129128
validate=True)
130-
assert_warns_message(UserWarning,
131-
"The provided functions are not strictly"
132-
" inverse of each other. If you are sure you"
133-
" want to proceed regardless, set"
134-
" 'check_inverse=False'.",
135-
trans.fit, X)
129+
warning_message = ("The provided functions are not strictly"
130+
" inverse of each other. If you are sure you"
131+
" want to proceed regardless, set"
132+
" 'check_inverse=False'.")
133+
with pytest.warns(UserWarning, match=warning_message):
134+
trans.fit(X)
136135

137136
trans = FunctionTransformer(func=np.expm1,
138137
inverse_func=np.log1p,
139138
accept_sparse=accept_sparse,
140139
check_inverse=True,
141140
validate=True)
142-
Xt = assert_no_warnings(trans.fit_transform, X)
141+
with pytest.warns(None) as record:
142+
Xt = trans.fit_transform(X)
143+
assert len(record) == 0
143144
assert_allclose_dense_sparse(X, trans.inverse_transform(Xt))
144145

145146
# check that we don't check inverse when one of the func or inverse is not
146147
# provided.
147148
trans = FunctionTransformer(func=np.expm1, inverse_func=None,
148149
check_inverse=True, validate=True)
149-
assert_no_warnings(trans.fit, X_dense)
150+
with pytest.warns(None) as record:
151+
trans.fit(X_dense)
152+
assert len(record) == 0
150153
trans = FunctionTransformer(func=None, inverse_func=np.expm1,
151154
check_inverse=True, validate=True)
152-
assert_no_warnings(trans.fit, X_dense)
155+
with pytest.warns(None) as record:
156+
trans.fit(X_dense)
157+
assert len(record) == 0
153158

154159

155160
def test_function_transformer_frame():

‎sklearn/preprocessing/tests/test_label.py

Copy file name to clipboardExpand all lines: sklearn/preprocessing/tests/test_label.py
+6-8Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from sklearn.utils.multiclass import type_of_target
1313

1414
from sklearn.utils._testing import assert_array_equal
15-
from sklearn.utils._testing import assert_warns_message
1615
from sklearn.utils._testing import ignore_warnings
1716
from sklearn.utils import _to_object_array
1817

@@ -351,15 +350,14 @@ def test_multilabel_binarizer_unknown_class():
351350
mlb = MultiLabelBinarizer()
352351
y = [[1, 2]]
353352
Y = np.array([[1, 0], [0, 1]])
354-
w = 'unknown class(es) [0, 4] will be ignored'
355-
matrix = assert_warns_message(UserWarning, w,
356-
mlb.fit(y).transform, [[4, 1], [2, 0]])
357-
assert_array_equal(matrix, Y)
353+
warning_message = 'unknown class.* will be ignored'
354+
with pytest.warns(UserWarning, match=warning_message):
355+
matrix = mlb.fit(y).transform([[4, 1], [2, 0]])
358356

359357
Y = np.array([[1, 0, 0], [0, 1, 0]])
360358
mlb = MultiLabelBinarizer(classes=[1, 2, 3])
361-
matrix = assert_warns_message(UserWarning, w,
362-
mlb.fit(y).transform, [[4, 1], [2, 0]])
359+
with pytest.warns(UserWarning, match=warning_message):
360+
matrix = mlb.fit(y).transform([[4, 1], [2, 0]])
363361
assert_array_equal(matrix, Y)
364362

365363

@@ -535,7 +533,7 @@ def check_binarized_results(y, classes, pos_label, neg_label, expected):
535533
output_type=y_type,
536534
classes=classes,
537535
threshold=((neg_label +
538-
pos_label) /
536+
pos_label) /
539537
2.))
540538

541539
assert_array_equal(toarray(inversed), toarray(y))

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.