numpy.compress#

numpy.compress(condition,a,axis=None,out=None)[source]#

Return selected slices of an array along given axis.

When working along a given axis, a slice along that axis is returned inoutput for each index wherecondition evaluates to True. Whenworking on a 1-D array,compress is equivalent toextract.

Parameters:
condition1-D array of bools

Array that selects which entries to return. If len(condition)is less than the size ofa along the given axis, then output istruncated to the length of the condition array.

aarray_like

Array from which to extract a part.

axisint, optional

Axis along which to take slices. If None (default), work on theflattened array.

outndarray, optional

Output array. Its type is preserved and it must be of the rightshape to hold the output.

Returns:
compressed_arrayndarray

A copy ofa without the slices along axis for whichconditionis false.

See also

take,choose,diag,diagonal,select
ndarray.compress

Equivalent method in ndarray

extract

Equivalent method when working on 1-D arrays

Output type determination

Examples

>>>importnumpyasnp>>>a=np.array([[1,2],[3,4],[5,6]])>>>aarray([[1, 2],       [3, 4],       [5, 6]])>>>np.compress([0,1],a,axis=0)array([[3, 4]])>>>np.compress([False,True,True],a,axis=0)array([[3, 4],       [5, 6]])>>>np.compress([False,True],a,axis=1)array([[2],       [4],       [6]])

Working on the flattened array does not return slices along an axis butselects elements.

>>>np.compress([False,True],a)array([2])
On this page