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

NumPy · Function

np.std

Computes the standard deviation of array elements, measuring distribution spread. It reduces the flattened input by default or operates along specified axes.

How it works

np.std computes the square root of the mean squared deviation from the mean. It reduces all elements by default, or reduces selected axes, and can preserve reduced dimensions with keepdims; where selects the elements included in the calculation.

Syntax

numpy.std(a, axis=None, dtype=None, out=None, ddof=0, keepdims=<no value>, *, where=<no value>, mean=<no value>, correction=<no value>)

Parameters

NameTypeDescription
aarray_likeInput array or array-like values whose standard deviation is calculated.
axisNone, int, or tuple of ints, optionalAxis or tuple of axes to reduce; None reduces the flattened input.
dtypedtype, optionalData type used during calculation.
outndarray, optionalOutput array receiving the result; if supplied, the returned value references it.
ddofint, optionalDelta degrees of freedom subtracted from the number of included elements in the divisor.
keepdimsbool, optionalIf true, reduced axes remain as dimensions of length one.
wherearray_like of bool, optionalBoolean mask or broadcastable condition selecting values included in the calculation.
meanarray_like, optionalPrecomputed mean used by the standard-deviation calculation.
correctionfloat, optionalAlternative name for ddof; cannot be used together with ddof.

Returns

If out is None, a new ndarray containing the standard deviation; otherwise, a reference to the supplied output array.

Key points

  • Use np.std to measure the spread of values along a chosen axis.
  • With axis omitted or None, the input is treated as one flattened array.
  • A tuple passed to axis reduces several dimensions in one operation.
  • Default computation dtype depends on the input: integers promote to float64, while floating inputs keep their dtype.
  • Set keepdims=True to retain reduced axes as size-one dimensions for broadcasting-compatible output.
  • Conceptually, np.std calculates the square root of the mean squared distance from the mean.

Common mistakes

  • Floating-point calculations use the input dtype by default, which can produce less accurate results for low-precision data; choose dtype=float64 when appropriate.
  • The default ddof=0 computes population standard deviation; use ddof=1 for the usual sample correction, provided the selected data contains enough elements.

Example: Compute standard deviations

import numpy as np

values = np.array([[1, 2, 3], [4, 5, 6]])
print(np.std(values))
print(np.std(values, axis=0))
print(np.std(values, ddof=1))

Computes the overall, column-wise, and sample-style standard deviations.

When to use it

Overall dispersion

Summarize variability in a complete numeric array.

Axis-wise analysis

Compare variability independently across rows, columns, or other dimensions.

Broadcasting-aware reduction

Keep dimensions compatible with later broadcasting operations.

Masked statistics

Calculate dispersion over a selected subset using a mask.

Interview questions

QWhat does np.std compute by default?

It returns the standard deviation of array elements, reducing the flattened array when axis=None. The calculation is the square root of the average squared deviation from the mean, and ddof controls the divisor.

Follow-up: How would you compute standard deviation across rows while retaining a two-dimensional result? Use axis=1 and keepdims=True.

QHow does axis affect np.std output?

axis selects one axis, multiple axes as a tuple, or all axes when None. The output shape is reduced along the selected axes unless keepdims=True.

Follow-up: How can selected observations be excluded without copying the array? Pass a boolean condition through where.

QWhat dtype does np.std use?

Integer inputs use float64 by default, while floating inputs retain their dtype unless dtype is supplied. The out argument can receive the result and is returned by reference.

Follow-up: Why might dtype=float64 be useful for float32 input? It can improve accumulation accuracy at the cost of additional computation and storage.

  • numpy.var
  • numpy.mean
  • numpy.nanmean
  • numpy.nanstd
  • numpy.nanvar

Source: numpy.std — NumPy v2.5 Manual. Checked 2026-10-15.