{ } Raw JSON

bundles / scipy latest / scipy / ndimage / _measurements / median

function

scipy.ndimage._measurements:median

source: /scipy/ndimage/_measurements.py :1180

Signature

def   median ( input labels = None index = None )

Summary

Calculate the median of the values of an array over labeled regions.

Parameters

input : array_like

Array_like of values. For each region specified by labels, the median value of input over the region is computed.

labels : array_like, optional

An array_like of integers marking different regions over which the median value of input is to be computed. labels must have the same shape as input. If labels is not specified, the median over the whole array is returned.

index : array_like, optional

A list of region labels that are taken into account for computing the medians. If index is None, the median over all elements where labels is non-zero is returned.

Returns

median : float or list of floats

List of medians of input over the regions determined by labels and whose index is in index. If index or labels are not specified, a float is returned: the median value of input if labels is None, and the median value of elements where labels is greater than zero if index is None.

Notes

The function returns a Python list and not a NumPy array, use np.array to convert the list to an array.

Array API Standard Support

median has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.

====================  ====================  ====================
Library               CPU                   GPU
====================  ====================  ====================
NumPy                 ✅                     n/a                 
CuPy                  n/a                   ✅                   
PyTorch               ✅                     ⛔                   
JAX                   ⚠️ no JIT
Dask                  ⚠️ computes graph     n/a                 
====================  ====================  ====================

See dev-arrayapi for more information.

Examples

from scipy import ndimage
import numpy as np
a = np.array([[1, 2, 0, 1],
              [5, 3, 0, 4],
              [0, 0, 0, 7],
              [9, 3, 0, 0]])
labels, labels_nb = ndimage.label(a)
labels
ndimage.median(a, labels=labels, index=np.arange(1, labels_nb + 1))
ndimage.median(a)
ndimage.median(a, labels=labels)

See also

extrema
label
maximum
mean
minimum
standard_deviation
sum
variance

Aliases

  • scipy.ndimage.median