8000 Revert "MNT Remove utils.validation._shape_repr (#13083)" · xhluca/scikit-learn@b905339 · GitHub
[go: up one dir, main page]

Skip to content

Commit b905339

Browse files
author
Xing
authored
Revert "MNT Remove utils.validation._shape_repr (scikit-learn#13083)"
This reverts commit 21809ce.
1 parent 9f74811 commit b905339

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

sklearn/utils/validation.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,40 @@ def _num_samples(x):
149149
return len(x)
150150

151151

152+
def _shape_repr(shape):
153+
"""Return a platform independent representation of an array shape
154+
155+
Under Python 2, the `long` type introduces an 'L' suffix when using the
156+
default %r format for tuples of integers (typically used to store the shape
157+
of an array).
158+
159+
Under Windows 64 bit (and Python 2), the `long` type is used by default
160+
in numpy shapes even when the integer dimensions are well below 32 bit.
161+
The platform specific type causes string messages or doctests to change
162+
from one platform to another which is not desirable.
163+
164+
Under Python 3, there is no more `long` type so the `L` suffix is never
165+
introduced in string representation.
166+
167+
>>> _shape_repr((1, 2))
168+
'(1, 2)'
169+
>>> one = 2 ** 64 / 2 ** 64 # force an upcast to `long` under Python 2
170+
>>> _shape_repr((one, 2 * one))
171+
'(1, 2)'
172+
>>> _shape_repr((1,))
173+
'(1,)'
174+
>>> _shape_repr(())
175+
'()'
176+
"""
177+
if len(shape) == 0:
178+
return "()"
179+
joined = ", ".join("%d" % e for e in shape)
180+
if len(shape) == 1:
181+
# special notation for singleton tuples
182+
joined += ','
183+
return "(%s)" % joined
184+
18 D374 5+
152186
def check_memory(memory):
153187
"""Check that ``memory`` is joblib.Memory-like.
154188
@@ -537,20 +571,21 @@ def check_array(array, accept_sparse=False, accept_large_sparse=True,
537571
_assert_all_finite(array,
538572
allow_nan=force_all_finite == 'allow-nan')
539573

574+
shape_repr = _shape_repr(array.shape)
540575
if ensure_min_samples > 0:
541576
n_samples = _num_samples(array)
542577
if n_samples < ensure_min_samples:
543578
raise ValueError("Found array with %d sample(s) (shape=%s) while a"
544579
" minimum of %d is required%s."
545-
% (n_samples, array.shape, ensure_min_samples,
580+
% (n_samples, shape_repr, ensure_min_samples,
546581
context))
547582

548583
if ensure_min_features > 0 and array.ndim == 2:
549584
n_features = array.shape[1]
550585
if n_features < ensure_min_features:
551586
raise ValueError("Found array with %d feature(s) (shape=%s) while"
552587
" a minimum of %d is required%s."
553-
% (n_features, array.shape, ensure_min_features,
588+
% (n_features, shape_repr, ensure_min_features,
554589
context))
555590

556591
if warn_on_dtype and dtype_orig is not None and array.dtype != dtype_orig:

0 commit comments

Comments
 (0)
0