当前位置: 代码迷 >> 综合 >> 加权平均np.average()
  详细解决方案

加权平均np.average()

热度:33   发布时间:2023-12-18 15:20:24.0

定义:若有n个数  的权分别是 ,那么 

叫做这n个数的加权平均值。如果权重相同,加权平均值即为平均值

def weighted_average(datalist,weigth=1):numerator=0if weigth !=1:denominator=sum(weigth)assert len(datalist) == len(weigth), "length not equal"for i in range(len(datalist)):numerator+=datalist[i]*weigth[i]else:denominator = len(datalist)numerator=sum(datalist)return numerator/denominatorif __name__=='__main__':datalist=list(range(1,11))weigth=list(range(10,0,-1))num=weighted_average(datalist,weigth)print(num)

 

  相关解决方案