省交通运输厅举办学习贯彻领导干部报告个人有关事...

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 bfloat16, float16, float32, float64, int32, int64.

  • axis (int|None, 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|None, 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

>>> 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.]])

>>> y5 = paddle.median(x, mode='min')
>>> print(y5)
Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True,
5)

>>> median_value, median_indices = paddle.median(x, axis=1, mode='min')
>>> print(median_value)
Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True,
[1, 5, 9])
>>> print(median_indices)
Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True,
[1, 1, 1])

>>> # cases containing nan values
>>> x = paddle.to_tensor(np.array([[1,float('nan'),3,float('nan')],[1,2,3,4],[float('nan'),1,2,3]]))

>>> y6 = paddle.median(x, axis=-1, keepdim=True)
>>> print(y6)
Tensor(shape=[3, 1], dtype=float64, place=Place(cpu), stop_gradient=True,
[[nan       ],
 [2.50000000],
 [nan       ]])

>>> median_value, median_indices = paddle.median(x, axis=1, keepdim=True, mode='min')
>>> print(median_value)
Tensor(shape=[3, 1], dtype=float64, place=Place(cpu), stop_gradient=True,
[[nan],
 [2. ],
 [nan]])
>>> print(median_indices)
Tensor(shape=[3, 1], dtype=int64, place=Place(cpu), stop_gradient=True,
[[1],
 [1],
 [0]])