numpy.extract

numpy.extract(condition, arr)

Return the elements of an array that satisfy some condition.

This is equivalent to np.compress(ravel(condition), ravel(arr)). If condition is boolean np.extract is equivalent to arr[condition].

Parameters:

condition : array_like

An array whose nonzero or True entries indicate the elements of arr to extract.

arr : array_like

Input array of the same size as condition.

See also

take, put, putmask

Examples

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

If condition is boolean:

>>> arr[condition]
array([ 3,  6,  9, 12])

Previous topic

numpy.searchsorted

Next topic

Logic functions

This Page

Quick search