- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- 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

# Convert inputs to arrays with at least three dimensions in Numpy

To convert inputs to arrays with at least three dimensions, use the **ma.atleast_3d()** method in Python Numpy. The parameters are one or more array-like sequences. Non-array inputs are converted to arrays. Arrays that already have three or more dimensions are preserved.

The function returns an array, or list of arrays, each with a.ndim >= 3. Copies are avoided where possible, and views with three or more dimensions are returned. For example, a 1-D array of shape (N,) becomes a view of shape (1, N, 1), and a 2-D array of shape (M, N) becomes a view of shape (M, N, 1). It is applied to both the _data and the _mask, if any.

## 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([[65, 68, 81], [93, 33, 39], [73, 88, 51], [62, 45, 67]]) 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 =[[1, 1, 0], [ 1, 0, 0], [0, 1, 0], [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)

To convert inputs to arrays with at least three dimensions, use the ma.atleast_3d() method in Python Numpy −

print("

Result...

",np.atleast_3d(1, maskArr))

## Example

import numpy as np import numpy.ma as ma # Create an array with int elements using the numpy.array() method arr = np.array([[65, 68, 81], [93, 33, 39], [73, 88, 51], [62, 45, 67]]) 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 =[[1, 1, 0], [ 1, 0, 0], [0, 1, 0], [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) # To convert inputs to arrays with at least three dimensions, use the ma.atleast_3d() method in Python Numpy print("

Result...

",np.atleast_3d(1, maskArr))

## Output

Array... [[65 68 81] [93 33 39] [73 88 51] [62 45 67]] Array type... int64 Array Dimensions... 2 Our Masked Array [[-- -- 81] [-- 33 39] [73 -- 51] [62 -- 67]] Our Masked Array type... int64 Our Masked Array Dimensions... 2 Our Masked Array Shape... (4, 3) Elements in the Masked Array... 12 Result... [array([[[1]]]), masked_array( data=[[[--], [--], [81]], [[--], [33], [39]], [[73], [--], [51]], [[62], [--], [67]]], mask=[[[ True], [ True], [False]], [[ True], [False], [False]], [[False], [ True], [False]], [[False], [ True], [False]]], fill_value=999999)]

- Related Articles
- Convert inputs to arrays with at least two dimensions in Numpy
- Convert inputs to arrays with at least one dimension in Numpy
- Mention at least three causes of deforestation.
- Broadcasting with NumPy Arrays in Python
- Return the ceil of the inputs in Numpy
- Name at least three commonly used units of energy.
- Return the inner product of two masked Three Dimensional arrays in Numpy
- Return the truncated value of the inputs in Numpy
- Return the outer product of two masked Three-Dimensional Numpy arrays
- Return numbers spaced evenly on a geometric progression but with negative inputs in Numpy
- Return numbers spaced evenly on a geometric progression but with complex inputs in Numpy
- Compute the tensor dot product for arrays with different dimensions in Python
- Get the Kronecker product of two arrays with different dimensions in Python
- Get the Masked Array Dimensions in Numpy
- Compute the tensor dot product for arrays with different dimensions with double contraction in Python