Friday, December 12, 2025
HomeLanguagesnumpy.ma.where() function – Python

numpy.ma.where() function – Python

numpy.ma.where() function return a masked array with elements from x or y, depending on condition.

Syntax : numpy.ma.where(condition, x, y)

Parameter :
condition : [array_like, bool] Where True, yield x, otherwise yield y.
x, y : [array_like, optional] Values from which to choose. x, y and condition need to be broadcastable to some shape.

Return : [MaskedArray] An masked array with masked elements where the condition is masked, elements from x where condition is True, and elements from y elsewhere.

Code #1 :




# Python program explaining
# numpy.ma.where() function
   
# importing numpy as geek 
# and numpy.ma module as ma 
import numpy as geek 
import numpy.ma as ma
    
x = geek.ma.array(geek.arange(4.).reshape(2, 2), 
                        mask =[[0, 1], [1, 0]])
  
gfg = geek.ma.where(x > 5, x, -3.1416)
   
print (gfg)


Output :

[[-3.1416 --]
 [-- -3.1416]]

 
Code #2 :




# Python program explaining
# numpy.ma.where() function
   
# importing numpy as geek 
# and numpy.ma module as ma 
import numpy as geek 
import numpy.ma as ma
    
x = geek.ma.array(geek.arange(9.).reshape(3, 3), 
       mask =[[0, 1, 0], [1, 0, 1], [0, 1, 0]])
  
gfg = geek.ma.where(x > 5, x, -3.1416)
   
print (gfg)


Output :

[[-3.1416 -- -3.1416]
 [-- -3.1416 --]
 [6.0 -- 8.0]]
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32444 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6813 POSTS0 COMMENTS
Nicole Veronica
11951 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12028 POSTS0 COMMENTS
Shaida Kate Naidoo
6945 POSTS0 COMMENTS
Ted Musemwa
7198 POSTS0 COMMENTS
Thapelo Manthata
6892 POSTS0 COMMENTS
Umr Jansen
6881 POSTS0 COMMENTS