Return a masked array containing the same data but with a new shape in Numpy

NumpyServer Side ProgrammingProgramming

To return a masked array containing the same data, but with a new shape, use the ma.MaskedArray.reshape() method in Numpy. Give a new shape to the array without changing its data. The new shape should be compatible with the original shape. If an integer is supplied, then the result will be a 1-D array of that length.

The order determines whether the array data should be viewed as in C (row-major) or FORTRAN (column-major) order. Returns a masked array containing the same data, but with a new shape. The result is a view on the original array; if this is not possible, a ValueError is raised.

Steps

At first, import the required library −

import numpy as np
import numpy.ma as ma

Create an array with int elements using the numpy.array() method −

arr = np.array([[49, 85, 45], [67, 33, 59]])
print("Array...\n", arr)
print("\nArray type...\n", arr.dtype)

Get the dimensions of the Array −

print("Array Dimensions...\n",arr.ndim)

Create a masked array and mask some of them as invalid −

maskArr = ma.masked_array(arr, mask =[[0, 0, 1], [ 0, 1, 0]])
print("\nOur Masked Array\n", maskArr)
print("\nOur Masked Array type...\n", maskArr.dtype)

Get the dimensions of the Masked Array −

print("\nOur Masked Array Dimensions...\n",maskArr.ndim)

Get the shape of the Masked Array −

print("\nOur Masked Array Shape...\n",maskArr.shape)

Get the number of elements of the Masked Array −

print("\nElements in the Masked Array...\n",maskArr.size)

Return a masked array containing the same data, but with a new shape, use the ma.MaskedArray.reshape() method. Give a new shape to the array without changing its data. The new shape of the masked array is set to 6x1 as a parameter. The new shape should be compatible with the original shape. If an integer is supplied, then the result will be a 1-D array of that length −

print("\nResult...\n",maskArr.reshape((6,1)))

Example

# Python ma.MaskedArray - Return a masked array containing the same data but with a new shape

import numpy as np
import numpy.ma as ma

# Create an array with int elements using the numpy.array() method
arr = np.array([[78, 85, 51], [56, 33, 97]])
print("Array...\n", arr)
print("\nArray type...\n", arr.dtype)

# Get the dimensions of the Array
print("\nArray Dimensions...\n",arr.ndim)

# Create a masked array and mask some of them as invalid

# The masked array is 1x6
maskArr = ma.masked_array(arr, mask =[[0, 1, 0, 0, 0, 1]])
print("\nOur Masked Array\n", maskArr)
print("\nOur Masked Array type...\n", maskArr.dtype)

# Get the dimensions of the Masked Array
print("\nOur Masked Array Dimensions...\n",maskArr.ndim)

# Get the shape of the Masked Array
print("\nOur Masked Array Shape...\n",maskArr.shape)

# Get the number of elements of the Masked Array
print("\nElements in the Masked Array...\n",maskArr.size)

# To return a masked array containing the same data, but with a new shape, use the ma.MaskedArray.reshape() method in Numpy
# Give a new shape to the array without changing its data
# The new shape of the masked array is set to 6x1 as a parameter
# The new shape should be compatible with the original shape.
# If an integer is supplied, then the result will be a 1-D array of that length
print("\nResult...\n",maskArr.reshape((6,1)))

Output

Array...
[[78 85 51]
[56 33 97]]

Array type...
int64

Array Dimensions...
2

Our Masked Array
[[78 -- 51]
[56 33 --]]

Our Masked Array type...
int64

Our Masked Array Dimensions...
2

Our Masked Array Shape...
(2, 3)

Elements in the Masked Array...
6

Result...
[[78]
[--]
[51]
[56]
[33]
[--]]
raja
Updated on 02-Feb-2022 09:00:47

Advertisements