# Return an array formed from the elements of a masked array but wrap around range in NumPy

NumpyServer Side ProgrammingProgramming

#### Python Data Science basics with Numpy, Pandas and Matplotlib

Most Popular

63 Lectures 6 hours

#### Data Analysis using NumPy and Pandas

19 Lectures 8 hours

#### Numpy with Python

Most Popular

12 Lectures 3 hours

To return an array formed from the elements of a masked array at the given indices, use the ma.MaskedArray.take() method. The "wrap" mode is set using the "mode" parameter.

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...\n", arr)
print("\nArray type...\n", arr.dtype)

Get the dimensions of the Array −

print("Array Dimensions...\n",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("\nOur Masked Array type...\n", maskArr.dtype)

Get the dimensions of the Masked Array −

print("\nOur Masked Array Dimensions...\n",maskArr.ndim)


Get the shape of the Masked Array −

print("\nOur Masked Array Shape...\n",maskArr.shape)

Get the number of elements of the Masked Array −

print("\nElements in the Masked Array...\n",maskArr.size)


The given indices i.e. the indices of the values to extract.We have set an index here i.e, out-ofbounds −

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

To return an array formed from the elements of a masked array at the given indices, use the take() method. The "wrap" mode is set using the "mode" parameter −

print("\nResult...\n",np.take(maskArr, indices, mode='wrap'))


## 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...\n", arr)
print("\nArray type...\n", arr.dtype)

# Get the dimensions of the Array
print("\nArray Dimensions...\n",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]])

# Get the dimensions of the Masked Array

# Get the shape of the Masked Array

# Get the number of elements of the Masked Array

# The given indices i.e. the indices of the values to extract
# We have set an index here i.e, out-of-bounds
indices = [4, 5, 8, 13, 25]

# To return an array formed from the elements of a masked array at the given indices, use the take() method
# The "wrap" mode is set using the "mode" parameter
print("\nResult...\n",np.take(maskArr, indices, mode='wrap'))

## Output

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

Array type...
int64

Array Dimensions...
2

[[-- -- 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 -- 88]