Numpy MaskedArray asarray() 方法 | Python
当我们想要将输入转换为给定数据类型的掩码数组时,使用numpy.ma.asarray()
函数。
如果输入已经是 ndarray,则不执行复制。如果 arr 是 MaskedArray 的子类,则返回基类 MaskedArray。
Syntax : numpy.ma.asarray(arr, dtype=None, order=None)
Parameters :
arr : [array_like] Input data, in any form that can be converted to a masked array. This includes lists, lists of tuples, tuples, tuples of tuples, tuples of lists, ndarrays and masked arrays.
dtype : [data-type, optional] By default, the data-type is inferred from the input data.
order : Whether to use row-major (C-style) or column-major (Fortran-style) memory representation. Defaults to ‘C’.
Return : [MaskedArray] Masked array interpretation of arr.
代码#1:
# Python program explaining
# numpy.ma.asarray() function
import numpy as geek
my_list = [1, 4, 8, 7, 2, 5]
print ("Input list : ", my_list)
out_arr = geek.ma.asarray(my_list)
print ("output array from input list : ", out_arr)
输出 :
Input list : [1, 4, 8, 7, 2, 5]
output array from input list : [1 4 8 7 2 5]
代码#2:
# Python program explaining
# numpy.ma.asarray() function
import numpy as geek
my_tuple = ([1, 4, 8], [7, 2, 5])
print ("Input tuple : ", my_tuple)
out_arr = geek.ma.asarray(my_tuple)
print ("output array from input tuple : ", out_arr)
输出 :
Input tuple : ([1, 4, 8], [7, 2, 5])
output array from input tuple : [[1 4 8]
[7 2 5]]