numpy.choose

numpy.choose(a, choices, out=None, mode='raise')

Use an index array to construct a new array from a set of choices.

Given an array of integers and a set of n choice arrays, this function will create a new array that merges each of the choice arrays. Where a value in a is i, then the new array will have the value that choices[i] contains in the same place.

Parameters:

a : int array

This array must contain integers in [0, n-1], where n is the number of choices.

choices : sequence of arrays

Choice arrays. The index array and all of the choices should be broadcastable to the same shape.

out : array, optional

If provided, the result will be inserted into this array. It should be of the appropriate shape and dtype

mode : {‘raise’, ‘wrap’, ‘clip’}, optional

Specifies how out-of-bounds indices will behave:

  • ‘raise’ : raise an error
  • ‘wrap’ : wrap around
  • ‘clip’ : clip to the range
Returns:

merged_array : array

The merged results.

See also

ndarray.choose
equivalent method

Examples

>>> choices = [[0, 1, 2, 3], [10, 11, 12, 13],
...   [20, 21, 22, 23], [30, 31, 32, 33]]
>>> np.choose([2, 3, 1, 0], choices)
array([20, 31, 12,  3])
>>> np.choose([2, 4, 1, 0], choices, mode='clip')
array([20, 31, 12,  3])
>>> np.choose([2, 4, 1, 0], choices, mode='wrap')
array([20,  1, 12,  3])

Previous topic

numpy.take

Next topic

numpy.compress

This Page

Quick search