October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

SciPy Stats Mode in Python: Examples for Arrays, Axes, and NaNs

A practical guide to scipy.stats.mode: find a mode and count, calculate per-column or whole-array results, and handle dimensions, NaNs, ties, and version changes.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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:

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:

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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': raise ValueError if 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.