Return an array formed from the elements of a masked array at the given indices in NumPy


To return an array formed from the elements of a masked array at the given indices, use the ma.MaskedArray.take() method in Python Numpy.

The take() method’s returned array has the same type as array. The indices parameter is the indices of the values to extract. The axis parameter is the axis over which to select values. By default, the flattened input array is used. The out parameter, if provided, the result will be placed in this array. It should be of the appropriate shape and dtype. Note that out is always buffered if mode=’raise’; use other modes for better performance.

The mode parameter specifies how out-of-bounds indices will behave.

  • ‘raise’ - raise an error (default)

  • ‘wrap’ - wrap around

  • ‘clip’ - clip to the range

‘clip’ mode means that all indices that are too large are replaced by the index that addresses the last element along that axis.

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([[49, 85, 45], [67, 33, 59]])
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, 0, 1], [ 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)

The given indices i.e. the indices of the values to extract −

indices = [4, 5, 8, 13, 14]

To return an array formed from the elements of a masked array at the given indices, use the ma.MaskedArray.take() method −

print("
Result...
",np.take(maskArr, indices))

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, 68, 84], [67, 33, 39, 53], [29, 88, 51, 37], [56, 45, 99, 85]])
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, 0], [ 0, 0, 1, 0], [0, 0, 0, 1], [0, 1, 0, 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) # The given indices i.e. the indices of the values to extract indices = [4, 5, 8, 13, 14] # To return an array formed from the elements of a masked array at the given indices, use the take() method print("
Result...
",np.take(maskArr, indices))

Output

Array...
[[55 85 68 84]
[67 33 39 53]
[29 88 51 37]
[56 45 99 85]]

Array type...
int64

Array Dimensions...
2

Our Masked Array
[[-- -- 68 84]
[67 33 -- 53]
[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...
[67 33 29 -- 99]

Updated on: 04-Feb-2022

470 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements