📜  numpy.ma.where()函数– Python

📅  最后修改于: 2022-05-13 01:55:17.805000             🧑  作者: Mango

numpy.ma.where()函数– Python

numpy.ma.where()函数返回一个掩码数组,其中包含来自 x 或 y 的元素,具体取决于条件。

代码#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)

输出 :

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


代码#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)

输出 :

[[-3.1416 -- -3.1416]
 [-- -3.1416 --]
 [6.0 -- 8.0]]