NumPy choose uses integer codes to select from corresponding positions in choice arrays

Selecting from several arrays requires a rule about which array supplies each output entry. NumPy choose uses integer codes to express that rule. Academia Master Trader explains the operation in English for readers in Azerbaijan. Constructed examples show how a code selects a source array while the output position determines which entry is taken from that source. This is different from simply retrieving positions within one vector.

Match each code to a choice array

After import numpy as np, define low = np.array([10,20,30]) and high = np.array([100,200,300]). The expression np.choose([0,1,0],[low,high]) returns [10,200,30]. Code zero names low, the first choice array. Code one names high, the second choice array.

At output position zero, code zero selects low's entry at position zero, which is ten. At position one, code one selects high's entry at position one, which is two hundred. At position two, code zero selects low's entry at position two, which is thirty. Both the code and the position matter.

NumPy choose

A code does not name an entry within one vector

The codes [0,1,0] do not mean to retrieve positions zero, one and zero from low. They identify which member of the choice sequence supplies the value at each output position. This distinction explains why the final entry is thirty rather than ten in the example above.

Keep the choices in an explicit list or tuple. The sequence order establishes the meaning of each integer code. Reversing the choices while leaving the code array unchanged changes the selection rule. For example, np.choose([0,1,0],[high,low]) returns [100,20,300]. Document the code mapping beside the data instead of assuming that zero always names a particular category.

Scalar choices can fill a shaped code array

The call np.choose([[0,1],[1,0]],[5,9]) returns [[5,9],[9,5]]. Each scalar choice is broadcast across the shape of the code array. Zero selects five and one selects nine at each of the four positions. The resulting array has shape (2,2).

The code array and every choice must be broadcastable to a common shape. Equal shapes satisfy that requirement, but they are not the only possibility, as the scalar example shows. Check the common shape when using more complicated inputs. An unexpected shape can change the arrangement of the output even when the code values look familiar.

Default mode requires valid choice codes

With two choices, the valid codes in the default mode are zero and one. A code of two is outside that range and raises an error. The upper limit depends on the number of choices, not on the number of entries within each choice array.

For the three entry low and high arrays, np.choose([0,2,1],[low,high],mode='wrap') returns [10,20,300]. Wrap maps code two back to zero because there are two choices. The same call with mode clip returns [10,200,300], since clipping maps code two to the highest valid code, one. Choose the boundary rule deliberately rather than treating these modes as interchangeable corrections.

Read the result through its source mapping

A selected value remains the entry from its chosen source at the corresponding position after broadcasting. Choose does not average the alternatives or combine their magnitudes. The output is an arrangement governed by the supplied codes, choice sequence and boundary mode.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the code mapping, common shape and mode. Trace a few output positions back to their source entries. This makes the selection understandable without assigning extra meaning to the numerical codes themselves.