Numpy MaskedArray.anom()函数| Python
在许多情况下,数据集可能不完整或因存在无效数据而受到污染。例如,传感器可能无法记录数据,或记录无效值。 numpy.ma
模块通过引入掩码数组提供了一种解决此问题的便捷方法。掩码数组是可能缺少或无效条目的数组。
numpy.MaskedArray.anom()
函数计算沿给定轴的异常(与算术平均值的偏差)。它返回一个异常数组,其形状与输入相同,算术平均值沿给定轴计算。
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.
代码#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)
输出:
Input array : [ 1 2 3 -1 5]
Masked array : [1 2 -- -1 5]
Output anomalies array : [-0.75 0.25 -- -2.75 3.25]
代码#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)
输出:
nput array : [10 20 30 40 50]
Masked array : [-- 20 -- 40 50]
Output anomalies array : [-- -16.666666666666664 -- 3.3333333333333357 13.333333333333336]