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

Updated on: 03-Feb-2022

179 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements