bundles / numpy latest / numpy / compress
_ArrayFunctionDispatcher
numpy:compress
source: /dev/numpy/build-install/usr/lib/python3.14/site-packages/numpy/_core/fromnumeric.py :2137
Signature
def compress ( condition , a , axis = None , out = None ) Summary
Return selected slices of an array along given axis.
Extended Summary
When working along a given axis, a slice along that axis is returned in output for each index where condition evaluates to True. When working on a 1-D array, compress is equivalent to extract.
Parameters
condition: 1-D array of boolsArray 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_likeArray from which to extract a part.
axis: int, optionalAxis along which to take slices. If None (default), work on the flattened array.
out: ndarray, optionalOutput array. Its type is preserved and it must be of the right shape to hold the output.
Returns
compressed_array: ndarrayA copy of
awithout the slices along axis for whichconditionis false.
Examples
import numpy as np a = np.array([[1, 2], [3, 4], [5, 6]]) a np.compress([0, 1], a, axis=0) np.compress([False, True, True], a, axis=0) np.compress([False, True], a, axis=1)✓
np.compress([False, True], a)
✓See also
- choose
- diag
- diagonal
- extract
Equivalent method when working on 1-D arrays
- ndarray.compress
Equivalent method in ndarray
- select
- take
- ufuncs-output-type
ref
Aliases
-
numpy.compress