Skip to content

MAINT Parameters validation for sklearn.preprocessing.normalize #26069

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 3 commits into from
Apr 4, 2023
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
16 changes: 10 additions & 6 deletions sklearn/preprocessing/_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1777,6 +1777,15 @@ def robust_scale(
return X


@validate_params(
{
"X": ["array-like", "sparse matrix"],
"norm": [StrOptions({"l1", "l2", "max"})],
"axis": [Options(Integral, {0, 1})],
"copy": ["boolean"],
"return_norm": ["boolean"],
}
)
def normalize(X, norm="l2", *, axis=1, copy=True, return_norm=False):
"""Scale input vectors individually to unit norm (vector length).

Expand Down Expand Up @@ -1826,15 +1835,10 @@ def normalize(X, norm="l2", *, axis=1, copy=True, return_norm=False):
see :ref:`examples/preprocessing/plot_all_scaling.py
<sphx_glr_auto_examples_preprocessing_plot_all_scaling.py>`.
"""
if norm not in ("l1", "l2", "max"):
raise ValueError("'%s' is not a supported norm" % norm)

if axis == 0:
sparse_format = "csc"
elif axis == 1:
else: # axis == 1:
sparse_format = "csr"
else:
raise ValueError("'%d' is not a supported axis" % axis)

X = check_array(
X,
Expand Down
4 changes: 0 additions & 4 deletions sklearn/preprocessing/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1988,10 +1988,6 @@ def test_normalize():
# Only tests functionality not used by the tests for Normalizer.
X = np.random.RandomState(37).randn(3, 2)
assert_array_equal(normalize(X, copy=False), normalize(X.T, axis=0, copy=False).T)
with pytest.raises(ValueError):
normalize([[0]], axis=2)
with pytest.raises(ValueError):
normalize([[0]], norm="l3")

rs = np.random.RandomState(0)
X_dense = rs.randn(10, 5)
Expand Down
1 change: 1 addition & 0 deletions sklearn/tests/test_public_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ def _check_function_param_validation(
"sklearn.preprocessing.binarize",
"sklearn.preprocessing.label_binarize",
"sklearn.preprocessing.maxabs_scale",
"sklearn.preprocessing.normalize",
"sklearn.preprocessing.scale",
"sklearn.random_projection.johnson_lindenstrauss_min_dim",
"sklearn.svm.l1_min_c",
Expand Down