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 reinstallOutdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchUse scipy.stats.mode to find the most frequent value in a numeric array. It returns both the mode and its count; set axis to choose whether to calculate per slice or across the whole array, and set nan_policy to decide how missing values are handled.
Find the mode of a one-dimensional array
Import NumPy and SciPy, pass the array to stats.mode, then read the .mode and .count fields from the result. The current SciPy API documents the signature as scipy.stats.mode(a, axis=0, nan_policy='propagate', keepdims=False).
As an Amazon Associate I earn from qualifying purchases.
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 appears three times, more than any other value. The result exposes the most frequent value as mode and its frequency as count. See the SciPy 1.18.0 scipy.stats.mode reference for the current API details.
Recommended Free Tools
Choose the axis for a multidimensional array
With the default axis=0, SciPy calculates a result independently for each slice along the first axis. For a two-dimensional array, that means one result per column. This example removes the reduced axis in the output:
#1 Best Overall
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 1 2 1]
The first column contains four instances of 3, so its count is 4. A column can have a mode that occurs only once when every value is unique; in that case the selected value still has count 1.
Calculate one mode across the whole array
Pass axis=None to flatten the input before calculating a single result. For the array above, the output is scalar-shaped when keepdims=False:
Rank #2
result = stats.mode(a, axis=None, keepdims=False)
print(result.mode)
print(result.count)
Use axis=0 when each column is a separate unit of analysis, or axis=None when every array element belongs to one group. The resulting shape differs because the first choice produces one value per column, while the second produces one overall value.
Free tools Windows power users keep installed
One-click scans. No signup required.
Choose whether to keep reduced dimensions
keepdims=False removes the reduced axes, which is the current default. Set keepdims=True to retain those axes at length one, which can make the result easier to broadcast against the original data in later NumPy operations.
This default changed in SciPy 1.11.0. SciPy 1.10.1 documented the earlier legacy behavior as keepdims=None; current SciPy documents keepdims=False. Older examples may therefore show outputs with extra length-one dimensions. Specify keepdims=True or keepdims=False explicitly when output shape matters or code must behave consistently across versions. The transition is described in the SciPy 1.10.1 API reference.
Set a NaN policy deliberately
The default is nan_policy='propagate'. Choose among the documented options according to what missing values mean in your data:
'propagate': if a slice contains a NaN, that slice’s output mode 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 stats.mode(a, axis=0, nan_policy='omit') only when treating NaNs as missing observations is appropriate; omission changes which values contribute to the frequency count.
Understand ties and numeric input
If multiple values share the highest frequency, SciPy returns one mode, not every tied value. The current reference does not promise a universal tie-breaking rule, so do not rely on a particular tied value without checking behavior for the exact SciPy version in use. The SciPy 1.8.1 manual described returning the smallest tied value, but that wording is not a guarantee in the current reference.
Best Value
The current API is for numeric arrays. Support for non-numeric arrays was deprecated in SciPy 1.9.0 and removed in SciPy 1.11.0; the older documentation pointed readers working with categorical DataFrame columns to pandas.DataFrame.mode. See the SciPy 1.8.1 reference for the historical tie wording and the SciPy 1.10.1 reference for the deprecation and removal timeline.
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.




