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.
axisshould be int, list(int) or tuple(int). Ifaxisis a list/tuple of dimension(s), nanmean is calculated along all element(s) ofaxis.axisor element(s) ofaxisshould be in range [-D, D), where D is the dimensions ofx. Ifaxisor element(s) ofaxisis less than 0, it works the same way as \(axis + D\) . Ifaxisis None, nanmean is calculated over all elements ofx. Default is None.keepdim (bool, optional) – Whether to reserve the reduced dimension(s) in the output Tensor. If
keepdimis True, the dimensions of the output Tensor is the same asxexcept 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.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
axisofx, with the same data type asx.
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.])
