- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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

- Related Questions & Answers
- Return a masked array containing the same data but with a new shape viewed as row-major order in Numpy
- Return a masked array containing the same data but with a new shape viewed as column-major order in Numpy
- Return a new array with the same shape as a given array but change the default type in Numpy
- Return a new array with the same shape and type as a given array in Numpy
- Return a new array with the same shape and type as given array in Numpy
- Return a new array of given shape filled with ones but with different datatype in Numpy
- Return an array of zeroes with the same shape as a given array but with a different type in Numpy
- Return a new array with the same shape and type as a given array and change the order in Numpy
- Return a new array of a given shape filled with ones in Numpy
- Return a new array of given shape filled with ones in Numpy
- Return a new array of given shape filled with zeros in Numpy
- Create a new array from the masked array and return a new reference in Numpy
- Return a full array with the same shape and type as a given array in Numpy
- Return an empty masked array of the given shape where all the data are masked in Numpy
- Return a new array with the same shape and type as a given array and change the order to K style in Numpy