mindspore.numpy.choose
- mindspore.numpy.choose(a, choices, mode='clip')[源代码]
Construct an array from an index array and a list of arrays to choose from. Given an "index" array a of integers and a sequence of n arrays (choices), a and each choice array are first broadcast, as necessary, to arrays of a common shape; calling these Ba and Bchoices[i], i = 0,…,n-1 we have that, necessarily,
Ba.shape == Bchoices[i].shape
for each i. Then, a new array withshape Ba.shape
is created as follows:if
mode='raise'
(the default), then, first of all, each element of a (and thus Ba) must be in the range [0, n-1]; now, suppose that i (in that range) is the value at the (j0, j1, …, jm) position in Ba - then the value at the same position in the new array is the value inBchoices[i]
at that same position;if
mode='wrap'
, values in a (and thus Ba) may be any (signed) integer; modular arithmetic is used to map integers outside the range[0, n-1]
back into that range; and then the new array is constructed as above;if
mode='clip'
, values in a (and thus Ba) may be any (signed) integer; negative integers are mapped to 0; values greater than n-1 are mapped to n-1; and then the new array is constructed as above.
说明
Numpy argument out is not supported.
mode = 'raise'
is not supported, and the default mode is 'clip' instead.- 参数
a (int array) – This array must contain integers in
[0, n-1]
, where n is the number of choices, unlessmode=wrap
ormode=clip
, in which cases any integers are permissible.choices (sequence of arrays) – Choice arrays. a and all of the choices must be broadcastable to the same shape. If choices is itself an array, then its outermost dimension (i.e., the one corresponding to
choices.shape[0]
) is taken as defining the "sequence".mode ('raise', 'wrap', 'clip', optional) –
Specifies how indices outside
[0, n-1]
will be treated:'raise' – raise an error;
'wrap' – wrap around;
'clip' – clip to the range. 'clip' mode means that all indices that are too large are replaced by the index that addresses the last element along that axis. Note that this disables indexing with negative numbers.
- 返回
Tensor, the merged result.
- 异常
ValueError – If a and any of the choices cannot be broadcast.
- Supported Platforms:
Ascend
GPU
CPU
样例
>>> import mindspore.numpy as np >>> choices = [[0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23], [30, 31, 32, 33]] >>> print(np.choose([2, 3, 1, 0], choices)) [20 31 12 3] >>> print(np.choose([2, 4, 1, 0], choices, mode='clip')) [20 31 12 3] >>> print(np.choose([2, 4, 1, 0], choices, mode='wrap')) [20 1 12 3] >>> a = [[1, 0, 1], [0, 1, 0], [1, 0, 1]] >>> choices = [-10, 10] >>> print(np.choose(a, choices)) [[ 10 -10 10] [-10 10 -10] [ 10 -10 10]]