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

Remove assert warn from preprocessing tests #19691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions 37 sklearn/preprocessing/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
from sklearn.utils._testing import assert_array_almost_equal
from sklearn.utils._testing import assert_array_equal
from sklearn.utils._testing import assert_array_less
from sklearn.utils._testing import assert_warns_message
from sklearn.utils._testing import assert_no_warnings
from sklearn.utils._testing import assert_allclose
from sklearn.utils._testing import assert_allclose_dense_sparse
from sklearn.utils._testing import skip_if_32bit
Expand Down Expand Up @@ -490,28 +488,37 @@ def test_standard_scaler_numerical_stability():
x = np.full(8, np.log(1e-5), dtype=np.float64)
# This does not raise a warning as the number of samples is too low
# to trigger the problem in recent numpy
x_scaled = assert_no_warnings(scale, x)
with pytest.warns(None) as record:
scale(x)
assert len(record) == 0
assert_array_almost_equal(scale(x), np.zeros(8))

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

x = np.full(10, 1e-100, dtype=np.float64)
x_small_scaled = assert_no_warnings(scale, x)
with pytest.warns(None) as record:
x_small_scaled = scale(x)
assert len(record) == 0
assert_array_almost_equal(x_small_scaled, np.zeros(10))

# Large values can cause (often recoverable) numerical stability issues:
x_big = np.full(10, 1e100, dtype=np.float64)
w = "Dataset may contain too large values"
x_big_scaled = assert_warns_message(UserWarning, w, scale, x_big)
warning_message = (
"Dataset may contain too large values"
)
with pytest.warns(UserWarning, match=warning_message):
x_big_scaled = scale(x_big)
assert_array_almost_equal(x_big_scaled, np.zeros(10))
assert_array_almost_equal(x_big_scaled, x_small_scaled)

x_big_centered = assert_warns_message(UserWarning, w, scale, x_big,
with_std=False)
with pytest.warns(UserWarning, match=warning_message):
x_big_centered = scale(x_big, with_std=False)
assert_array_almost_equal(x_big_centered, np.zeros(10))
assert_array_almost_equal(x_big_centered, x_small_scaled)

Expand Down Expand Up @@ -1438,9 +1445,11 @@ def test_quantile_transform_sparse_ignore_zeros():
n_quantiles=5)

# dense case -> warning raise
assert_warns_message(UserWarning, "'ignore_implicit_zeros' takes effect"
" only with sparse matrix. This parameter has no"
" effect.", transformer.fit, X)
warning_message = ("'ignore_implicit_zeros' takes effect"
" only with sparse matrix. This parameter has no"
" effect.")
with pytest.warns(UserWarning, match=warning_message):
transformer.fit(X)

X_expected = np.array([[0, 0],
[0, 0],
Expand Down
21 changes: 11 additions & 10 deletions 21 sklearn/preprocessing/tests/test_discretization.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from sklearn.utils._testing import (
assert_array_almost_equal,
assert_array_equal,
assert_warns_message,
assert_allclose_dense_sparse
)

Expand Down Expand Up @@ -109,9 +108,10 @@ def test_same_min_max(strategy):
[1, 0],
[1, 1]])
est = KBinsDiscretizer(strategy=strategy, n_bins=3, encode='ordinal')
assert_warns_message(UserWarning,
"Feature 0 is constant and will be replaced "
"with 0.", est.fit, X)
warning_message = ("Feature 0 is constant and will be replaced "
"with 0.")
with pytest.warns(UserWarning, match=warning_message):
est.fit(X)
assert est.n_bins_[0] == 1
# replace the feature with zeros
Xt = est.transform(X)
Expand Down Expand Up @@ -257,9 +257,9 @@ def test_overwrite():
def test_redundant_bins(strategy, expected_bin_edges):
X = [[0], [0], [0], [0], [3], [3]]
kbd = KBinsDiscretizer(n_bins=3, strategy=strategy)
msg = ("Bins whose width are too small (i.e., <= 1e-8) in feature 0 "
"are removed. Consider decreasing the number of bins.")
assert_warns_message(UserWarning, msg, kbd.fit, X)
warning_message = ("Consider decreasing the number of bins.")
with pytest.warns(UserWarning, match=warning_message):
kbd.fit(X)
assert_array_almost_equal(kbd.bin_edges_[0], expected_bin_edges)


