larray.LArray.median

LArray.median(*axes_and_groups, out=None, skipna=None, keepaxes=False, **explicit_axes)[source]

Computes the arithmetic median.

Parameters
*axes_and_groupsNone or int or str or Axis or Group or any combination of those

Axis(es) or group(s) along which the median is performed. The default (no axis or group) is to perform the median over all the dimensions of the input array.

An axis can be referred by:

  • its index (integer). Index can be a negative integer, in which case it counts from the last to the first axis.

  • its name (str or AxisReference). You can use either a simple string (‘axis_name’) or the special variable X (X.axis_name).

  • a variable (Axis). If the axis has been defined previously and assigned to a variable, you can pass it as argument.

You may not want to perform the median over a whole axis but over a selection of specific labels. To do so, you have several possibilities:

  • ([‘a1’, ‘a3’, ‘a5’], ‘b1, b3, b5’) : labels separated by commas in a list or a string

  • (‘a1:a5:2’) : select labels using a slice (general syntax is ‘start:end:step’ where is ‘step’ is optional and 1 by default).

  • (a=’a1, a2, a3’, X.b[‘b1, b2, b3’]) : in case of possible ambiguity, i.e. if labels can belong to more than one axis, you must precise the axis.

  • (‘a1:a3; a5:a7’, b=’b0,b2; b1,b3’) : create several groups with semicolons. Names are simply given by the concatenation of labels (here: ‘a1,a2,a3’, ‘a5,a6,a7’, ‘b0,b2’ and ‘b1,b3’)

  • (‘a1:a3 >> a123’, ‘b[b0,b2] >> b12’) : operator ‘ >> ‘ allows to rename groups.

outLArray, optional

Alternate output array in which to place the result. It must have the same shape as the expected output and its type is preserved (e.g., if dtype(out) is float, the result will consist of 0.0’s and 1.0’s). Axes and labels can be different, only the shape matters. Defaults to None (create a new array).

skipnabool, optional

Whether or not to skip NaN (null) values. If False, resulting cells will be NaN if any of the aggregated cells is NaN. Defaults to True.

keepaxesbool or label-like, optional

Whether or not reduced axes are left in the result as dimensions with size one. If True, reduced axes will contain a unique label representing the applied aggregation (e.g. ‘sum’, ‘prod’, …). It is possible to override this label by passing a specific value (e.g. keepaxes=’summation’). Defaults to False.

Returns
LArray or scalar

Examples

>>> arr = ndtest((4, 4))
>>> arr[:,:] = [[10, 7, 5, 9],                         [5, 8, 3, 7],                         [6, 2, 0, 9],                         [9, 10, 5, 6]]
>>> arr
a\b  b0  b1  b2  b3
 a0  10   7   5   9
 a1   5   8   3   7
 a2   6   2   0   9
 a3   9  10   5   6
>>> arr.median()
6.5
>>> # along axis 'a'
>>> arr.median('a')
b   b0   b1   b2   b3
   7.5  7.5  4.0  8.0
>>> # along axis 'b'
>>> arr.median('b')
a   a0   a1   a2   a3
   8.0  6.0  4.0  7.5

Select some rows only

>>> arr.median(['a0', 'a1'])
b   b0   b1   b2   b3
   7.5  7.5  4.0  8.0
>>> # or equivalently
>>> # arr.median('a0,a1')

Split an axis in several parts

>>> arr.median((['a0', 'a1'], ['a2', 'a3']))
  a\b   b0   b1   b2   b3
a0,a1  7.5  7.5  4.0  8.0
a2,a3  7.5  6.0  2.5  7.5
>>> # or equivalently
>>> # arr.median('a0,a1;a2,a3')

Same with renaming

>>> arr.median((X.a['a0', 'a1'] >> 'a01', X.a['a2', 'a3'] >> 'a23'))
a\b   b0   b1   b2   b3
a01  7.5  7.5  4.0  8.0
a23  7.5  6.0  2.5  7.5
>>> # or equivalently
>>> # arr.median('a0,a1>>a01;a2,a3>>a23')