mindspore.ops.combinations

View Source On Gitee
mindspore.ops.combinations(input, r=2, with_replacement=False)[source]

Returns all r-length subsequences of input Tensor.

When with_replacement is set to False, it works similar to Python's itertools.combinations, and when with_replacement is set to True, it behaves like itertools.combinations_with_replacement.

Parameters
  • input (Tensor) – One-dimensional tensors.

  • r (int, optional) – Number of elements to perform combination. Default: 2 .

  • with_replacement (bool, optional) – Allow duplication or not. Default: False .

Returns

Tensor, contains all possible combinations of elements sampled from input Tensor.

Raises
Supported Platforms:

Ascend GPU CPU

Examples

>>> from mindspore import Tensor, ops
>>> input = Tensor([1, 3, -1, 0, 4])
>>> output = ops.combinations(input)
>>> print(output.asnumpy())
[[ 1  3]
 [ 1 -1]
 [ 1  0]
 [ 1  4]
 [ 3 -1]
 [ 3  0]
 [ 3  4]
 [-1  0]
 [-1  4]
 [ 0  4]]