Open In App

numpy.ma.compress_rowcols() function in Python

Last Updated : 12 Nov, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

numpy.ma.compress_rowcols() function suppresses rows and columns that contain masked values in a 2-D array. 
The suppression behavior is selected with the axis parameter: 

  • If axis is None, both rows and columns are suppressed.
  • If axis is 0, only rows are suppressed.
  • If axis is 1 or -1, only columns are suppressed.

Syntax : numpy.ma.compress_rowcols(arr, axis = None) 

Parameters : 
arr : [array_like, MaskedArray] This parameter holds the array to operate on.The array must be a 2D array. If no array elements are masked, arr is interpreted as a MaskedArray with mask set to nomask. 
axis : [int, optional] Axis along which to perform the operation. Default is None. 

Return : Return the compressed array.

Code #1: 

Python3
# Python program explaining
# numpy.ma.compress_rowcols() function

# importing numpy as geek
import numpy as geek

arr = geek.ma.array(geek.arange(6).reshape(2, 3),
                    mask=[[1, 0, 0], [0, 0, 0]])

gfg = geek.ma.compress_rowcols(arr)

print(gfg)

Output: 

[[4 5]]

Code #2: 

Python3
# Python program explaining
# numpy.ma.compress_rowcols() function

# importing numpy as geek
import numpy as geek

arr = geek.ma.array(geek.arange(6).reshape(2, 3),
                    mask=[[1, 0, 0], [0, 0, 0]])

gfg = geek.ma.compress_rowcols(arr, 1)

print(gfg)

Output:

[[1 2]
 [4 5]]

Next Article
Practice Tags :

Similar Reads