Numpy MaskedArray.sum() function | Python

numpy.MaskedArray.median() function is used to compute the sum of the masked array elements over the given axis.

Syntax : numpy.ma.sum(arr, axis=None, dtype=None, out=None, keepdims=False)

Parameters:



arr : [ ndarray ] Input masked array.
axis :[ int, optional] Axis along which the sum is computed. The default (None) is to compute the sum over the flattened array.
dtype : [dtype, optional] Type of the returned array, as well as of the accumulator in which the elements are multiplied.
out : [ndarray, optional] A location into which the result is stored.
  -> If provided, it must have a shape that the inputs broadcast to.
  -> If not provided or None, a freshly-allocated array is returned.
keepdims :[ bool, optional] If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

Return : [sum_along_axis, ndarray] A new array holding the result is returned unless out is specified, in which case a reference to out is returned.

Code #1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program explaining
# numpy.MaskedArray.sum() method 
    
# importing numpy as geek  
# and numpy.ma module as ma 
import numpy as geek 
import numpy.ma as ma 
    
# creating input array  
in_arr = geek.array([[1, 2], [ 3, -1], [ 5, -3]])
print ("Input array : ", in_arr) 
    
# Now we are creating a masked array. 
# by making  entry as invalid.  
mask_arr = ma.masked_array(in_arr, mask =[[1, 0], [ 1, 0], [ 0, 0]]) 
print ("Masked array : ", mask_arr) 
    
# applying MaskedArray.sum    
# methods to masked array
out_arr = ma.sum(mask_arr) 
print ("sum of masked array along default axis : ", out_arr) 

chevron_right


Output:

Input array :  [[ 1  2]
 [ 3 -1]
 [ 5 -3]]
Masked array :  [[-- 2]
 [-- -1]
 [5 -3]]
sum of masked array along default axis :  3

 

Code #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program explaining
# numpy.MaskedArray.sum() method 
     
# importing numpy as geek  
# and numpy.ma module as ma 
import numpy as geek 
import numpy.ma as ma 
     
# creating input array 
in_arr = geek.array([[1, 0, 3], [ 4, 1, 6]]) 
print ("Input array : ", in_arr)
      
# Now we are creating a masked array. 
# by making one entry as invalid.  
mask_arr = ma.masked_array(in_arr, mask =[[ 0, 0, 0], [ 0, 0, 1]]) 
print ("Masked array : ", mask_arr) 
     
# applying MaskedArray.sum methods 
# to masked array
out_arr1 = ma.sum(mask_arr, axis = 0
print ("sum of masked array along 0 axis : ", out_arr1)
  
out_arr2 = ma.sum(mask_arr, axis = 1
print ("sum of masked array along 1 axis : ", out_arr2)

chevron_right


Output:

Input array :  [[1 0 3]
 [4 1 6]]
Masked array :  [[1 0 3]
 [4 1 --]]
sum of masked array along 0 axis :  [5 1 3]
sum of masked array along 1 axis :  [4 5]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.