mindspore.ops.combinations

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

Return 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 input tensor.

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

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

Returns

Tensor

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> input = mindspore.tensor([1, 2, 3])
>>> mindspore.ops.combinations(input)
Tensor(shape=[3, 2], dtype=Int64, value=
[[1, 2],
 [1, 3],
 [2, 3]])
>>> mindspore.ops.combinations(input, r=3)
Tensor(shape=[1, 3], dtype=Int64, value=
[[1, 2, 3]])
>>> mindspore.ops.combinations(input, with_replacement=True)
Tensor(shape=[6, 2], dtype=Int64, value=
[[1, 1],
 [1, 2],
 [1, 3],
 [2, 2],
 [2, 3],
 [3, 3]])
>>> # It has the same results as using itertools.combinations.
>>> import itertools
>>> input = [1, 2, 3]
>>> list(itertools.combinations(input, r=2))
[(1, 2), (1, 3), (2, 3)]
>>> list(itertools.combinations(input, r=3))
[(1, 2, 3)]
>>> list(itertools.combinations_with_replacement(input, r=2))
[(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)]