Numpy MaskedArray.anom () Function | python

NumPy | Python Methods and Functions

numpy.MaskedArray.anom() Calculates anomalies (deviations from the arithmetic mean) along the specified axis. Returns an array of anomalies that have the same shape as the input and where the arithmetic mean is calculated along the specified axis.

Syntax: numpy.MaskedArray. anom (axis = None, dtype = None)

Parameters:
axis: [int or None] Axis over which the anomalies are taken.
dtype: [dtype, optional] Type to use in computing the variance.

Return: [ndarray ] an array of anomalies.

Code # 1:

# Python program, explaining
# numpy.MaskedArray.anom () method

 
# import numy as geek
# and numpy.ma module as ma

 import numpy as geek

import numpy. ma as ma

 
# create input array

in_arr = geek.array ([ 1 , 2 , 3 , - 1 , 5 ])

print ( "Input array:" , in_arr)

 
# Now we create a masked array
# making the third the entry is invalid.

mask_arr = ma. masked_array (in_arr, mask = [ 0 , 0 , 1 , 0 , 0 ])

print ( "Masked array:" , mask_arr)

  
# using MaskedArray.anom methods to mask the array

out_arr = mask_arr.anom ()

print ( "Output anomalies array:" , out_arr)

Output :

 Input array: [1 2 3 -1 5] Masked array: [1 2 - -1 5] Output anomalies array : [-0.75 0.25 - -2.75 3.25] 

Code # 2:

# Python program explaining
# numpy.MaskedArray.anom () method

  
# import numy as geek
# and numpy.ma module as ma

import numpy as geek

import numpy.ma as ma

 
# create input array

in_arr = geek.array ([ 10 , 20 , 30 , 40 , 50 ])

print ( "Input array:" , in_arr)

 
# We now create a masked array by making
# the first and third entries are invalid.

mask_arr = ma.masked_array (in_arr , mask = [ 1 , 0 , 1 , 0 , 0 ])

print ( "Masked array:" , mask_arr)

 
# using MaskedArray.anom methods to mask an array

out_arr = mask_arr.anom ()

print ( "Output anomalies array:" , out_arr)

Output :

 nput array: [10 20 30 40 50] Masked array: [- 20 - 40 50] Output anomalies array: [ - -16.666666666666664 - 3.3333333333333 357 13.333333333333336] 




Get Solution for free from DataCamp guru