flopscope.numpy.compress
fnp.compress(condition, a, axis=None, out=None)[flopscope source][numpy source]
Return selected slices of an array along given axis.
Adapted from NumPy docs np.compress
Return selected slices along axis. Cost: numel(input).
Parameters
- condition:1-D array of bools
Array that selects which entries to return. If len(condition) is less than the size of
aalong the given axis, then output is truncated to the length of the condition array.- a:array_like
Array from which to extract a part.
- axis:int, optional
Axis along which to take slices. If None (default), work on the flattened array.
- out:ndarray, optional
Output array. Its type is preserved and it must be of the right shape to hold the output.
Returns
- compressed_array:ndarray
A copy of
awithout the slices along axis for whichconditionis false.
See also
- we.flops.take
- we.flops.choose
- we.flops.diag
- we.flops.diagonal
- we.flops.select
- ndarray.compress Equivalent method in ndarray
- we.flops.extract Equivalent method when working on 1-D arrays
- ufuncs-output-type
Examples
>>> import flopscope.numpy as fnp
>>> a = flops.array([[1, 2], [3, 4], [5, 6]])
>>> a
array([[1, 2],
[3, 4],
[5, 6]])
>>> flops.compress([0, 1], a, axis=0)
array([[3, 4]])
>>> flops.compress([False, True, True], a, axis=0)
array([[3, 4],
[5, 6]])
>>> flops.compress([False, True], a, axis=1)
array([[2],
[4],
[6]])Working on the flattened array does not return slices along an axis but selects elements.
>>> flops.compress([False, True], a)
array([2])