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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。