iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
Use scipy.stats.mode to find the most frequent value in a numeric array. It returns both that value and its frequency; set axis to choose whether to calculate modes for slices or across the whole array, and set nan_policy to control how missing values are handled.
Get the mode of a one-dimensional array
Import NumPy and SciPy, pass the array to stats.mode, and read the result’s mode and count fields:
import numpy as np
from scipy import stats
values = np.array([2, 1, 2, 3, 2, 1])
result = stats.mode(values)
print(result.mode) # 2
print(result.count) # 3
Here, 2 occurs three times, more than any other value. The current API, documented in SciPy 1.18.0, has the signature scipy.stats.mode(a, axis=0, nan_policy='propagate', keepdims=False). SciPy’s mode reference describes the input, arguments, and return values.
What the result contains
The returned result has two fields: .mode holds the modal value or values for the requested slices, and .count holds the frequency of the returned mode. Their shapes depend on the input dimensions, axis, and keepdims.
#1 Best Overall
If multiple values tie for the highest frequency, SciPy returns one of them, not a list of every tied value. The current reference does not promise a universal tie-breaking rule, so do not rely on which tied value is selected across versions.
Calculate modes along a 2D array axis
By default, axis=0: SciPy computes a result for each slice along the first axis. For a two-dimensional array, that means one mode per column. This example removes the reduced axis from the result:
Rank #2
import numpy as np
from scipy import stats
a = np.array([[3, 0, 3, 7],
[3, 2, 6, 2],
[1, 7, 2, 8],
[3, 0, 6, 1],
[3, 2, 5, 5]])
result = stats.mode(a, axis=0, keepdims=False)
print(result.mode) # [3 0 6 1]
print(result.count) # [4 2 2 1]
The first column is [3, 3, 1, 3, 3], so its mode is 3 and its count is four. The second column has two occurrences of 0; the remaining columns have modes 6 and 1, with counts two and one.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Choose between per-axis and whole-array results
Use an integer axis for a mode per slice, or axis=None for one mode across all elements. With axis=None, SciPy ravels the input before calculating:
result = stats.mode(a, axis=None, keepdims=False)
print(result.mode) # one overall modal value
print(result.count) # its frequency in the flattened array
Use the whole-array form when every element belongs to one group. Use an axis when each row, column, or other slice should be analyzed independently. The exact output shape depends on the chosen axis and the dimension setting.
Control whether reduced dimensions remain
keepdims=False, the current default, removes the axis that was reduced. Set keepdims=True to retain that axis as a dimension of length one. Preserved dimensions can make the result easier to broadcast against the original array; removed dimensions produce a simpler shape for many other operations.
This default changed in SciPy 1.11.0. The SciPy 1.10.1 reference documented legacy behavior for keepdims=None and warned that the default would become False in 1.11.0. Older examples may therefore show extra length-one dimensions. For predictable shape behavior, pass keepdims=True or keepdims=False explicitly. See the SciPy 1.10.1 reference for the transition warning.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Scan for outdated or missing drivers - takes under a minute3Clear out junk files and repair common Windows errorsPick a NaN policy deliberately
The default is nan_policy='propagate'. Choose among the documented options according to what missing values mean in your data:
Best Value
'propagate': if a slice contains a NaN, that slice’s result is NaN.'omit': ignore NaNs when calculating the mode. If too little data remains in a slice, its result is NaN.'raise': raiseValueErrorif a NaN is present.
For example, use omission only when excluding missing entries is appropriate for the analysis:
result = stats.mode(values_with_missing_data, nan_policy='omit')
These policies apply to slices when an axis is specified; with axis=None, the input is treated as one flattened set of values.
Use numeric arrays with current SciPy
The current scipy.stats.mode API specifies numeric input. Support for non-numeric arrays was deprecated in SciPy 1.9.0 and removed in SciPy 1.11.0. The SciPy 1.10.1 documentation directed readers working with categorical DataFrame columns to pandas.DataFrame.mode; see its historical compatibility notes.
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallCrashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteFor a broader introduction to the scipy.stats package, see the SciPy statistics tutorial. The API reference is the place to check details such as edge cases and supported inputs.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

