如何在 np.average() 时简单地传递权重

How to simply pass weights while np.average()

我对 将权重 传递给 np.average() 函数感到困惑。示例如下:

import numpy as np

weights = [0.35, 0.05, 0.6]
abc = list()

a = [[ 0.5,  1],
   [ 5,  7],
   [ 3,  8]]

b = [[ 10,  1],
   [ 0.5,  1],
   [ 0.7,  0.2]]

c = [[ 10,  12],
   [ 0.5,  13],
   [ 5,  0.7]]

abc.append(a)
abc.append(b)
abc.append(c)

print(np.average(np.array(abc), weights=[weights], axis=0))

OUT:
TypeError: 1D weights expected when shapes of a and weights differ.

我知道形状不同,但如何在不做的情况下简单地添加权重列表

np.average(np.array(abc), weights=[weights[0], weights[1], weights[2]], ..., axis=0)

因为我正在执行一个循环,其中权重与大小的差异最大为 30

输出:像这样的加权数组:

OUT:
[[6.675,  7.6],
[ 2.075,  10.3],
[ 4.085,  3.23]]

*average(a * weights[0] + b * weights[1] + c * weights[2])*

欢迎任何其他解决方案。

不确定第一个元素怎么会是 4.675?

weights = [0.35, 0.05, 0.6]


a = [[ 0.5,  1],
   [ 5,  7],
   [ 3,  8]]

b = [[ 10,  1],
   [ 0.5,  1],
   [ 0.7,  0.2]]

c = [[ 10,  12],
   [ 0.5,  13],
   [ 5,  0.7]]

abc=[a, b, c]

print(np.average(np.array(abc), weights=weights,axis=0))

您的 abc 数组的形状为 (1, 3, 3, 2)。所以要么改变 axis=1 要么像@BingWang 建议的那样使用 abc = [a, b, c]