# Return a masked array containing the same data but with a new shape viewed as row-major order in Numpy

NumpyServer Side ProgrammingProgramming

#### Python Data Science basics with Numpy, Pandas and Matplotlib

Most Popular

63 Lectures 6 hours

#### Data Analysis using NumPy and Pandas

19 Lectures 8 hours

#### Numpy with Python

Most Popular

12 Lectures 3 hours

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 order is set using the "order" parameter. The 'C' order determines whether the array data should be viewed as in C (row-major).

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 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(). The order is set using the "order" parameter. The 'C' order determines whether the array data should be viewed as in C (row-major) −

print("\nResult...\n",maskArr.reshape((6,1),order='C'))

## Example

# Python ma.MaskedArray - Return a masked array containing the same data but with a new shape
# viewed as row-major order

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]])

# Get the dimensions of the Masked Array

# Get the shape of the Masked Array

# Get the number of elements of the Masked Array

# 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
# The order is set using the "order" parameter
# The 'C' order determines whether the array data should be viewed as in C (row-major)
print("\nResult...\n",maskArr.reshape((6,1),order='C'))

## Output

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

Array type...
int64

Array Dimensions...
2

[[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...
[
[--]



[--]]