median
- paddle. median ( x, axis=None, keepdim=False, mode='avg', name=None ) [source]
-
Compute the median along the specified axis.
Note
Alias Support: The parameter name
input
can be used as an alias forx
, anddim
can be used as an alias foraxis
. When an alias replacement occurs, the default parameter for mode setting is min instead of avg. For example,median(input=tensor_x, dim=1, ...)
is equivalent tomedian(x=tensor_x, axis=1, ...)
.- Parameters
-
x (Tensor) – The input Tensor, it’s data type can be bfloat16, float16, float32, float64, int32, int64. alias:
input
.axis (int|None, optional) – The axis along which to perform median calculations
axis
should be int. alias:dim
.axis
should be in range [-D, D), where D is the dimensions ofx
. Ifaxis
is less than 0, it works the same way as \(axis + D\). Ifaxis
is None, median is calculated over all elements ofx
. 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 asx
except in the reduced dimensions(it is of size 1 in this case). Otherwise, the shape of the output Tensor is squeezed inaxis
. 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’. When an alias replacement occurs, the default parameter for mode setting is min instead of avg.name (str|None, optional) – Name for the operation (optional, default is None). For more information, please refer to api_guide_Name.
- Returns
-
Tensor or tuple of Tensor. If
mode
== ‘avg’, the result will be the tensor of median values; Ifmode
== ‘min’ andaxis
is None, the result will be the tensor of median values; Ifmode
== ‘min’ andaxis
is not None, the result will be a tuple of two tensors containing median values and their indices.When
mode
== ‘avg’, if data type ofx
is float64, data type of median values will be float64, otherwise data type of median values will be float32. Whenmode
== ‘min’, the data type of median values will be the same asx
. 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]])