- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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...
", arr) print("
Array type...
", arr.dtype)
Get the dimensions of the Array −
print("Array Dimensions...
",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("
Our Masked Array
", maskArr) print("
Our Masked Array type...
", maskArr.dtype)
Get the dimensions of the Masked Array −
print("
Our Masked Array Dimensions...
",maskArr.ndim)
Get the shape of the Masked Array −
print("
Our Masked Array Shape...
",maskArr.shape)
Get the number of elements of the Masked Array −
print("
Elements in the Masked Array...
",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("
Result...
",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...
", arr) print("
Array type...
", arr.dtype) # Get the dimensions of the Array print("
Array Dimensions...
",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("
Our Masked Array
", maskArr) print("
Our Masked Array type...
", maskArr.dtype) # Get the dimensions of the Masked Array print("
Our Masked Array Dimensions...
",maskArr.ndim) # Get the shape of the Masked Array print("
Our Masked Array Shape...
",maskArr.shape) # Get the number of elements of the Masked Array print("
Elements in the Masked Array...
",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("
Result...
",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 Articles
- 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 given array in Numpy
- Return a new array with the same shape and type as a 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
- Create a new array from the masked array and return a new reference 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
- Return a new array of a given shape filled with ones in Numpy
- Return an empty masked array of the given shape where all the data are masked in Numpy
- Return a full array with the same shape and type as a given array in Numpy
- Return a new array of given shape and type, filled with array-like in Numpy
