Skip to content

Commit 725a4eb

Browse files
authored
MAINT: stats: mode: mode is a reduction operation; should consume an axis (scipy#15423)
* MAINT: stats: mode: mode is a reduction operation; should consume a dimension
1 parent a574519 commit 725a4eb

File tree

2 files changed

+37
-27
lines changed

2 files changed

+37
-27
lines changed

scipy/stats/_stats_py.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -440,12 +440,12 @@ def mode(a, axis=0, nan_policy='propagate'):
440440
... [4, 7, 5, 9]])
441441
>>> from scipy import stats
442442
>>> stats.mode(a)
443-
ModeResult(mode=array([[3, 1, 0, 0]]), count=array([[1, 1, 1, 1]]))
443+
ModeResult(mode=array([3, 1, 0, 0]), count=array([1, 1, 1, 1]))
444444
445445
To get mode of whole array, specify ``axis=None``:
446446
447447
>>> stats.mode(a, axis=None)
448-
ModeResult(mode=array([3]), count=array([3]))
448+
ModeResult(mode=3, count=3)
449449
450450
"""
451451
a, axis = _chk_asarray(a, axis)
@@ -462,18 +462,18 @@ def mode(a, axis=0, nan_policy='propagate'):
462462
# Fall back to a slower method since np.unique does not work with NaN
463463
scores = set(np.ravel(a)) # get ALL unique values
464464
testshape = list(a.shape)
465-
testshape[axis] = 1
465+
testshape.pop(axis)
466466
oldmostfreq = np.zeros(testshape, dtype=a.dtype)
467467
oldcounts = np.zeros(testshape, dtype=int)
468468

469469
for score in scores:
470470
template = (a == score)
471-
counts = np.sum(template, axis, keepdims=True)
471+
counts = np.sum(template, axis)
472472
mostfrequent = np.where(counts > oldcounts, score, oldmostfreq)
473473
oldcounts = np.maximum(counts, oldcounts)
474474
oldmostfreq = mostfrequent
475475

476-
return ModeResult(mostfrequent, oldcounts)
476+
return ModeResult(mostfrequent[()], oldcounts[()])
477477

478478
def _mode1D(a):
479479
vals, cnts = np.unique(a, return_counts=True)
@@ -490,9 +490,8 @@ def _mode1D(a):
490490
counts = np.empty(a_view.shape[:-1], dtype=np.int_)
491491
for ind in inds:
492492
modes[ind], counts[ind] = _mode1D(a_view[ind])
493-
newshape = list(a.shape)
494-
newshape[axis] = 1
495-
return ModeResult(modes.reshape(newshape), counts.reshape(newshape))
493+
494+
return ModeResult(modes[()], counts[()])
496495

497496

498497
def _mask_to_limits(a, limits, inclusive):

scipy/stats/tests/test_stats.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,8 +2144,8 @@ def test_scalar(self):
21442144
def test_basic(self):
21452145
data1 = [3, 5, 1, 10, 23, 3, 2, 6, 8, 6, 10, 6]
21462146
vals = stats.mode(data1)
2147-
assert_equal(vals[0][0], 6)
2148-
assert_equal(vals[1][0], 3)
2147+
assert_equal(vals[0], 6)
2148+
assert_equal(vals[1], 3)
21492149

21502150
def test_axes(self):
21512151
data1 = [10, 10, 30, 40]
@@ -2156,16 +2156,16 @@ def test_axes(self):
21562156
arr = np.array([data1, data2, data3, data4, data5])
21572157

21582158
vals = stats.mode(arr, axis=None)
2159-
assert_equal(vals[0], np.array([30]))
2160-
assert_equal(vals[1], np.array([8]))
2159+
assert_equal(vals[0], np.array(30))
2160+
assert_equal(vals[1], np.array(8))
21612161

21622162
vals = stats.mode(arr, axis=0)
2163-
assert_equal(vals[0], np.array([[10, 10, 30, 30]]))
2164-
assert_equal(vals[1], np.array([[2, 3, 3, 2]]))
2163+
assert_equal(vals[0], np.array([10, 10, 30, 30]))
2164+
assert_equal(vals[1], np.array([2, 3, 3, 2]))
21652165

21662166
vals = stats.mode(arr, axis=1)
2167-
assert_equal(vals[0], np.array([[10], [10], [20], [30], [30]]))
2168-
assert_equal(vals[1], np.array([[2], [4], [3], [4], [3]]))
2167+
assert_equal(vals[0], np.array([10, 10, 20, 30, 30]))
2168+
assert_equal(vals[1], np.array([2, 4, 3, 4, 3]))
21692169

21702170
@pytest.mark.parametrize('axis', np.arange(-4, 0))
21712171
def test_negative_axes_gh_15375(self, axis):
@@ -2178,16 +2178,16 @@ def test_negative_axes_gh_15375(self, axis):
21782178
def test_strings(self):
21792179
data1 = ['rain', 'showers', 'showers']
21802180
vals = stats.mode(data1)
2181-
assert_equal(vals[0][0], 'showers')
2182-
assert_equal(vals[1][0], 2)
2181+
assert_equal(vals[0], 'showers')
2182+
assert_equal(vals[1], 2)
21832183

21842184
def test_mixed_objects(self):
21852185
objects = [10, True, np.nan, 'hello', 10]
21862186
arr = np.empty((5,), dtype=object)
21872187
arr[:] = objects
21882188
vals = stats.mode(arr)
2189-
assert_equal(vals[0][0], 10)
2190-
assert_equal(vals[1][0], 2)
2189+
assert_equal(vals[0], 10)
2190+
assert_equal(vals[1], 2)
21912191

21922192
def test_objects(self):
21932193
# Python objects must be sortable (le + eq) and have ne defined
@@ -2215,8 +2215,8 @@ def __hash__(self):
22152215
assert_equal(np.unique(arr).shape, (4,))
22162216
vals = stats.mode(arr)
22172217

2218-
assert_equal(vals[0][0], Point(2))
2219-
assert_equal(vals[1][0], 4)
2218+
assert_equal(vals[0], Point(2))
2219+
assert_equal(vals[1], 4)
22202220

22212221
def test_mode_result_attributes(self):
22222222
data1 = [3, 5, 1, 10, 23, 3, 2, 6, 8, 6, 10, 6]
@@ -2245,21 +2245,32 @@ def test_mode_nan(self):
22452245
])
22462246
def test_smallest_equal(self, data):
22472247
result = stats.mode(data, nan_policy='omit')
2248-
assert_equal(result[0][0], 1)
2248+
assert_equal(result[0], 1)
22492249

22502250
def test_obj_arrays_ndim(self):
22512251
# regression test for gh-9645: `mode` fails for object arrays w/ndim > 1
22522252
data = [['Oxidation'], ['Oxidation'], ['Polymerization'], ['Reduction']]
22532253
ar = np.array(data, dtype=object)
22542254
m = stats.mode(ar, axis=0)
2255-
assert np.all(m.mode == 'Oxidation') and m.mode.shape == (1, 1)
2256-
assert np.all(m.count == 2) and m.count.shape == (1, 1)
2255+
assert np.all(m.mode == 'Oxidation') and m.mode.shape == (1,)
2256+
assert np.all(m.count == 2) and m.count.shape == (1,)
22572257

22582258
data1 = data + [[np.nan]]
22592259
ar1 = np.array(data1, dtype=object)
22602260
m = stats.mode(ar1, axis=0)
2261-
assert np.all(m.mode == 'Oxidation') and m.mode.shape == (1, 1)
2262-
assert np.all(m.count == 2) and m.count.shape == (1, 1)
2261+
assert np.all(m.mode == 'Oxidation') and m.mode.shape == (1,)
2262+
assert np.all(m.count == 2) and m.count.shape == (1,)
2263+
2264+
@pytest.mark.parametrize('axis', np.arange(-3, 3))
2265+
@pytest.mark.parametrize('dtype', [np.float64, 'object'])
2266+
def test_mode_shape_gh_9955(self, axis, dtype):
2267+
rng = np.random.default_rng(984213899)
2268+
a = rng.uniform(size=(3, 4, 5)).astype(dtype)
2269+
res = stats.mode(a, axis=axis)
2270+
reference_shape = list(a.shape)
2271+
reference_shape.pop(axis)
2272+
np.testing.assert_array_equal(res.mode.shape, reference_shape)
2273+
np.testing.assert_array_equal(res.count.shape, reference_shape)
22632274

22642275

22652276
class TestSEM:

0 commit comments

Comments
 (0)