Thursday, September 4, 2025
HomeLanguagesNumpy MaskedArray.compressed() function – Python

Numpy MaskedArray.compressed() function – Python

numpy.MaskedArray.compressed() function return all the non-masked data as a 1-D array.

Syntax : numpy.MaskedArray.compressed(self)

Return : [ndarray] A new ndarray holding the non-masked data is returned.

Code #1 :




# Python program explaining
# numpy.MaskedArray.compressed() function
  
# importing numpy as geek  
# and numpy.ma module as ma 
import numpy as geek 
import numpy.ma as ma
  
arr = geek.ma.array(geek.arange(10), mask =[0]*6 + [1]*4)
gfg = arr.compressed()
  
print(gfg)


Output :

[0 1 2 3 4 5]

 
Code #2 :




# Python program explaining
# numpy.MaskedArray.compressed() function
  
# importing numpy as geek  
# and numpy.ma module as ma 
import numpy as geek 
import numpy.ma as ma
  
arr = geek.ma.array(geek.arange(7), mask =[0]*4 + [1]*3)
gfg = arr.compressed()
  
print(gfg)


Output :

[0 1 2 3]
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6632 POSTS0 COMMENTS
Nicole Veronica
11800 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11859 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS