mindspore.ops.median

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

Return the median(s) and indice(s) of the tensor along the specified axis.

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) – The input tensor.

  • axis (int, optional) – Specify the axis for computation. Default -1 .

  • keepdims (bool, optional) – Whether the output tensor has dim retained. Default False .

Returns

Tuple(median, median_indices) of 2 tensors.

Supported Platforms:

GPU CPU

Examples

>>> import mindspore
>>> input = mindspore.tensor([[9, 3, 4, 5],
...                           [5, 2, 7, 4],
...                           [8, 1, 3, 6]])
>>> # case 1: By default, compute the median along axis -1.
>>> mindspore.ops.median(input)
(Tensor(shape=[3], dtype=Int64, value= [4, 4, 3]),
 Tensor(shape=[3], dtype=Int64, value= [2, 3, 2]))
>>>
>>> # case 2: Compute the median along axis 0.
>>> mindspore.ops.median(input, axis=0)
(Tensor(shape=[4], dtype=Int64, value= [8, 2, 4, 5]),
 Tensor(shape=[4], dtype=Int64, value= [2, 1, 0, 0]))
>>>
>>> # case 3: If keepdims=True, the output shape will be same of that of the input.
>>> mindspore.ops.median(input, axis=0, keepdims=True)
(Tensor(shape=[1, 4], dtype=Int64, value=
 [[8, 2, 4, 5]]),
 Tensor(shape=[1, 4], dtype=Int64, value=
 [[2, 1, 0, 0]]))