[英]How to simply pass weights while np.average()
我對將權重傳遞給 np.average() function 感到困惑。 下面的例子:
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 。
Output:加權數組,如下所示:
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]
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.