- 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

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

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

The method returns an array, or list of arrays, each with a.ndim >= 2. Copies are avoided where possible, and views with two or more dimensions are returned.

## 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 two dimensions, use the ma.atleast_2d() method in Python Numpy −

print("

Result...

",np.atleast_2d(1, maskArr))

## Example

# Python ma.MaskedArray - Convert inputs to arrays with at least two dimensions 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 two dimensions, use the ma.atleast_2d() method in Python Numpy print("

Result...

",np.atleast_2d(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 three dimensions in Numpy
- Convert inputs to arrays with at least one dimension in Numpy
- Get the Kronecker product of two arrays with different dimensions in Python
- Subarray sum with at least two elements in JavaScript
- Design a DFA of a string with at least two 0’s and at least two 1’s
- Return the cross product of two (arrays of) vectors with different dimensions in Python
- Matrix product of two arrays in Numpy
- How to find intersection between two Numpy arrays?
- How to create a function in R with two inputs?
- Broadcasting with NumPy Arrays in Python
- How to find set difference between two Numpy arrays?
- Return the ceil of the inputs in Numpy
- Return the outer product of two masked arrays with different shapes in Numpy
- Return the inner product of two masked arrays with different shapes in Numpy
- Compare two Numpy arrays and return the element-wise maximum with fmax()