Numpy MaskedArray.average() function | Python

numpy.MaskedArray.average() function is used to return the weighted average of array over the given axis.

Syntax : numpy.ma.average(arr, axis=None, weights=None, returned=False)

Parameters:



arr :[ array_like] Input masked array whose data to be averaged. Masked entries are not taken into account in the computation.
axis :[ int, optional] Axis along which to average arr. If None, averaging is done over the flattened array.
weights : [array_like, optional] The importance that each element has in the computation of the average. If weights=None, then all data in arr are assumed to have a weight equal to one. If weights is complex, the imaginary parts are ignored.
returned :[ bool, optional] It indicates whether a tuple (result, sum of weights) should be returned as output (True), or just the result (False). Default is False.

Return : [ scalar or MaskedArray] The average along the specified axis. When returned is True, return a tuple with the average as the first element and the sum of the weights as the second element.

Code #1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program explaining
# numpy.MaskedArray.average() 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.average    
# methods to masked array
out_arr = ma.average(mask_arr) 
print ("normal average of masked array : ", out_arr) 

chevron_right


Output:

Input array :  [[ 1  2]
 [ 3 -1]
 [ 5 -3]]
Masked array :  [[-- 2]
 [-- -1]
 [5 -3]]
normal average of masked array :  0.75

 

Code #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program explaining
# numpy.MaskedArray.average() 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.average    
# methods to masked array
out_arr = ma.average(mask_arr, weights =[[0, 1], [ 0, 2], [ 3, 1]]) 
print ("weighted average of masked array : ", out_arr) 

chevron_right


Output:

Input array :  [[ 1  2]
 [ 3 -1]
 [ 5 -3]]
Masked array :  [[-- 2]
 [-- -1]
 [5 -3]]
weighted average of masked array :  1.7142857142857142



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.