- 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 view of the masked array with axes transposed along given axis in NumPy

To return a view of the array with axes transposed in Python, use the **ma.MaskedArray.transpose()** method in Numpy. For a 1-D array this has no effect, as a transposed vector is simply the same vector. To convert a 1-D array into a 2D column vector, an additional dimension must be added. np.atleast2d(a).T achieves this, as does a[:, np.newaxis]. For a 2-D array, this is a standard matrix transpose.

The axes can be,

None or no argument − reverses the order of the axes.

tuple of ints − i in the j-th place in the tuple means a’s i-th axis becomes a.transpose()’s j-th axis.

n ints − same as an n-tuple of the same ints

## 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 view of the array with axes transposed, use the ma.MaskedArray.transpose() −

print("\nResult...\n",maskArr.transpose((1, 0)))

## Example

# Python ma.MaskedArray - Return a view of the array with axes transposed along given axis 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 maskArr = ma.masked_array(arr, mask =[[0, 1, 0], [ 0, 0, 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) # To return a view of the array with axes transposed, use the ma.MaskedArray.transpose() method in Numpy print("\nResult...\n",maskArr.transpose((1, 0)))

## Output

Array... [[78 85 51] [56 33 97]] Array type... int64 Array Dimensions... 2 Our Masked Array [[78 -- 51] [56 33 97]] Our Masked Array type... int64 Our Masked Array Dimensions... 2 Our Masked Array Shape... (2, 3) Elements in the Masked Array... 6 Result... [[78 56] [-- 33] [51 97]]

- Related Questions & Answers
- Return a view of the masked array with axes transposed in NumPy
- Return the variance of the masked array elements along given axis in Numpy
- Return the standard deviation of the masked array elements along given axis in NumPy
- Return range of values from a masked array along a given axis in NumPy
- Repeat elements of a masked array along given axis in NumPy
- Return the average of the masked array elements along specific axis in Numpy
- Return the variance of the masked array elements along column axis in Numpy
- Count the non-masked elements of the masked array along the given axis in Numpy
- Compute the maximum of the masked array elements along a given axis in Numpy
- Compute the minimum of the masked array elements along a given axis in Numpy
- Return the standard deviation of the masked array elements along row axis in NumPy
- Return the standard deviation of the masked array elements along column axis in NumPy
- Return array of indices of the maximum values along axis 0 from a masked array in NumPy
- Return array of indices of the maximum values along axis 1 from a masked array in NumPy
- Return array of indices of the minimum values along axis 0 from a masked array in NumPy