nanmedian¶
- paddle. nanmedian ( x, axis=None, keepdim=False, mode='avg', name=None ) [source]
-
Compute the median along the specified axis, while ignoring NaNs.
If the valid count of elements is a even number, the average value of both elements in the middle is calculated as the median.
- Parameters
-
x (Tensor) – The input Tensor, it’s data type can be int32, int64, float16, bfloat16, float32, float64.
axis (None|int|list|tuple, optional) – The axis along which to perform median calculations
axis
should be int or list of int.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 nanmedian values when the input tensor has an even number of non-NaN elements along 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
== ‘min’ andaxis
is int, the result will be a tuple of two tensors (nanmedian value and nanmedian index). Otherwise, only nanmedian value will be returned.
Examples
>>> import paddle >>> x = paddle.to_tensor([[float('nan'), 2. , 3. ], [0. , 1. , 2. ]]) >>> y1 = x.nanmedian() >>> print(y1.numpy()) 2.0 >>> y2 = x.nanmedian(0) >>> print(y2.numpy()) [0. 1.5 2.5] >>> y3 = x.nanmedian(0, keepdim=True) >>> print(y3.numpy()) [[0. 1.5 2.5]] >>> y4 = x.nanmedian((0, 1)) >>> print(y4.numpy()) 2.0 >>> y5 = x.nanmedian(mode='min') >>> print(y5.numpy()) 2.0 >>> y6, y6_index = x.nanmedian(0, mode='min') >>> print(y6.numpy()) [0. 1. 2.] >>> print(y6_index.numpy()) [1 1 1] >>> y7, y7_index = x.nanmedian(1, mode='min') >>> print(y7.numpy()) [2. 1.] >>> print(y7_index.numpy()) [1 1] >>> y8 = x.nanmedian((0,1), mode='min') >>> print(y8.numpy()) 2.0