首页 > 代码库 > numpy加权平均

numpy加权平均

1 import numpy as np
2 a = np.arange(15).reshape(3,5)
3 a

array([[ 0, 1, 2, 3, 4],
      [ 5, 6, 7, 8, 9],
      [10, 11, 12, 13, 14]])

np.average(a, axis=0,weights=(10, 5, 1))

array([ 2.1875,  3.1875,  4.1875,  5.1875,  6.1875])

axis=0 对最外层维度元素计算, weights=(10, 5, 1) 给投的权重分别为10 ,5 , 1

[ 2.1875,  3.1875,  4.1875,  5.1875,  6.1875]  =  ( 10*[ 0, 1, 2, 3, 4] + 5*[ 5, 6, 7, 8, 9] + 1*[10, 11, 12, 13, 14]) / (10+5+1)

 

numpy加权平均