nanmean

paddle. nanmean ( x: Tensor, axis: int | Sequence[int] | None = None, keepdim: bool = False, name: str | None = None ) Tensor [source]

Compute the arithmetic mean along the specified axis, ignoring NaNs.

Parameters
  • x (Tensor) – The input Tensor with data type uint16, float16, float32, float64.

  • axis (int|list|tuple, optional) – The axis along which to perform nanmean calculations. axis should be int, list(int) or tuple(int). If axis is a list/tuple of dimension(s), nanmean is calculated along all element(s) of axis . axis or element(s) of axis should be in range [-D, D), where D is the dimensions of x . If axis or element(s) of axis is less than 0, it works the same way as \(axis + D\) . If axis is None, nanmean 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|None, optional) – Name for the operation (optional, default is None). For more information, please refer to api_guide_Name.

Returns

Tensor, results of arithmetic mean along axis of x, with the same data type as x.

Examples

>>> import paddle
>>> # x is a 2-D Tensor:
>>> x = paddle.to_tensor([[float('nan'), 0.3, 0.5, 0.9],
...                       [0.1, 0.2, float('-nan'), 0.7]])
>>> out1 = paddle.nanmean(x)
>>> out1
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
0.44999996)
>>> out2 = paddle.nanmean(x, axis=0)
>>> out2
Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.10000000, 0.25000000, 0.50000000, 0.79999995])
>>> out3 = paddle.nanmean(x, axis=0, keepdim=True)
>>> out3
Tensor(shape=[1, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0.10000000, 0.25000000, 0.50000000, 0.79999995]])
>>> out4 = paddle.nanmean(x, axis=1)
>>> out4
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.56666666, 0.33333334])
>>> out5 = paddle.nanmean(x, axis=1, keepdim=True)
>>> out5
Tensor(shape=[2, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0.56666666],
 [0.33333334]])

>>> # y is a 3-D Tensor:
>>> y = paddle.to_tensor([[[1, float('nan')], [3, 4]],
...                       [[5, 6], [float('-nan'), 8]]])
>>> out6 = paddle.nanmean(y, axis=[1, 2])
>>> out6
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[2.66666675, 6.33333349])
>>> out7 = paddle.nanmean(y, axis=[0, 1])
>>> out7
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[3., 6.])