- 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

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

To return an array formed from the elements of a masked array at the given indices, use the **ma.MaskedArray.take()** method. The "**clip**" 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\n", maskArr) 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 "clip" mode is set using the "mode" parameter. ‘clip’ mode means that all indices that are too large are replaced by the index that addresses the last element along that axis −

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

## 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]]) print("\nOur Masked Array\n", maskArr) 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-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 "clip" mode is set using the "mode" parameter # ‘clip’ mode means that all indices that are too large are replaced by # the index that addresses the last element along that axis print("\nResult...\n",np.take(maskArr, indices, mode='clip'))

## 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 -- 85]

- Related Questions & Answers
- Return an array formed from the elements of a masked array but wrap around range in NumPy
- Return an array formed from the elements of a masked array at the given indices in NumPy
- Return the average of the masked array elements in Numpy
- Return the variance of the masked array elements in Numpy
- Return range of values from the masked array for each row in Numpy
- Return the standard deviation of the masked array elements in NumPy
- Return range of values from a masked array for each column in Numpy
- Return array of indices of the maximum values from a masked array in NumPy
- Return array of indices of the minimum values from a masked array in NumPy
- Copy and return all the elements of a masked array in Numpy
- Return a copy of the masked array in NumPy
- Return the mask of a masked array in Numpy
- Return the average of the masked array elements axis 1 in Numpy
- Return range of values from a masked array along a given axis in NumPy
- Return the transpose of the masked array in NumPy