numpy.extract#

numpy.extract(condition,arr)[source]#

Return the elements of an array that satisfy some condition.

This is equivalent tonp.compress(ravel(condition),ravel(arr)). Ifcondition is booleannp.extract is equivalent toarr[condition].

Note thatplace does the exact opposite ofextract.

Parameters:
conditionarray_like

An array whose nonzero or True entries indicate the elements ofarrto extract.

arrarray_like

Input array of the same size ascondition.

Returns:
extractndarray

Rank 1 array of values fromarr wherecondition is True.

Examples

>>>importnumpyasnp>>>arr=np.arange(12).reshape((3,4))>>>arrarray([[ 0,  1,  2,  3],       [ 4,  5,  6,  7],       [ 8,  9, 10, 11]])>>>condition=np.mod(arr,3)==0>>>conditionarray([[ True, False, False,  True],       [False, False,  True, False],       [False,  True, False, False]])>>>np.extract(condition,arr)array([0, 3, 6, 9])

Ifcondition is boolean:

>>>arr[condition]array([0, 3, 6, 9])
On this page