Tuesday, October 7, 2025
HomeLanguagesNumpy MaskedArray.anom() function | Python

Numpy MaskedArray.anom() function | Python

In many circumstances, datasets can be incomplete or tainted by the presence of invalid data. For example, a sensor may have failed to record a data, or recorded an invalid value. The numpy.ma module provides a convenient way to address this issue, by introducing masked arrays.Masked arrays are arrays that may have missing or invalid entries.

numpy.MaskedArray.anom() function Compute the anomalies (deviations from the arithmetic mean) along the given axis.It returns an array of anomalies, with the same shape as the input and where the arithmetic mean is computed along the given 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
 
# 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])
print ("Input array : ", in_arr)
 
# Now we are creating a masked array
# by making third entry as invalid.
mask_arr = ma.masked_array(in_arr, mask =[0, 0, 1, 0, 0])
print ("Masked array : ", mask_arr)
 
# applying MaskedArray.anom methods to mask 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
 
# 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([10, 20, 30, 40, 50])
print ("Input array : ", in_arr)
 
# Now we are creating a masked array by making
# first and third entry as invalid.
mask_arr = ma.masked_array(in_arr, mask =[1, 0, 1, 0, 0])
print ("Masked array : ", mask_arr)
 
# applying MaskedArray.anom methods to mask array
out_arr = mask_arr.anom()
print ("Output anomalies array : ", out_arr)


Output:

input array :  [10 20 30 40 50]
Masked array :  [-- 20 -- 40 50]
Output anomalies array :  [-- -16.666666666666664 -- 3.3333333333333357 13.333333333333336]
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32340 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6708 POSTS0 COMMENTS
Nicole Veronica
11872 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6829 POSTS0 COMMENTS
Ted Musemwa
7090 POSTS0 COMMENTS
Thapelo Manthata
6780 POSTS0 COMMENTS
Umr Jansen
6784 POSTS0 COMMENTS