The Wayback Machine - https://web.archive.org/web/20230512160720/https://www.geeksforgeeks.org/numpy-compress-python/
Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

numpy.compress() in Python

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The numpy.compress() function returns selected slices of an array along mentioned axis, that satisfies an axis.

Syntax: numpy.compress(condition, array, axis = None, out = None)

Parameters :

condition : [array_like]Condition on the basis of which user extract elements. 
      Applying condition on input_array, if we print condition, it will return an arra
      filled with either True or False. Array elements are extracted from the Indices having 
      True value.
array     : Input array. User apply conditions on input_array elements
axis      : [optional, int]Indicating which slice to select. 
         By Default, work on flattened array[1-D]
out       : [optional, ndarray]Output_array with elements of input_array, 
               that satisfies condition

Return :

Copy of array with elements of input_array,
that satisfies condition and along given axis




# Python Program illustrating
# numpy.compress method
  
import numpy as geek
  
array = geek.arange(10).reshape(5, 2)
print("Original array : \n", array)
  
a = geek.compress([0, 1], array, axis=0)
print("\nSliced array : \n", a)
  
a = geek.compress([False, True], array, axis=0)
print("\nSliced array : \n", a)

Output :

Original array : 
 [[0 1]
 [2 3]
 [4 5]
 [6 7]
 [8 9]]

Sliced array : 
 [[2 3]]

Sliced array : 
 [[2 3]]

References :
https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.compress.html
Note :
This codes won’t run on online-ID. Please run them on your systems to explore the working.
.
This article is contributed by Mohit Gupta_OMG 😀. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up
Last Updated : 22 Oct, 2020
Like Article
Save Article
Similar Reads
Related Tutorials