numpy.compress

numpy.compress(condition, a, axis=None, out=None)

Return selected slices of an array along given axis.

Parameters:

condition : array_like

Boolean 1-D array selecting which entries to return. If len(condition) is less than the size of a along 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 a without the slices along axis for which condition is false.

See also

ndarray.compress
Equivalent method.

Examples

>>> a = np.array([[1, 2], [3, 4]])
>>> np.compress([0, 1], a, axis=0)
array([[3, 4]])
>>> np.compress([1], a, axis=1)
array([[1],
       [3]])
>>> np.compress([0,1,1], a)
array([2, 3])

Previous topic

numpy.choose

Next topic

numpy.diag

This Page

Quick search