Friday, September 5, 2025
HomeLanguagesNumpy MaskedArray asarray() method | Python

Numpy MaskedArray asarray() method | Python

numpy.ma.asarray() function is used when we want to convert input to a masked array of the given data-type.
No copy is performed if the input is already a ndarray. If arr is a subclass of MaskedArray, a base class MaskedArray is returned.

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.

Code #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) 


Output :

Input list :  [1, 4, 8, 7, 2, 5]
output array from input list :  [1 4 8 7 2 5]

 
Code #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) 


Output :

Input tuple :  ([1, 4, 8], [7, 2, 5])
output array from input tuple :  [[1 4 8]
 [7 2 5]]

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11863 POSTS0 COMMENTS
Shaida Kate Naidoo
6750 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6701 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS