在許多情況下,數據集可能不完整或被無效數據汙染。例如,傳感器可能無法記錄數據或記錄了無效值。的numpy.ma
模塊通過引入掩碼數組提供了解決此問題的便捷方法。掩碼數組是可能缺少條目或無效條目的數組。
numpy.MaskedArray.anom()函數沿給定軸計算異常(與算術平均值的偏差),它返回異常數組,其形狀與輸入相同,並且沿給定軸計算算術平均值。
用法: numpy.MaskedArray.anom(axis=None, dtype=None)
參數:
axis :[int或None]異常所在的軸。
dtype :[dtype,可選]用於計算方差的類型。
Return :[ndarray]一係列異常。
代碼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]
相關用法
- Python numpy.cov()用法及代碼示例
- Python numpy.copyto()用法及代碼示例
- Python Numpy MaskedArray.any()用法及代碼示例
- Python Numpy MaskedArray.var()用法及代碼示例
- Python Numpy MaskedArray.sum()用法及代碼示例
- Python numpy.issubsctype()用法及代碼示例
- Python Numpy MaskedArray.std()用法及代碼示例
- Python Numpy MaskedArray.mean()用法及代碼示例
- Python Numpy recarray.put()用法及代碼示例
- Python Numpy recarray.ptp()用法及代碼示例
- Python Numpy MaskedArray.all()用法及代碼示例
- Python Numpy MaskedArray.dot()用法及代碼示例
- Python Numpy recarray.all()用法及代碼示例
注:本文由純淨天空篩選整理自jana_sayantan大神的英文原創作品 Numpy MaskedArray.anom() function | Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。