NumPy ndarray.choose()
The numpy.ndarray.choose()
method constructs a new array by selecting elements from multiple choices based on an index array.
Each element in the calling array serves as an index to pick a corresponding element from a set of given choices.
Syntax
ndarray.choose(choices, out=None, mode='raise')
Parameters
Parameter | Type | Description |
---|---|---|
choices | sequence of arrays | A sequence of arrays from which elements are chosen. The shape of each choice array must match the shape of the calling array. |
out | ndarray, optional | Alternative output array to store the result. It must match the expected shape. |
mode | {‘raise’, ‘wrap’, ‘clip’}, optional | Determines behavior when an index is out of bounds:
|
Return Value
Returns an array constructed by choosing elements from choices
according to the indices in the calling array.
Examples
1. Basic Usage of ndarray.choose()
In this example, we use choose()
to select values from different arrays based on an index array.
import numpy as np
# Define an index array
index_array = np.array([[0, 1],
[1, 0]])
# Define the choice arrays
choice1 = np.array([[10, 20],
[30, 40]])
choice2 = np.array([[100, 200],
[300, 400]])
# Select values using choose()
result = index_array.choose([choice1, choice2])
print("Chosen elements:\n", result)
Output:
Chosen elements:
[[ 10 200]
[300 40]]
The values are selected from choice1
if the index is 0 and from choice2
if the index is 1.
2. Using Out Parameter in ndarray.choose()
Here, we store the result in a predefined output array.
import numpy as np
index_array = np.array([[1, 0],
[0, 1]])
choice1 = np.array([[5, 10],
[15, 20]])
choice2 = np.array([[50, 100],
[150, 200]])
# Define an output array with the same shape as the expected result
output_array = np.empty_like(choice1)
# Store the result in the output array
index_array.choose([choice1, choice2], out=output_array)
print("Output array:\n", output_array)
Output:
Output array:
[[ 50 10]
[ 15 200]]
The selected values are stored in output_array
instead of creating a new one.
3. Handling Out-of-Bounds Indices with mode=’clip’
Using mode='clip'
ensures that out-of-bounds indices are clipped to the valid range.
import numpy as np
index_array = np.array([[0, 2], # 2 is out of bounds
[1, -1]]) # -1 is also out of bounds
choice1 = np.array([[1, 2],
[3, 4]])
choice2 = np.array([[10, 20],
[30, 40]])
choice3 = np.array([[100, 200],
[300, 400]])
# Applying choose() with mode='clip'
result = index_array.choose([choice1, choice2, choice3], mode='clip')
print("Result with mode='clip':\n", result)
Output:
Result with mode='clip':
[[ 1 200]
[ 30 1]]
Out-of-bounds indices are clipped to the nearest valid index. The index 2
is valid, but -1
is clipped to 0
.
4. Using mode=’wrap’ to Wrap Indices
Using mode='wrap'
wraps out-of-bounds indices using modular arithmetic.
import numpy as np
index_array = np.array([[0, 3], # 3 wraps around to 0
[1, -2]]) # -2 wraps around to 1
choice1 = np.array([[5, 10],
[15, 20]])
choice2 = np.array([[50, 100],
[150, 200]])
# Applying choose() with mode='wrap'
result = index_array.choose([choice1, choice2], mode='wrap')
print("Result with mode='wrap':\n", result)
Output:
Result with mode='wrap':
[[ 5 100]
[150 20]]
The index 3
wraps to 0
, and -2
wraps to 1
, ensuring valid selection.