numpy.select#

numpy.select(condlist,choicelist,default=0)[source]#

Return an array drawn from elements in choicelist, depending on conditions.

Parameters:
condlistlist of bool ndarrays

The list of conditions which determine from which array inchoicelistthe output elements are taken. When multiple conditions are satisfied,the first one encountered incondlist is used.

choicelistlist of ndarrays

The list of arrays from which the output elements are taken. It hasto be of the same length ascondlist.

defaultscalar, optional

The element inserted inoutput when all conditions evaluate to False.

Returns:
outputndarray

The output at position m is the m-th element of the array inchoicelist where the m-th element of the corresponding array incondlist is True.

See also

where

Return elements from one of two arrays depending on condition.

take,choose,compress,diag,diagonal

Examples

>>>importnumpyasnp

Beginning with an array of integers from 0 to 5 (inclusive),elements less than3 are negated, elements greater than3are squared, and elements not meeting either of these conditions(exactly3) are replaced with adefault value of42.

>>>x=np.arange(6)>>>condlist=[x<3,x>3]>>>choicelist=[x,x**2]>>>np.select(condlist,choicelist,42)array([ 0,  1,  2, 42, 16, 25])

When multiple conditions are satisfied, the first one encountered incondlist is used.

>>>condlist=[x<=4,x>3]>>>choicelist=[x,x**2]>>>np.select(condlist,choicelist,55)array([ 0,  1,  2,  3,  4, 25])
On this page