📜  Numpy MaskedArray.default_fill_value()函数| Python

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

Numpy MaskedArray.default_fill_value()函数| Python

numpy.MaskedArray.default_fill_value()函数用于返回参数对象的默认填充值。默认填充值取决于输入数组的数据类型或输入标量的类型。如果数据类型为 bool,则默认填充值为没错,int 是 999999,float 是 1.e20,复数是 1.e20+0.j,'?'对象和字符串'N/A' 。

代码#1:

# Python program explaining
# numpy.MaskedArray.default_fill_value() method 
    
# importing numpy as geek  
# and numpy.ma module as ma 
import numpy as geek 
import numpy.ma as ma 
    
# creating input array  
in_arr = geek.array([[1, 2], [ 3, -1], [ 5, -3]])
print ("Input array : ", in_arr) 
    
# Now we are creating a masked array. 
# by making  entry as invalid.  
mask_arr = ma.masked_array(in_arr,
     mask =[[1, 0], [ 1, 0], [ 0, 0]]) 
  
print ("Masked array : ", mask_arr) 
    
# applying MaskedArray.default_fill_value    
# methods to masked array
out_val = ma.default_fill_value(mask_arr) 
print ("Default filled value : ", out_val) 
输出:
Input array :  [[ 1  2]
 [ 3 -1]
 [ 5 -3]]
Masked array :  [[-- 2]
 [-- -1]
 [5 -3]]
Default filled value :  999999

代码#2:

# Python program explaining
# numpy.MaskedArray.default_fill_value() method 
    
# importing numpy as geek  
# and numpy.ma module as ma 
import numpy as geek 
import numpy.ma as ma 
    
# creating input array  
in_arr = geek.array([[1 + 2j, 2 + 3j],
                     [ 3-2j, -1 + 2j],
                     [ 5-4j, -3-3j]])
print ("Input array : ", in_arr) 
    
# Now we are creating a masked array. 
# by making two entry as invalid.  
mask_arr = ma.masked_array(in_arr, 
     mask =[[1, 0], [ 1, 0], [ 0, 0]]) 
  
print ("Masked array : ", mask_arr) 
  
# applying MaskedArray.default_fill_value    
# methods to masked array
out_val = ma.default_fill_value(mask_arr) 
print ("Default filled value : ", out_val) 
输出:
Input array :  [[ 1.+2.j  2.+3.j]
 [ 3.-2.j -1.+2.j]
 [ 5.-4.j -3.-3.j]]
Masked array :  [[-- (2+3j)]
 [-- (-1+2j)]
 [(5-4j) (-3-3j)]]
Default filled value :  (1e+20+0j)