Expand All @@ -269,9 +269,10 @@ def test_percentile_numeric_stability():
Xt = np.array([0, 0, 4]).reshape(-1, 1)
kbd = KBinsDiscretizer(n_bins=10, encode='ordinal',
strategy='quantile')
msg = ("Bins whose width are too small (i.e., <= 1e-8) in feature 0 "
"are removed. Consider decreasing the number of bins.")
assert_warns_message(UserWarning, msg, kbd.fit, X)
warning_message = ("Consider decreasing the number of bins.")
with pytest.warns(UserWarning, match=warning_message):
kbd.fit(X)

assert_array_almost_equal(kbd.bin_edges_[0], bin_edges)
assert_array_almost_equal(kbd.transform(X), Xt)

Expand Down
27 changes: 16 additions & 11 deletions 27 sklearn/preprocessing/tests/test_function_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

from sklearn.preprocessing import FunctionTransformer
from sklearn.utils._testing import (assert_array_equal,
assert_allclose_dense_sparse)
from sklearn.utils._testing import assert_warns_message, assert_no_warnings
assert_allclose_dense_sparse)


def _make_func(args_store, kwargs_store, func=lambda X, *a, **k: X):
Expand Down Expand Up @@ -127,29 +126,35 @@ def test_check_inverse():
accept_sparse=accept_sparse,
check_inverse=True,
validate=True)
assert_warns_message(UserWarning,
"The provided functions are not strictly"
" inverse of each other. If you are sure you"
" want to proceed regardless, set"
" 'check_inverse=False'.",
trans.fit, X)
warning_message = ("The provided functions are not strictly"
" inverse of each other. If you are sure you"
" want to proceed regardless, set"
" 'check_inverse=False'.")
with pytest.warns(UserWarning, match=warning_message):
trans.fit(X)

trans = FunctionTransformer(func=np.expm1,
inverse_func=np.log1p,
accept_sparse=accept_sparse,
check_inverse=True,
validate=True)
Xt = assert_no_warnings(trans.fit_transform, X)
with pytest.warns(None) as record:
Xt = trans.fit_transform(X)
assert len(record) == 0
assert_allclose_dense_sparse(X, trans.inverse_transform(Xt))

# check that we don't check inverse when one of the func or inverse is not
# provided.
trans = FunctionTransformer(func=np.expm1, inverse_func=None,
check_inverse=True, validate=True)
assert_no_warnings(trans.fit, X_dense)
with pytest.warns(None) as record:
trans.fit(X_dense)
assert len(record) == 0
trans = FunctionTransformer(func=None, inverse_func=np.expm1,
check_inverse=True, validate=True)
assert_no_warnings(trans.fit, X_dense)
with pytest.warns(None) as record:
trans.fit(X_dense)
assert len(record) == 0


def test_function_transformer_frame():
Expand Down
14 changes: 6 additions & 8 deletions 14 sklearn/preprocessing/tests/test_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from sklearn.utils.multiclass import type_of_target

from sklearn.utils._testing import assert_array_equal
from sklearn.utils._testing import assert_warns_message
from sklearn.utils._testing import ignore_warnings
from sklearn.utils import _to_object_array

Expand Down Expand Up @@ -351,15 +350,14 @@ def test_multilabel_binarizer_unknown_class():
mlb = MultiLabelBinarizer()
y = [[1, 2]]
Y = np.array([[1, 0], [0, 1]])
w = 'unknown class(es) [0, 4] will be ignored'
matrix = assert_warns_message(UserWarning, w,
mlb.fit(y).transform, [[4, 1], [2, 0]])
assert_array_equal(matrix, Y)
warning_message = 'unknown class.* will be ignored'
with pytest.warns(UserWarning, match=warning_message):
matrix = mlb.fit(y).transform([[4, 1], [2, 0]])

Y = np.array([[1, 0, 0], [0, 1, 0]])
mlb = MultiLabelBinarizer(classes=[1, 2, 3])
matrix = assert_warns_message(UserWarning, w,
mlb.fit(y).transform, [[4, 1], [2, 0]])
with pytest.warns(UserWarning, match=warning_message):
matrix = mlb.fit(y).transform([[4, 1], [2, 0]])
assert_array_equal(matrix, Y)


Expand Down Expand Up @@ -535,7 +533,7 @@ def check_binarized_results(y, classes, pos_label, neg_label, expected):
output_type=y_type,
classes=classes,
threshold=((neg_label +
pos_label) /
pos_label) /
2.))

assert_array_equal(toarray(inversed), toarray(y))
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.