简体   繁体   中英

Weighted average of dictionary values in python

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.

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

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?

You might want to use a defaultdict instead:

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])

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM