What is the numpy.ma.masked_object in NumPy?
Overview
The masked_object() function in NumPy is used to mask a given array where the specified value is equal to the values of the array.
Syntax
This is the syntax of the masked_object() function:
ma.masked_object(x, value, copy=True, shrink=True)
Syntax of the masked_object() function in NumPy
Parameter values
The masked_object()function takes the following parameter values:
x: This is the input array that needs to be masked. This is a required parameter value.value: This is the value that needs to be compared to the values of the input array. This is a required parameter value.copy: This parameter takes abooleanvalue, which indicates whether a copy of the input array should be returned or not. This is an optional parameter value.shrink: This parameter takes abooleanvalue, which indicates whether the masked array full ofFalseis collapsed to no mask or not. This is an optional parameter value.
Return value
The masked_object() function returns a masked array.
Example
# A code to illustrate the masked_object() function# importing the necessary librariesimport numpy as npimport numpy.ma as ma# creating an input arraymy_array = np.array([1, 2, 3, 4, 5, 2])# masking the array where values are equal to 2mask_array =ma.masked_object(my_array, 2)print(mask_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_equal()function to mask the values of the arrays that are equal to2. We assign the result to a variable calledmask_array. - Line 13: We print the masked array,
mask_array, to the console.