8000 ENH Better error message for check_scalar by thomasjpfan · Pull Request #22218 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

ENH Better error message for check_scalar #22218

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
merged 13 commits into from
Feb 11, 2022
3 changes: 3 additions & 0 deletions doc/whats_new/v1.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,9 @@ Changelog
left corner of the HTML representation to show how the elements are
clickable. :pr:`21298` by `Thomas Fan`_.

- |Enhancement| :func:`utils.validation.check_scalar` now has better messages
when displaying the type. :pr:`22218` by `Thomas Fan`_.

- |Fix| :func:`check_scalar` raises an error when `include_boundaries={"left", "right"}`
and the boundaries are not set.
:pr:`22027` by `Marie Lanternier <mlant>`.
Expand Down
6 changes: 2 additions & 4 deletions sklearn/cluster/tests/test_birch.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,14 @@ def test_birch_fit_attributes_deprecated(attribute):
(
{"branching_factor": 1.5},
TypeError,
"branching_factor must be an instance of <class 'numbers.Integral'>, not"
" <class 'float'>.",
"branching_factor must be an instance of int, not float.",
),
({"branching_factor": -2}, ValueError, "branching_factor == -2, must be > 1."),
({"n_clusters": 0}, ValueError, "n_clusters == 0, must be >= 1."),
(
{"n_clusters": 2.5},
TypeError,
"n_clusters must be an instance of <class 'numbers.Integral'>, not <class"
" 'float'>.",
"n_clusters must be an instance of int, not float.",
),
(
{"n_clusters": "whatever"},
Expand Down
9 changes: 3 additions & 6 deletions sklearn/cluster/tests/test_dbscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,24 +436,21 @@ def test_dbscan_precomputed_metric_with_initial_rows_zero():
(
{"min_samples": 1.5},
TypeError,
"min_samples must be an instance of <class 'numbers.Integral'>, not <class"
" 'float'>.",
"min_samples must be an instance of int, not float.",
),
({"min_samples": -2}, ValueError, "min_samples == -2, must be >= 1."),
({"leaf_size": 0}, ValueError, "leaf_size == 0, must be >= 1."),
(
{"leaf_size": 2.5},
TypeError,
"leaf_size must be an instance of <class 'numbers.Integral'>, not <class"
" 'float'>.",
"leaf_size must be an instance of int, not float.",
),
({"leaf_size": -3}, ValueError, "leaf_size == -3, must be >= 1."),
({"p": -2}, ValueError, "p == -2, must be >= 0.0."),
(
{"n_jobs": 2.5},
TypeError,
"n_jobs must be an instance of <class 'numbers.Integral'>, not <class"
" 'float'>.",
"n_jobs must be an instance of int, not float.",
),
],
)
Expand Down
6 changes: 2 additions & 4 deletions sklearn/cluster/tests/test_spectral.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,15 @@ def test_spectral_unknown_assign_labels():
X,
{"n_clusters": 1.5},
TypeError,
"n_clusters must be an instance of <class 'numbers.Integral'>,"
" not <class 'float'>",
"n_clusters must be an instance of int, not float",
),
(X, {"n_init": -1}, ValueError, "n_init == -1, must be >= 1"),
(X, {"n_init": 0}, ValueError, "n_init == 0, must be >= 1"),
(
X,
{"n_init": 1.5},
TypeError,
"n_init must be an instance of <class 'numbers.Integral'>,"
" not <class 'float'>",
"n_init must be an instance of int, not float",
),
(X, {"gamma": -1}, ValueError, "gamma == -1, must be >= 1"),
(X, {"gamma": 0}, ValueError, "gamma == 0, must be >= 1"),
Expand Down
4 changes: 2 additions & 2 deletions sklearn/cross_decomposition/tests/test_pls.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ def test_scale_and_stability(Est, X, Y):
(
2.0,
TypeError,
"n_components must be an instance of <class 'numbers.Integral'>",
"n_components must be an instance of int",
),
],
)
Expand All @@ -498,7 +498,7 @@ def test_n_components_bounds(Est, n_components, err_type, err_msg):
(
2.0,
TypeError,
"n_components must be an instance of <class 'numbers.Integral'>",
"n_components must be an instance of int",
),
],
)
Expand Down
2 changes: 1 addition & 1 deletion sklearn/decomposition/tests/test_pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ def test_pca_randomized_svd_n_oversamples():
(
{"n_oversamples": 1.5},
TypeError,
"n_oversamples must be an instance of <class 'numbers.Integral'>",
"n_oversamples must be an instance of int",
),
],
)
Expand Down
26 changes: 13 additions & 13 deletions sklearn/ensemble/tests/test_gradient_boosting.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,21 @@ def test_classification_toy(loss):
(
{"learning_rate": "foo"},
TypeError,
"learning_rate must be an instance of <class 'numbers.Real'>",
"learning_rate must be an instance of float",
),
({"n_estimators": 0}, ValueError, "n_estimators == 0, must be >= 1"),
(
{"n_estimators": 1.5},
TypeError,
"n_estimators must be an instance of <class 'numbers.Integral'>,",
"n_estimators must be an instance of int,",
),
({"loss": "foobar"}, ValueError, "Loss 'foobar' not supported"),
({"subsample": 0.0}, ValueError, "subsample == 0.0, must be > 0.0"),
({"subsample": 1.1}, ValueError, "subsample == 1.1, must be <= 1.0"),
(
{"subsample": "foo"},
TypeError,
"subsample must be an instance of <class 'numbers.Real'>",
"subsample must be an instance of float",
),
({"init": {}}, ValueError, "The init parameter must be an estimator or 'zero'"),
({"max_features": 0}, ValueError, "max_features == 0, must be >= 1"),
Expand All @@ -125,27 +125,27 @@ def test_classification_toy(loss):
(
{"validation_fraction": "foo"},
TypeError,
"validation_fraction must be an instance of <class 'numbers.Real'>",
"validation_fraction must be an instance of float",
),
({"n_iter_no_change": 0}, ValueError, "n_iter_no_change == 0, must be >= 1"),
(
{"n_iter_no_change": 1.5},
TypeError,
"n_iter_no_change must be an instance of <class 'numbers.Integral'>,",
"n_iter_no_change must be an instance of int,",
),
({"tol": 0.0}, ValueError, "tol == 0.0, must be > 0.0"),
(
{"tol": "foo"},
TypeError,
"tol must be an instance of <class 'numbers.Real'>,",
"tol must be an instance of float,",
),
# The following parameters are checked in BaseDecisionTree
({"min_samples_leaf": 0}, ValueError, "min_samples_leaf == 0, must be >= 1"),
({"min_samples_leaf": 0.0}, ValueError, "min_samples_leaf == 0.0, must be > 0"),
(
{"min_samples_leaf": "foo"},
TypeError,
"min_samples_leaf must be an instance of <class 'numbers.Real'>",
"min_samples_leaf must be an instance of float",
),
({"min_samples_split": 1}, ValueError, "min_samples_split == 1, must be >= 2"),
(
Expand All @@ -161,7 +161,7 @@ def test_classification_toy(loss):
(
{"min_samples_split": "foo"},
TypeError,
"min_samples_split must be an instance of <class 'numbers.Real'>",
"min_samples_split must be an instance of float",
),
(
{"min_weight_fraction_leaf": -1},
Expand All @@ -176,19 +176,19 @@ def test_classification_toy(loss):
(
{"min_weight_fraction_leaf": "foo"},
TypeError,
"min_weight_fraction_leaf must be an instance of <class 'numbers.Real'>",
"min_weight_fraction_leaf must be an instance of float",
),
({"max_leaf_nodes": 0}, ValueError, "max_leaf_nodes == 0, must be >= 2"),
(
{"max_leaf_nodes": 1.5},
TypeError,
"max_leaf_nodes must be an instance of <class 'numbers.Integral'>",
"max_leaf_nodes must be an instance of int",
),
({"max_depth": -1}, ValueError, "max_depth == -1, must be >= 1"),
(
{"max_depth": 1.1},
TypeError,
"max_depth must be an instance of <class 'numbers.Integral'>",
"max_depth must be an instance of int",
),
(
{"min_impurity_decrease": -1},
Expand All @@ -198,13 +198,13 @@ def test_classification_toy(loss):
(
{"min_impurity_decrease": "foo"},
TypeError,
"min_impurity_decrease must be an instance of <class 'numbers.Real'>",
"min_impurity_decrease must be an instance of float",
),
({"ccp_alpha": -1.0}, ValueError, "ccp_alpha == -1.0, must be >= 0.0"),
(
{"ccp_alpha": "foo"},
TypeError,
"ccp_alpha must be an instance of <class 'numbers.Real'>",
"ccp_alpha must be an instance of float",
),
({"criterion": "mae"}, ValueError, "criterion='mae' is not supported."),
],
Expand Down
3 changes: 1 addition & 2 deletions sklearn/ensemble/tests/test_weight_boosting.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,8 +564,7 @@ def test_adaboostregressor_sample_weight():
(
{"n_estimators": 1.5},
TypeError,
"n_estimators must be an instance of <class 'numbers.Integral'>,"
" not <class 'float'>",
"n_estimators must be an instance of int, not float",
),
({"learning_rate": -1}, ValueError, "learning_rate == -1, must be > 0."),
({"learning_rate": 0}, ValueError, "learning_rate == 0, must be > 0."),
Expand Down
3 changes: 1 addition & 2 deletions sklearn/feature_extraction/tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,8 +869,7 @@ def test_vectorizer_min_df():
(
{"max_features": 3.5},
TypeError,
"max_features must be an instance of <class 'numbers.Integral'>, not <class"
" 'float'>",
"max_features must be an instance of int, not float",
),
),
)
Expand Down
21 changes: 8 additions & 13 deletions sklearn/linear_model/_glm/tests/test_glm.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,52 +142,47 @@ def test_glm_solver_argument(solver):
(
{"max_iter": "not a number"},
TypeError,
"max_iter must be an instance of <class 'numbers.Integral'>, not <class"
" 'str'>",
"max_iter must be an instance of int, not str",
),
(
{"max_iter": [1]},
TypeError,
"max_iter must be an instance of <class 'numbers.Integral'>,"
" not <class 'list'>",
"max_iter must be an instance of int, not list",
),
(
{"max_iter": 5.5},
TypeError,
"max_iter must be an instance of <class 'numbers.Integral'>,"
" not <class 'float'>",
"max_iter must be an instance of int, not float",
),
({"alpha": -1}, ValueError, "alpha == -1, must be >= 0.0"),
(
{"alpha": "1"},
TypeError,
"alpha must be an instance of <class 'numbers.Real'>, not <class 'str'>",
"alpha must be an instance of float, not str",
),
({"tol": -1.0}, ValueError, "tol == -1.0, must be > 0."),
({"tol": 0.0}, ValueError, "tol == 0.0, must be > 0.0"),
({"tol": 0}, ValueError, "tol == 0, must be > 0.0"),
(
{"tol": "1"},
TypeError,
"tol must be an instance of <class 'numbers.Real'>, not <class 'str'>",
"tol must be an instance of float, not str",
),
(
{"tol": [1e-3]},
TypeError,
"tol must be an instance of <class 'numbers.Real'>, not <class 'list'>",
"tol must be an instance of float, not list",
),
({"verbose": -1}, ValueError, "verbose == -1, must be >= 0."),
(
{"verbose": "1"},
TypeError,
"verbose must be an instance of <class 'numbers.Integral'>, not <class"
" 'str'>",
"verbose must be an instance of int, not str",
),
(
{"verbose": 1.0},
TypeError,
"verbose must be an instance of <class 'numbers.Integral'>, not <class"
" 'float'>",
"verbose must be an instance of int, not float",
),
],
)
Expand Down
7 changes: 3 additions & 4 deletions sklearn/linear_model/tests/test_coordinate_descent.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,19 @@ def test_assure_warning_when_normalize(CoordinateDescentModel, normalize, n_warn
(
{"l1_ratio": "1"},
TypeError,
"l1_ratio must be an instance of <class 'numbers.Real'>, not <class 'str'>",
"l1_ratio must be an instance of float, not str",
),
({"tol": -1.0}, ValueError, "tol == -1.0, must be >= 0."),
(
{"tol": "1"},
TypeError,
"tol must be an instance of <class 'numbers.Real'>, not <class 'str'>",
"tol must be an instance of float, not str",
),
({"max_iter": 0}, ValueError, "max_iter == 0, must be >= 1."),
(
{"max_iter": "1"},
TypeError,
"max_iter must be an instance of <class 'numbers.Integral'>, not <class"
" 'str'>",
"max_iter must be an instance of int, not str",
),
],
)
Expand Down
10 changes: 4 additions & 6 deletions sklearn/linear_model/tests/test_ridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,20 +342,19 @@ def test_ridge_individual_penalties():
(
{"alpha": "1"},
TypeError,
"alpha must be an instance of <class 'numbers.Real'>, not <class 'str'>",
"alpha must be an instance of float, not str",
),
({"max_iter": 0}, ValueError, "max_iter == 0, must be >= 1."),
(
{"max_iter": "1"},
TypeError,
"max_iter must be an instance of <class 'numbers.Integral'>, not <class"
" 'str'>",
"max_iter must be an instance of int, not str",
),
({"tol": -1.0}, ValueError, "tol == -1.0, must be >= 0."),
(
{"tol": "1"},
TypeError,
"tol must be an instance of <class 'numbers.Real'>, not <class 'str'>",
"tol must be an instance of float, not str",
),
],
)
Expand Down Expand Up @@ -1283,8 +1282,7 @@ def test_ridgecv_int_alphas():
(
{"alphas": (1, 1.0, "1")},
TypeError,
r"alphas\[2\] must be an instance of <class 'numbers.Real'>, not <class"
r" 'str'>",
r"alphas\[2\] must be an instance of float, not str",
),
],
)
Expand Down
5 changes: 1 addition & 4 deletions sklearn/preprocessing/tests/test_discretization.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,7 @@ def test_kbinsdiscretizer_subsample_invalid_type():
n_bins=10, encode="ordinal", strategy="quantile", subsample="full"
)

msg = (
"subsample must be an instance of <class 'numbers.Integral'>, not "
"<class 'str'>."
)
msg = "subsample must be an instance of int, not str."
with pytest.raises(TypeError, match=msg):
kbd.fit(X)

Expand Down
Loading
0