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


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


numpy.MaskedArray.resize()函數用於根據給定數組創建具有指定大小和形狀的新蒙版數組,該新數組填充有重複的arr副本(以將數據存儲在內存中的順序)。如果將arr屏蔽,則將屏蔽新數組,並且新掩碼將是舊數組的重複。

用法: numpy.ma.resize(arr, new_shape)

參數:
arr:要調整大小的輸入數組。
new_shape:[int或int元組]調整大小後的數組的新形狀。


Return :[resized_array]陣列的新形狀。

代碼1:

# Python program explaining 
# numpy.MaskedArray.resize() method  
    
# importing numpy as geek   
# and numpy.ma module as ma  
import numpy as geek  
import numpy.ma as ma  
    
# creating input array of 2 * 2   
in_arr = geek.array([[10, 20], [-10, 40]])  
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 ("Masked array:", mask_arr)  
    
# applying MaskedArray.resize methods to make   
# it a 3 * 3 masked array 
out_arr = ma.resize(mask_arr, (3, 3))  
print ("Output resized masked array:", out_arr) 
輸出:
Input array: [[ 10  20]
 [-10  40]]
Masked array: [[-- 20]
 [-10 40]]
Output resized masked array: [[-- 20 -10]
 [40 -- 20]
 [-10 40 --]]

代碼2:

# Python program explaining 
# numpy.MaskedArray.resize() 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]], [[ -4e-6, 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 ("Masked array:", mask_arr)  
    
# applying MaskedArray.resize methods to make   
# it a 1 * 6 masked array 
out_arr = ma.resize(mask_arr, (1, 6))  
print ("Output resized masked array:", out_arr)  
輸出:
Input array: [[[ 2.e+08  3.e-05]]

 [[-4.e-06  2.e+05]]]
Masked array: [[[-- 3e-05]]

 [[-4e-06 200000.0]]]
Output resized masked array: [[-- 3e-05 -4e-06 200000.0 -- 3e-05]]
?


相關用法


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