Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

numpy
data science
python
arrays
communitycreator

How to mask an array inside a given interval in NumPy

Onyejiaku Theophilus Chidalu

Overview

The masked_inside() function in NumPy is used to mask an array inside a given interval.

Syntax

This is the syntax of the masked_inside() function:

ma.masked_inside(x, v1, v2)
Syntax of the ma.masked_inside() function

Parameter values

The masked_inside() function takes the following parameter values:

  • x: This is the input array.
  • v1 and v2 : These represent the boundaries for which the values of the given array are to be masked. Both values are not excluded when masking.

Return value

The masked_inside() function returns a masked array with values that fall within the specified intervals that are masked.

Example

# A code to illustrate the masked_inside() function 

# importing the necessary libraries
import numpy as np
import numpy.ma as ma

# creating an input array
my_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

# masking the values starting from 3 to 7
mask_array =ma.masked_inside(my_array, 3, 7)

print(mask_array)
Masking an array

Explanation

  • Lines 4–5: We import the necessary library and module.
  • Line 8: We create an input array called my_array.
  • Line 11: We use the masked_greater_equal() function to mask the values from 3 to 7. We assign the result to a variable called mask_array.
  • Line 13: We print the value of the masked array, mask_array, to the console.

RELATED TAGS

numpy
data science
python
arrays
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring