Return the indices of unmasked elements that are not zero in NumPy

To return the indices of unmasked elements that are not zero, use the ma.MaskedArray.nonzero()

Returns a tuple of arrays, one for each dimension, containing the indices of the non-zero elements in that dimension. The corresponding non-zero values can be obtained with −

a[a.nonzero()]

To group the indices by element, rather than dimension, use instead −

np.transpose(a.nonzero())

The result of this is always a 2d array, with a row for each non-zero element.

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([[55, 85, 59, 77], [67, 33, 39, 57], [29, 88, 51, 37], [56, 45, 99, 85]])
print("Array...<br>", arr)
print("\nArray type...<br>", arr.dtype)

Get the dimensions of the Array −

print("Array Dimensions...<br>",arr.ndim)

Create a masked array and mask some of them as invalid −

maskArr = ma.masked_array(arr, mask =[[1, 1, 0, 0], [ 0, 0, 1, 0], [0, 0, 0, 1], [0, 1, 0, 0]])
print("\nOur Masked Array<br>", maskArr)
print("\nOur Masked Array type...<br>", maskArr.dtype)

Get the dimensions of the Masked Array −

print("\nOur Masked Array Dimensions...<br>",maskArr.ndim)

Get the shape of the Masked Array −

print("\nOur Masked Array Shape...<br>",maskArr.shape)

Get the number of elements of the Masked Array −

print("\nElements in the Masked Array...<br>",maskArr.size)

To return the indices of unmasked elements that are not zero, use the ma.MaskedArray.nonzero() method in Numpy. Returns a tuple of arrays, one for each dimension, containing the indices of the non-zero elements in that dimension −

print("\nResult...<br>",maskArr.nonzero())

Example

import numpy as np
import numpy.ma as ma

# Create an array with int elements using the numpy.array() method
arr = np.array([[55, 85, 59, 77], [67, 33, 39, 57], [29, 88, 51, 37], [56, 45, 99, 85]])
print("Array...<br>", arr)
print("\nArray type...<br>", arr.dtype)

# Get the dimensions of the Array
print("\nArray Dimensions...<br>",arr.ndim)

# Create a masked array and mask some of them as invalid
maskArr = ma.masked_array(arr, mask =[[1, 1, 0, 0], [ 0, 0, 1, 0], [0, 0, 0, 1], [0, 1, 0, 0]])
print("\nOur Masked Array<br>", maskArr)
print("\nOur Masked Array type...<br>", maskArr.dtype)

# Get the dimensions of the Masked Array
print("\nOur Masked Array Dimensions...<br>",maskArr.ndim)

# Get the shape of the Masked Array
print("\nOur Masked Array Shape...<br>",maskArr.shape)

# Get the number of elements of the Masked Array
print("\nElements in the Masked Array...<br>",maskArr.size)

# To return the indices of unmasked elements that are not zero, use the ma.MaskedArray.nonzero() method in Numpy
# Returns a tuple of arrays, one for each dimension, containing the indices of the non-zero elements in that dimension.
print("\nResult...<br>",maskArr.nonzero())

Output

Array...
[[55 85 59 77]
[67 33 39 57]
[29 88 51 37]
[56 45 99 85]]

Array type...
int64

Array Dimensions...
2

Our Masked Array
[[-- -- 59 77]
[67 33 -- 57]
[29 88 51 --]
[56 -- 99 85]]

Our Masked Array type...
int64

Our Masked Array Dimensions...
2

Our Masked Array Shape...
(4, 4)

Elements in the Masked Array...
16

Result...
(array([0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3]), array([2, 3, 0, 1, 3, 0, 1, 2, 0, 2, 3]))
Updated on: 2022-02-05T07:29:12+05:30

363 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements