The pythonic way of calculating the average value and variance of values โ€‹โ€‹in counters

I am wondering if there is a Pythonic way to calculate the means and deviations of the counters?

For example, I have four counters that use the same keys:

a = Counter({1: 23, 2: 39, 3: 1}) b = Counter({1: 28, 2: 39, 3: 1}) c = Counter({1: 23, 2: 39, 3: 2}) d = Counter({1: 23, 2: 22, 3: 1}) 

My way to do this is:

 each_key_val = {} for i in a.keys(): # The assumption here is that all Counters must share the same keys for j in [a, b, c, d]: try: each_key_val[i].append(j[i]) except: each_key_val[i] = [j[i]] 

I could use the following code to find the average / variance for each key:

  np.mean(each_key_val[i]) np.var(each_key_val[i]) 

Is there an easier way to calculate the average / variance for each key compared to my method?

+5
source share
1 answer

This is not what I consider the following more readable than what you have, but it only uses lists.

Say what you have

 cs = (a, b, c, d) 

Then the average vocabulary can be found using

 m = {k: float(d) / len(cs) for k, d in sum(cs).iteritems()} 

For variance, note that in the definition of variance V [X] = E [x 2 ] - (E [X]) 2 therefore, if you define:

 p = sum([Counter({k: ((float(d**2) / len(cs))) for (k, d) in cn.iteritems()}) \ for cn in cs]) 

then the variance dictionary

 {k: p[k] - m[k]**2 for k in m} 
+3
source

All Articles