python中字典值的加权平均值

时间:2021-11-04 21:23:48

I have a grid of parameters which is composed of 234 dictionaries, everyone having the same keys. Then I have a list of weights through which I want to compute a weighted average of such dictionaries' values. In other words I need to obtin just one dictionary that has the same keys of the initial 243, but as values attached to each keys a weighted average of values, using the 243 weights.

我有一个参数网格,由234个字典组成,每个人都有相同的键。然后我有一个权重列表,我想通过它来计算这些词典值的加权平均值。换句话说,我需要只使用一个具有初始243的相同键的字典,但是使用243个权重作为每个键附加值的加权平均值。

I tried to use Counter in order to cumulate results, but it returns very small values that do not make sense to me. w[0] is the list of 243 weights, each of them related to 243 dictionaries inside grid

我尝试使用Counter来累积结果,但它返回的值非常小,对我来说没有意义。 w [0]是243个权重的列表,每个权重与网格内的243个词典相关

from collections import Counter

def avg_grid(grid, w, labels=["V0", "omega", "kappa", "rho", "theta"]):

    avgHeston = Counter({label: 0 for label in labels})

    for i in range(len(grid)):

        avgHeston.update(Counter({label: grid[i][label] * w[0][i] for label in labels}))

    totPar = dict(avgHeston)

    return totPar

Is there an easier way to implement it?

有没有更简单的方法来实现它?

1 个解决方案

#1


0  

You might want to use a defaultdict instead:

您可能希望使用defaultdict:

from collections import defaultdict

def avg_grid(grid, wgts, labels=["V0", "rho"]):

    avgHeston = defaultdict(int)

    for g,w in zip(grid, wgts):
        for label in labels:
            avgHeston[label] += g[label] * w

    return avgHeston

weights = [4,3]
grd = [{'V0':4,'rho':3}, {'V0':1,'rho':2}]

print(avg_grid(grd, weights))

Output:

defaultdict(<class 'int'>, {'V0': 19, 'rho': 18})

Notes:

I have changed w so that you need to pass in a straight list. You might want to call the function like this: avg_grid(grids, w[0])

我已经改变了w,所以你需要传递一个直接列表。您可能想要调用这样的函数:avg_grid(grid,w [0])

Also this doesn't produce an average as such. you may want to do a divide by len(grid) at some point.

这也不会产生平均值。你可能想在某个时候通过len(网格)进行除法。

Also for g,w in zip(grid, wgts): is a more pythonic iteration

同样对于g,w in zip(grid,wgts):是一个更加pythonic迭代

#1


0  

You might want to use a defaultdict instead:

您可能希望使用defaultdict:

from collections import defaultdict

def avg_grid(grid, wgts, labels=["V0", "rho"]):

    avgHeston = defaultdict(int)

    for g,w in zip(grid, wgts):
        for label in labels:
            avgHeston[label] += g[label] * w

    return avgHeston

weights = [4,3]
grd = [{'V0':4,'rho':3}, {'V0':1,'rho':2}]

print(avg_grid(grd, weights))

Output:

defaultdict(<class 'int'>, {'V0': 19, 'rho': 18})

Notes:

I have changed w so that you need to pass in a straight list. You might want to call the function like this: avg_grid(grids, w[0])

我已经改变了w,所以你需要传递一个直接列表。您可能想要调用这样的函数:avg_grid(grid,w [0])

Also this doesn't produce an average as such. you may want to do a divide by len(grid) at some point.

这也不会产生平均值。你可能想在某个时候通过len(网格)进行除法。

Also for g,w in zip(grid, wgts): is a more pythonic iteration

同样对于g,w in zip(grid,wgts):是一个更加pythonic迭代