當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python Numpy MaskedArray.atleast_1d()用法及代碼示例


numpy.MaskedArray.atleast_1d()函數用於將輸入轉換為具有至少一維的掩碼數組。標量輸入將轉換為一維數組,而高維輸入將被保留。

用法: numpy.ma.atleast_1d(*arys)

參數:
arys:[數組]一個或多個輸入數組。


Return :[ndarray]一個數組或數組列表,每個數組 arr.ndim >= 1

代碼1:

# Python program explaining 
# numpy.MaskedArray.atleast_1d() method  
    
# importing numpy as geek   
# and numpy.ma module as ma  
import numpy as geek  
import numpy.ma as ma  
    
# creating input arrays   
in_arr1 = geek.array([[1, 2], [ 3, -1], [ 5, -3]]) 
print ("1st Input array:", in_arr1) 
  
in_arr2 = geek.array(2) 
print ("2nd Input array:", in_arr2) 
    
# Now we are creating  masked array.  
# by making  entry as invalid.   
mask_arr1 = ma.masked_array(in_arr1, mask =[[ 1, 0], [ 0, 1], [ 0, 0]])  
print ("1st Masked array:", mask_arr1) 
  
mask_arr2 = ma.masked_array(in_arr2, mask = 0)  
print ("2nd Masked array:", mask_arr2) 
    
# applying MaskedArray.atleast_1d methods  
# to masked array  
out_arr = ma.atleast_1d(mask_arr1, mask_arr2)  
print ("Output masked array:", out_arr) 
輸出:
1st Input array: [[ 1  2]
 [ 3 -1]
 [ 5 -3]]
2nd Input array: 2
1st Masked array: [[-- 2]
 [3 --]
 [5 -3]]
2nd Masked array: 2
Output masked array: [masked_array(
  data=[[--, 2],
        [3, --],
        [5, -3]],
  mask=[[ True, False],
        [False,  True],
        [False, False]],
  fill_value=999999), masked_array(data=[2],
             mask=[False],
       fill_value=999999)]

代碼2:

# Python program explaining 
# numpy.MaskedArray.atleast_1d() 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([[[ 2e8, 3e-5]], [[ -45.0, 2e5]]]) 
print ("Input array:", in_arr) 
      
# Now we are creating a masked array.  
# by making one entry as invalid.   
mask_arr = ma.masked_array(in_arr, mask =[[[ 1, 0]], [[ 0, 0]]])  
print ("3D Masked array:", mask_arr)  
     
# applying MaskedArray.atleast_1d methods  
# to masked array 
out_arr = ma.atleast_1d(mask_arr)  
print ("Output masked array:", out_arr)
輸出:
Input array: [[[ 2.0e+08  3.0e-05]]

 [[-4.5e+01  2.0e+05]]]
3D Masked array: [[[-- 3e-05]]

 [[-45.0 200000.0]]]
Output masked array: [[[-- 3e-05]]

 [[-45.0 200000.0]]]


相關用法


注:本文由純淨天空篩選整理自jana_sayantan大神的英文原創作品 Numpy MaskedArray.atleast_1d() function | Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。