scipp.median#

scipp.median(x, dim=None)#

Compute the median of the input values.

The median is the middle value of a sorted copy of the input array along each reduced dimension. That is, for an array of N unmasked values, the median is

  • odd N: x[(N-1)/2]

  • even N: (x[N/2-1] + x[N/2]) / 2

Note

Masks are broadcast to the shape of x. This can lead to a large temporary memory usage.

Parameters:
Returns:

Same type as x – The median of the input values.

Raises:

See also

scipp.nanmedian

Ignore NaN’s when calculating the median.

Examples

median is available as a method:

>>> x = sc.array(dims=['x'], values=[2, 5, 1, 8, 4])
>>> x.median()
<scipp.Variable> ()    float64  [dimensionless]  4
>>> x = sc.array(dims=['x'], values=[2, 5, 1, 8])
>>> x.median()
<scipp.Variable> ()    float64  [dimensionless]  3.5

The median can be computed along a given dimension:

>>> x = sc.array(dims=['x', 'y'], values=[[1, 3, 6], [2, 7, 4]])
>>> x.median('y')
<scipp.Variable> (x: 2)    float64  [dimensionless]  [3, 4]

Masked elements are ignored:

>>> x = sc.DataArray(
...     sc.array(dims=['x'], values=[5, 3, 4, 3]),
...     masks={'m': sc.array(dims=['x'], values=[False, True, False, False])}
... )
>>> x.median()
<scipp.DataArray>
Dimensions: Sizes[]
Data:
                            float64  [dimensionless]  ()  4