mindspore.ops.median

查看源文件
mindspore.ops.median(input, axis=- 1, keepdims=False)[源代码]

返回tensor在指定轴上的中位数及其索引。

警告

  • 如果 input 的中值不唯一,则 indices 不一定对应第一个出现的中值。该接口的具体实现方式和后端类型相关,CPU和GPU的返回值可能不相同。

参数:
  • input (Tensor) - 输入tensor。

  • axis (int,可选) - 指定计算的轴。默认 -1

  • keepdims (bool,可选) - 输出tensor是否保留维度。默认 False

返回:

两个tensor组成的tuple(median, median_indices)

支持平台:

GPU CPU

样例:

>>> 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]]))