Numpy MaskedArray.flatten () Function | python



numpy.MaskedArray.flatten() is used to return a copy of the input masked array collapsed into one dimension.

Syntax: numpy.ma.flatten(order=`C`)

Parameters:
order: [`C`, `F`, `A`, `K`, optional] Whether to flatten in C (row-major), Fortran (column-major) order, or preserve the C / Fortran ordering from a. The default is `C`.

Return: [ndarray] A copy of the input array, flattened to one dimension.

Code # 1:

# Python program explaining
# numpy.MaskedArray.flatten () method

 
# import numy as a geek
# and the numpy.ma module as ma

import numpy as geek 

import numpy.ma as ma 

 
# create an input array 2 * 2

in_arr = geek.array ([[ 10 , 20 ], [ - 10 , 40 ]]) 

print ( "Input array:" , in_arr) 

 
# We now create a masked array
# invalidating one post.

mask_arr = ma.masked_array (in_arr, mask = [[ 1 , 0 ], [ 0 , 0 ]]) 

print ( "Masked array:" , mask_arr) 

 
# applying MaskedArray.flatten methods to create
# this is a one-dimensional flat array

out_arr = mask_arr.flatten () 

print ( "Output flattened masked array:" , out_arr) 

Exit:

 Input array: [[10 20] [-10 40]] Masked array: [[- 20] [-10 40]] Output flattened masked array: [- 20 -10 40] 

Code # 2:

# Python program explaining
# numpy.MaskedArray.flatten () method

 
# import numy as a geek
# and the numpy.ma module as ma

import numpy as geek 

import numpy.ma as ma 

 
# create input array

in_arr = geek.array ([[[ 2e8 , 3e - 5 ]], [[ - 4e - 6 , 2e5 ]]])

print ( "Input array:" , in_arr) 

 
# Now we create a masked array
# making one entry invalid.

mask_arr = ma.masked_array (in_arr, mask = [[[ 1 , 0 ]], [[ 0 , 0 ]]]) 

print ( "Masked array:" , mask _arr) 

 
# applying MaskedArray.flatten methods to create
# this is a one-dimensional array

out_arr = mask_arr.flatten (order = `F`

print ( " Output flattened masked array: " , out_arr)

Output:

 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]