Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

stats.sem

Computes the standard error of the mean for an array, either overall or along a chosen axis. It supports degrees-of-freedom adjustment, NaN policies, and dimension preservation.

How it works

The function computes the standard error of the mean for an array or for each slice along a selected axis. It can flatten the input, adjust the variance degrees of freedom, control NaN handling, and optionally retain reduced dimensions.

Syntax

scipy.stats.sem(a, axis=0, ddof=1, nan_policy='propagate', *, keepdims=False)

Parameters

NameTypeDescription
aarray_likeArray-like values for which to calculate the standard error.
axisint or NoneAxis along which to calculate the statistic; None ravels the input. Default: 0.
ddofintDegrees-of-freedom adjustment for the variance. Default: 1.
nan_policy{'propagate', 'raise', 'omit'}NaN handling policy: 'propagate', 'raise', or 'omit'. Default: 'propagate'.
keepdimsboolWhether reduced axes are retained with size one. Default: False.

Returns

ndarray or float: the standard error of the mean in the sample(s), along the input axis.

Key points

  • sem measures uncertainty in a sample mean rather than returning the mean itself.
  • The primary input a may be an array-like collection of values.
  • Passing axis=None computes a single statistic over all input elements.
  • For an integer axis, the output contains one SEM for each slice along that axis.
  • ddof controls the degrees-of-freedom adjustment used for the variance underlying the SEM.
  • nan_policy determines whether NaNs propagate, raise an exception, or are omitted.
  • The return value is an ndarray or float representing the SEM along the selected axis.

Common mistakes

  • The default ddof is 1; using ddof=0 changes the degrees-of-freedom adjustment.
  • With the default nan_policy='propagate', NaN values cause the corresponding result to be NaN.
  • nan_policy='omit' ignores NaNs, but slices with insufficient non-NaN data can still produce an undefined result.

Example: Compute standard errors

import numpy as np
from scipy import stats

a = np.arange(20).reshape(5, 4)
print(stats.sem(a))
print(stats.sem(a, axis=None, ddof=0))

Computes column-wise standard errors, then computes one standard error over all values with population-style degrees of freedom.

When to use it

Measurement analysis

Estimate uncertainty for means computed from repeated measurements.

Axis-wise summaries

Compute one SEM per row or column in a multidimensional dataset.

NaN-aware processing

Handle missing observations explicitly during statistical aggregation.

Interview questions

QWhat does scipy.stats.sem compute, and what are its key defaults?

scipy.stats.sem calculates the standard error of the mean for the input values. By default it reduces along axis 0, uses ddof=1, and propagates NaNs.

Follow-up: How do you calculate one SEM for the entire array? Use axis=None, which ravels the input before computing the statistic.

QHow do axis, ddof, and keepdims affect the result?

Set axis=None to compute across the whole input after flattening it. Set ddof=0 when the variance calculation should use no degrees-of-freedom subtraction.

Follow-up: How would you preserve reduced axes? Set keepdims=True, so the output retains dimensions with length one.

QHow does nan_policy handle missing values?

nan_policy accepts 'propagate', 'raise', or 'omit'. These respectively return NaNs, raise an error, or ignore NaNs during calculation.

Follow-up: Which option is appropriate when missing values should not silently affect the result? Use 'raise' to fail explicitly when NaNs are present.

  • scipy.stats.tmean
  • scipy.stats.tstd
  • scipy.stats.describe

Source: sem. Checked 2026-10-15.