这个不同维度的加权平均具体是怎么算的呀

a是([[1,2,3],
[4,5,6]])的二维numpy数组

img

https://wenku.baidu.com/view/ddd4bfd807a1b0717fd5360cba1aa81144318f0e.html

权重数组weights=[xx,xx,xx]要与原数组的行列相同

>>> import numpy as np
>>> a = np.array([[1, 2, 3],[4, 5, 6]])
>>> a
array([[1, 2, 3],
       [4, 5, 6]])
>>> np.average(a, weights=[[3,2,2],[1,1,1]])
2.8
>>> 1*0.3+2*0.2+3*0.2+15*0.1
2.8
>>> 

不同形的话,报错:
np.average(a, weights=[[3,2,2,1,1,1]])
Traceback (most recent call last):
File " <pyshell#25>", line 1, in <module>
np.average(a, weights=[[3,2,2,1,1,1]])
File "<__array_function__ internals>", line 5, in average
File "D:\Python\lib\site-packages\numpy\lib\function_base.py", line 393, in average
raise TypeError(
TypeError: Axis must be specified when shapes of a and weights differ.