Python | Numpy MaskedArray .__ sub__

Arrays | NumPy | Python Methods and Functions

numpy.ma.MaskedArray class is a subclass of ndarray for manipulating numeric arrays with missing data. With Numpy MaskedArray .__ sub__ we can subtract a specific value that is provided as a parameter in the MaskedArray .__ sub __ () method. The value will be subtracted from each item in the numpy array.

Syntax: numpy.MaskedArray .__ sub__

Return: Subtract others from self, and return a new masked array.

Example # 1:
In this example, we see that each element of the array is subtracted from the value specified as a parameter in the MaskedArray .__ sub __ () method. Remember one thing, this will not work for double type values.

# import important module into python

import numpy as np 

 
# make an array with NumPy

gfg = np.ma.array ([ 11 , 22 , 23 , 24 , 25 ]) 

 
# applying MaskedArray .__ sub __ () method

print < / code> (gfg .__ sub __ ( 5 )) 

Exit:

 [6 17 18 19 20]  

Example # 2:

# import important module into python

import numpy as np 

 
# make an array with NumPy

gfg = np.ma.array ([[ 21 , 22 , 23 , 24 , 25 ], 

  [ 26 , 25 , 24 , 23 , 22 ]]) 

 
# applying MaskedArray .__ sub __ () method

print (gfg .__ sub __ ( 5 )) 

Output:

 [[16 17 18 19 20] [21 20 19 18 17]] 




Get Solution for free from DataCamp guru