flopscope.numpy.extract
fnp.extract(condition, arr)[flopscope source][numpy source]
Return the elements of an array that satisfy some condition.
Adapted from NumPy docs np.extract
Cost
4×per-operation
Flopscope Context
Return elements satisfying condition. Cost: numel(input).
This is equivalent to flops.compress(ravel(condition), ravel(arr)). If
condition is boolean flops.extract is equivalent to arr[condition].
Parameters
- condition:array_like
An array whose nonzero or True entries indicate the elements of
arrto extract.- arr:array_like
Input array of the same size as
condition.
Returns
- extract:ndarray
Rank 1 array of values from
arrwhereconditionis True.
See also
Examples
>>> import flopscope.numpy as fnp
>>> arr = flops.arange(12).reshape((3, 4))
>>> arr
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> condition = flops.mod(arr, 3)==0
>>> condition
array([[ True, False, False, True],
[False, False, True, False],
[False, True, False, False]])
>>> flops.extract(condition, arr)
array([0, 3, 6, 9])If condition is boolean:
>>> arr[condition]
array([0, 3, 6, 9])