Python Numpy MaskedArray.resize()函数
numpy.MaskedArray.resize()函数用于从给定的数组中制作一个具有指定大小和形状的新的掩码数组。新数组被arr的重复拷贝所填充(按照数据在内存中的存储顺序)。如果arr是被屏蔽的,新的数组将被屏蔽,新的屏蔽将是旧的屏蔽的重复。
语法: numpy.ma.resize(arr, new_shape)
参数:
arr:要调整大小的输入阵列。
new_shape: [ int or tuple of ints] 调整后的数组的新形状。
返回 : [ 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]]
?