dask 2021.10.0

NotesParametersRaisesReturnsBackRef
choose(a, choices)

This docstring was copied from numpy.choose.

Some inconsistencies with the Dask version may exist.

First of all, if confused or uncertain, definitely look at the Examples - in its full generality, this function is less simple than it might seem from the following code description (below ndi = numpy.lib.index_tricks ):

np.choose(a,c) == np.array([c[a[I]][I] for I in ndi.ndindex(a.shape)]) .

But this omits some subtleties. Here is a fully general summary:

Given an "index" array (a) of integers and a sequence of n arrays (:None:None:`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 with shape Ba.shape is created as follows:

Notes

To reduce the chance of misinterpretation, even though the following "abuse" is nominally supported, :None:None:`choices` should neither be, nor be thought of as, a single array, i.e., the outermost sequence-like container should be either a list or a tuple.

Parameters

a : int array

This array must contain integers in [0, n-1] , where n is the number of choices, unless mode=wrap or mode=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 :None:None:`choices` is itself an array (not recommended), then its outermost dimension (i.e., the one corresponding to choices.shape[0] ) is taken as defining the "sequence".

out : array, optional (Not supported in Dask)

If provided, the result will be inserted into this array. It should be of the appropriate shape and dtype. Note that :None:None:`out` is always buffered if mode='raise' ; use other modes for better performance.

mode : {'raise' (default), 'wrap', 'clip'}, optional (Not supported in Dask)

Specifies how indices outside [0, n-1] will be treated:

Raises

ValueError: shape mismatch

If a and each choice array are not all broadcastable to the same shape.

Returns

merged_array : array

The merged result.

Construct an array from an index array and a list of arrays to choose from.

See Also

ndarray.choose

equivalent method

numpy.take_along_axis

Preferable if :None:None:`choices` is an array

Examples

This example is valid syntax, but we were not able to check execution
>>> choices = [[0, 1, 2, 3], [10, 11, 12, 13],  # doctest: +SKIP
...  [20, 21, 22, 23], [30, 31, 32, 33]]
... np.choose([2, 3, 1, 0], choices # doctest: +SKIP
... # the first element of the result will be the first element of the
... # third (2+1) "array" in choices, namely, 20; the second element
... # will be the second element of the fourth (3+1) choice array, i.e.,
... # 31, etc.
... ) array([20, 31, 12, 3])
This example is valid syntax, but we were not able to check execution
>>> np.choose([2, 4, 1, 0], choices, mode='clip') # 4 goes to 3 (4-1)  # doctest: +SKIP
array([20, 31, 12,  3])
This example is valid syntax, but we were not able to check execution
>>> # because there are 4 choice arrays
... np.choose([2, 4, 1, 0], choices, mode='wrap') # 4 goes to (4 mod 4) # doctest: +SKIP array([20, 1, 12, 3])
This example is valid syntax, but we were not able to check execution
>>> # i.e., 0

A couple examples illustrating how choose broadcasts:

This example is valid syntax, but we were not able to check execution
>>> a = [[1, 0, 1], [0, 1, 0], [1, 0, 1]]  # doctest: +SKIP
... choices = [-10, 10] # doctest: +SKIP
... np.choose(a, choices) # doctest: +SKIP array([[ 10, -10, 10], [-10, 10, -10], [ 10, -10, 10]])
This example is valid syntax, but we were not able to check execution
>>> # With thanks to Anne Archibald
... a = np.array([0, 1]).reshape((2,1,1)) # doctest: +SKIP
... c1 = np.array([1, 2, 3]).reshape((1,3,1)) # doctest: +SKIP
... c2 = np.array([-1, -2, -3, -4, -5]).reshape((1,1,5)) # doctest: +SKIP
... np.choose(a, (c1, c2)) # result is 2x3x5, res[0,:,:]=c1, res[1,:,:]=c2 # doctest: +SKIP array([[[ 1, 1, 1, 1, 1], [ 2, 2, 2, 2, 2], [ 3, 3, 3, 3, 3]], [[-1, -2, -3, -4, -5], [-1, -2, -3, -4, -5], [-1, -2, -3, -4, -5]]])
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

dask.array.routines.piecewise dask.array.routines.where dask.array.routines.select dask.array.routines.compress

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /dask/array/routines.py#1974
type: <class 'function'>
Commit: