median

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

Compute the median along the specified axis.

Parameters
  • x (Tensor) – The input Tensor, it’s data type can be bool, 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.

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

Returns

Tensor, results of median along axis of x. If data type of x is float64, data type of results will be float64, otherwise data type will be float32.

Examples

>>> import paddle

>>> x = paddle.arange(12).reshape([3, 4])
>>> print(x)
Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
[[0 , 1 , 2 , 3 ],
 [4 , 5 , 6 , 7 ],
 [8 , 9 , 10, 11]])

>>> y1 = paddle.median(x)
>>> print(y1)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
5.50000000)

>>> y2 = paddle.median(x, axis=0)
>>> print(y2)
Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[4., 5., 6., 7.])

>>> y3 = paddle.median(x, axis=1)
>>> print(y3)
Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
[1.50000000, 5.50000000, 9.50000000])

>>> y4 = paddle.median(x, axis=0, keepdim=True)
>>> print(y4)
Tensor(shape=[1, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
[[4., 5., 6., 7.]])