- 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

# Convert inputs to arrays with at least one dimension in Numpy

To convert inputs to arrays with at least one dimension, use the **ma.atleast_1d()** method in Python Numpy. Scalar inputs are converted to 1-dimensional arrays, whilst higher-dimensional inputs are preserved. It returns an array, or list of arrays, each with a.ndim >= 1. Copies are made only if necessary. The function is applied to both the _data and the _mask, if any.

A masked array is the combination of a standard numpy.ndarray and a mask. A mask is either nomask, indicating that no value of the associated array is invalid, or an array of booleans that determines for each element of the associated array whether the value is valid or not.

## 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]) 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, 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)

Convert inputs to arrays with at least one dimension using the ma.atleast_1d() method −

print("

Result...

",np.atleast_1d(1, maskArr))

## Example

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

Result...

",np.atleast_1d(1, maskArr))

## Output

Array... [65 68 81] Array type... int64 Array Dimensions... 1 Our Masked Array [65 -- 81] Our Masked Array type... int64 Our Masked Array Dimensions... 1 Our Masked Array Shape... (3,) Elements in the Masked Array... 3 Result... [array([1]), masked_array(data=[65, --, 81], mask=[False, True, False], fill_value=999999)]

- Related Questions & Answers
- Convert inputs to arrays with at least two dimensions in Numpy
- Convert inputs to arrays with at least three dimensions in Numpy
- Get at least one match in list querying with MongoDB?
- Reduce array's dimension by one in Numpy
- How to remove rows that contain at least one 0 in R?
- How to extract columns having at least one non-duplicate in R?
- Return a copy of the masked array collapsed into one dimension in Numpy
- How to check if a string has at least one letter and one number in Python?
- Reduce array's dimension by one but initialize the reduction with a different value in Numpy
- How to aggregate two lists if at least one element matches in MongoDB?
- Count divisors of n that have at-least one digit common with n in Java
- Broadcasting with NumPy Arrays in Python
- C# program to check if two lists have at-least one element common
- Python program to check if two lists have at least one common element
- Shortest Subarray with Sum at Least K in C++