MCPcopy Create free account
hub / github.com/numpy/numpy / choose

Function choose

numpy/core/fromnumeric.py:295–416  ·  view source on GitHub ↗

Construct an array from an index array and a list of arrays to choose from. 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.

(a, choices, out=None, mode='raise')

Source from the content-addressed store, hash-verified

293
294@array_function_dispatch(_choose_dispatcher)
295def choose(a, choices, out=None, mode='raise'):
296 """
297 Construct an array from an index array and a list of arrays to choose from.
298
299 First of all, if confused or uncertain, definitely look at the Examples -
300 in its full generality, this function is less simple than it might
301 seem from the following code description (below ndi =
302 `numpy.lib.index_tricks`):
303
304 ``np.choose(a,c) == np.array([c[a[I]][I] for I in ndi.ndindex(a.shape)])``.
305
306 But this omits some subtleties. Here is a fully general summary:
307
308 Given an "index" array (`a`) of integers and a sequence of ``n`` arrays
309 (`choices`), `a` and each choice array are first broadcast, as necessary,
310 to arrays of a common shape; calling these *Ba* and *Bchoices[i], i =
311 0,...,n-1* we have that, necessarily, ``Ba.shape == Bchoices[i].shape``
312 for each ``i``. Then, a new array with shape ``Ba.shape`` is created as
313 follows:
314
315 * if ``mode='raise'`` (the default), then, first of all, each element of
316 ``a`` (and thus ``Ba``) must be in the range ``[0, n-1]``; now, suppose
317 that ``i`` (in that range) is the value at the ``(j0, j1, ..., jm)``
318 position in ``Ba`` - then the value at the same position in the new array
319 is the value in ``Bchoices[i]`` at that same position;
320
321 * if ``mode='wrap'``, values in `a` (and thus `Ba`) may be any (signed)
322 integer; modular arithmetic is used to map integers outside the range
323 `[0, n-1]` back into that range; and then the new array is constructed
324 as above;
325
326 * if ``mode='clip'``, values in `a` (and thus ``Ba``) may be any (signed)
327 integer; negative integers are mapped to 0; values greater than ``n-1``
328 are mapped to ``n-1``; and then the new array is constructed as above.
329
330 Parameters
331 ----------
332 a : int array
333 This array must contain integers in ``[0, n-1]``, where ``n`` is the
334 number of choices, unless ``mode=wrap`` or ``mode=clip``, in which
335 cases any integers are permissible.
336 choices : sequence of arrays
337 Choice arrays. `a` and all of the choices must be broadcastable to the
338 same shape. If `choices` is itself an array (not recommended), then
339 its outermost dimension (i.e., the one corresponding to
340 ``choices.shape[0]``) is taken as defining the "sequence".
341 out : array, optional
342 If provided, the result will be inserted into this array. It should
343 be of the appropriate shape and dtype. Note that `out` is always
344 buffered if ``mode='raise'``; use other modes for better performance.
345 mode : {'raise' (default), 'wrap', 'clip'}, optional
346 Specifies how indices outside ``[0, n-1]`` will be treated:
347
348 * 'raise' : an exception is raised
349 * 'wrap' : value becomes value mod ``n``
350 * 'clip' : values < 0 are mapped to 0, values > n-1 are mapped to n-1
351
352 Returns

Callers

nothing calls this directly

Calls 1

_wrapfuncFunction · 0.85

Tested by

no test coverage detected