mindspore.ops.median

mindspore.ops.median(input, axis=- 1, keepdims=False)[source]

Computes the median and indices of input tensor.

Warning

  • indices does not necessarily contain the first occurrence of each median value found in the input, unless it is unique. The specific implementation of this API is device-specific. The results may be different on CPU and GPU.

Parameters
  • input (Tensor) – A Tensor of any dimension whose data type is int16, int32, int64, float32 or float64.

  • axis (int, optional) – The dimension need to reduce. Default: -1 .

  • keepdims (bool, optional) – Whether the output tensor need to retain axis dimension or not. Default: False .

Returns

y (Tensor), has the same dtype as the input. If keepdims is true, the y has the same shape as the input except the shape of y in dimension axis is size 1. Otherwise, the y lacks axis dimension than input.

indices (Tensor), has the same shape as the y, but dtype is int64.

Raises
  • TypeError – If dtype of input is not one of the following: int16, int32, int64, float32, float64.

  • TypeError – If input input is not a Tensor.

  • TypeError – If axis is not a int.

  • TypeError – If keepdims is not a bool.

  • ValueError – If axis is not in range of [-x.dim, x.dim-1].

Supported Platforms:

GPU CPU

Examples

>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> x = Tensor(np.array([[0.57, 0.11, 0.21],[0.38, 0.50, 0.57], [0.36, 0.16, 0.44]]).astype(np.float32))
>>> y = ops.median(x, axis=0, keepdims=False)
>>> print(y)
(Tensor(shape=[3], dtype=Float32, value= [ 3.79999995e-01,  1.59999996e-01,  4.39999998e-01]),
Tensor(shape=[3], dtype=Int64, value= [1, 2, 2]))