numpy.MaskedArray.average()函數用於返回給定軸上數組的加權平均值。
用法: numpy.ma.average(arr, axis=None, weights=None, returned=False)
參數:
arr :[數組]輸入要對其數據求平均的掩碼數組。在計算中不考慮屏蔽的條目。
axis :[int,可選]平均arr的軸。如果為None,則對展平的數組進行平均。
weights : [數組,可選]每個元素在平均值計算中的重要性。如果weights = None,則假定arr中的所有數據的權重等於1。如果權重很複雜,則虛部將被忽略。
returned :[bool,可選]它指示是否應將元組(結果,權重之和)作為輸出(真)或僅作為結果(假)返回。默認值為False。
返回:[標量或MaskedArray]沿指定軸的平均值。當返回True時,返回一個以平均值為第一個元素,權重之和為第二個元素的元組。
代碼1:
# 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)
輸出:
Input array : [[ 1 2] [ 3 -1] [ 5 -3]] Masked array : [[-- 2] [-- -1] [5 -3]] normal average of masked array : 0.75
代碼2:
# 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)
輸出:
Input array : [[ 1 2] [ 3 -1] [ 5 -3]] Masked array : [[-- 2] [-- -1] [5 -3]] weighted average of masked array : 1.7142857142857142
相關用法
- Python numpy.cov()用法及代碼示例
- Python Numpy recarray.mean()用法及代碼示例
- Python Numpy MaskedArray.any()用法及代碼示例
- Python Numpy recarray.ptp()用法及代碼示例
- Python Numpy recarray.max()用法及代碼示例
- Python Numpy recarray.min()用法及代碼示例
- Python Numpy recarray.dot()用法及代碼示例
- Python Numpy MaskedArray.var()用法及代碼示例
- Python numpy.nanmean()用法及代碼示例
- Python Numpy MaskedArray.mean()用法及代碼示例
- Python Numpy recarray.any()用法及代碼示例
- Python numpy.issubsctype()用法及代碼示例
- Python Numpy recarray.all()用法及代碼示例
- Python numpy.issubdtype()用法及代碼示例
- Python Numpy MaskedArray.sum()用法及代碼示例
注:本文由純淨天空篩選整理自jana_sayantan大神的英文原創作品 Numpy MaskedArray.average() function | Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。