A masked array in NumPy can be reshaped using the ma.reshape()
function of the numpy.ma
module. The new array with a new shape will still have the same data as the original array.
A masked array is an array that has any or all of its elements hidden or masked with a
--
.
The ma.reshape()
function takes the following syntax:
ma.reshape(a, new_shape, order='C')
The ma.reshape()
function takes the following parameter values:
a
: This is the input array and is required. new_shape
: This is the desired shape of the output array. It is also a required parameter. order
: This determines the order in which the array data should be viewed. It takes any of the following orders: "C"
, "F"
, "A"
, or "K"
. This is an optional parameter. The ma.reshape()
function returns an array of the same data as the input array but with a different shape.
# A code to illustrate the ma.reshape() function in NumPY# importing the numpy.ma moduleimport numpy.ma as ma# creating a masked arraymy_array = ma.array([[1, 2, 3, 4] ,[5, 6, 7, 8]], mask=[1,0,1,0,1,0,1,0])# printing the input arrayprint(my_array)# reshaping the arraynew_array = ma.reshape(my_array, (4,2), order = "F")# printing the new arrayprint(new_array)
numpy.ma
module.ma.array()
function to create a masked 2D
array, my_array
. It has the shape of (2, 4)
. This means that the two arrays have four elements each. my_array
.ma.reshape()
function to reshape the input array to an array of the shape, (4, 2)
. This means that the four arrays have two elements each. The result is assigned to a variable, new_array
.new_array
.