当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python Numpy MaskedArray.flatten()用法及代码示例


numpy.MaskedArray.flatten()函数用于返回折叠成一维的输入掩码数组的副本。

用法: numpy.ma.flatten(order='C')

参数:
order :[“ C”,“ F”,“ A”,“ K”,可选)是按C(行大),Fortran(列大)顺序展平还是保留a的C /Fortran顺序。默认值为“ C”。


Return :[ndarray]输入数组的副本,展平为一个维度。

代码1:

# Python program explaining 
# numpy.MaskedArray.flatten() 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.flatten methods to make   
# it a 1D flattened array 
out_arr = mask_arr.flatten()  
print ("Output flattened masked array:", out_arr) 
输出:
Input array: [[ 10  20]
 [-10  40]]
Masked array: [[-- 20]
 [-10 40]]
Output flattened masked array: [-- 20 -10 40]

代码2:

# Python program explaining 
# numpy.MaskedArray.flatten() 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.flatten methods to make   
# it a 1D masked array 
out_arr = mask_arr.flatten(order ='F')  
print ("Output flattened 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 flattened masked array: [-- -4e-06 3e-05 200000.0]



相关用法


注:本文由纯净天空筛选整理自jana_sayantan大神的英文原创作品 Numpy MaskedArray.flatten() function | Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。