nanmedian
- paddle.compat. nanmedian ( input: Tensor, dim: int | None = None, keepdim: bool = False, *, out: tuple[Tensor, Tensor] | Tensor | None = None ) Tensor | MedianRetType [source]
-
Returns the median of the values in input, ignoring NaN values.
- Parameters
-
input (Tensor) – The input tensor.
dim (int|None, optional) – The dimension to reduce. If None, computes the nanmedian over all elements. Default is None.
keepdim (bool, optional) – Whether the output tensor has dim retained or not. Default is False.
out (Tensor|tuple[Tensor, Tensor], optional) – If provided, the result will be written into this tensor. For global nanmedian (dim=None), out must be a single tensor. For nanmedian along a dimension (dim specified, including dim=-1), out must be a tuple of two tensors (values, indices).
- Returns
-
The median values, ignoring NaN. If dim is None, returns a single tensor. If dim is specified (including dim=-1), returns a named tuple MedianRetType(values: Tensor, indices: Tensor).
- Return type
-
Tensor|MedianRetType
Examples
>>> import paddle >>> import numpy as np >>> x = paddle.to_tensor([[1, float('nan'), 3], [4, 5, 6], [float('nan'), 8, 9]], dtype='float32') >>> result = paddle.compat.nanmedian(x) >>> print(result) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, 5.0) >>> ret = paddle.compat.nanmedian(x, dim=1) >>> print(ret.values) Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, [1.0, 5.0, 8.0]) >>> print(ret.indices) Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True, [0, 1, 1]) >>> # Using out parameter >>> out_values = paddle.zeros([3], dtype='float32') >>> out_indices = paddle.zeros([3], dtype='int64') >>> paddle.compat.nanmedian(x, dim=1, out=(out_values, out_indices)) >>> print(out_values) Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, [1.0, 5.0, 8.0])