# median¶

paddle. median ( x, axis=None, keepdim=False, mode='avg', name=None ) [source]

Compute the median along the specified axis.

Parameters
• x (Tensor) – The input Tensor, it’s data type can be float16, float32, float64, int32, int64.

• axis (int, optional) – The axis along which to perform median calculations `axis` should be int. `axis` should be in range [-D, D), where D is the dimensions of `x` . If `axis` is less than 0, it works the same way as \(axis + D\). If `axis` is None, median is calculated over all elements of `x`. Default is None.

• keepdim (bool, optional) – Whether to reserve the reduced dimension(s) in the output Tensor. If `keepdim` is True, the dimensions of the output Tensor is the same as `x` except in the reduced dimensions(it is of size 1 in this case). Otherwise, the shape of the output Tensor is squeezed in `axis` . Default is False.

• mode (str, optional) – Whether to use mean or min operation to calculate the median values when the input tensor has an even number of elements in the dimension `axis`. Support ‘avg’ and ‘min’. Default is ‘avg’.

• name (str, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.

Returns

Tensor or tuple of Tensor. If `mode` == ‘avg’, the result will be the tensor of median values; If `mode` == ‘min’ and `axis` is None, the result will be the tensor of median values; If `mode` == ‘min’ and `axis` is not None, the result will be a tuple of two tensors containing median values and their indices.

When `mode` == ‘avg’, if data type of `x` is float64, data type of median values will be float64, otherwise data type of median values will be float32. When `mode` == ‘min’, the data type of median values will be the same as `x`. The data type of indices will be int64.

Examples

```>>> import paddle
>>> import numpy as np

>>> print(x)
[[0 , 1 , 2 , 3 ],
[4 , 5 , 6 , 7 ],
[8 , 9 , 10, 11]])

>>> print(y1)
5.50000000)

>>> print(y2)
[4., 5., 6., 7.])

>>> print(y3)
[1.50000000, 5.50000000, 9.50000000])

>>> y4 = paddle.median(x, axis=0, keepdim=True)
>>> print(y4)
[[4., 5., 6., 7.]])

>>> print(y5)
5)

>>> median_value, median_indices = paddle.median(x, axis=1, mode='min')
>>> print(median_value)
[1, 5, 9])
>>> print(median_indices)
[1, 1, 1])

>>> # cases containing nan values

>>> y6 = paddle.median(x, axis=-1, keepdim=True)
>>> print(y6)
[[nan       ],
[2.50000000],
[nan       ]])

>>> median_value, median_indices = paddle.median(x, axis=1, keepdim=True, mode='min')
>>> print(median_value)