8000 ENH: Let numpy.size accept multiple axes. · numpy/numpy@8f17ccf · GitHub
[go: up one dir, main page]

Skip to content
10000

Commit 8f17ccf

Browse files
committed
ENH: Let numpy.size accept multiple axes.
1 parent 7b30ce7 commit 8f17ccf

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

numpy/_core/fromnumeric.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
"""
44
import functools
5+
import math
56
import types
67
import warnings
78

@@ -3572,8 +3573,8 @@ def size(a, axis=None):
35723573
----------
35733574
a : array_like
35743575
Input data.
3575-
axis : int, optional
3576-
Axis along which the elements are counted. By default, give
3576+
axis : None or int or tuple of ints, optional
3577+
Axis or axes along which the elements are counted. By default, give
35773578
the total number of elements.
35783579
35793580
Returns
@@ -3604,6 +3605,12 @@ def size(a, axis=None):
36043605
return a.size
36053606
except AttributeError:
36063607
return asarray(a).size
3608+
elif isinstance(axis, tuple):
3609+
try:
3610+
shape = a.shape
3611+
except AttributeError:
3612+
shape = asarray(a).shape
3613+
return math.prod(shape[ax] for ax in axis)
36073614
else:
36083615
try:
36093616
return a.shape[axis]

numpy/_core/tests/test_numeric.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@ def test_size(self):
292292
assert_(np.size(A) == 6)
293293
assert_(np.size(A, 0) == 2)
294294
assert_(np.size(A, 1) == 3)
295+
assert_(np.size(A, () == 1))
296+
assert_(np.size(A, (0,) == 2))
297+
assert_(np.size(A, (1,) == 3))
298+
assert_(np.size(A, (0, 1) == 6))
295299

296300
def test_squeeze(self):
297301
A = [[[1, 1, 1], [2, 2, 2], [3, 3, 3]]]

0 commit comments

Comments
 (0)
0