Pythonic way to calculate the mean and variance of values in Counters

datadatadata

I'm wondering whether there is a Pythonic way to compute the means and variances of Counters?

For example, I have four Counters sharing 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 that 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 mean / variance for each key:

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

Is there an easier way to compute the mean / variance for each key compared to my way?

Ami Tavory

It's not that I think the following is more readable than what you have, but it only uses list comprehensions.

Say you have

cs = (a, b, c, d)

Then a dictionary of the mean can be found with

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

For the variance, note that, by the definition of variance V[X] = E[x2] - (E[X])2, so, 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 is

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pandas - Calculate Mean and Variance

From Dev

Pythonic way to default loop counters

From Dev

Calculate Mean and Variance of function output

From Dev

How to calculate/normalize Zero mean and unit variance

From Dev

Using AWK to calculate mean and variance of columns

From Dev

Dplyr calculate mean and variance without all the data

From Dev

Calculate moments (mean, variance) of distribution in python

From Dev

How could I calculate the variance with specific mean

From Dev

What is the pythonic way to calculate argmax?

From Dev

Calculate mean of calculated values

From Dev

Calculate mean difference values

From Dev

Calculate the mean of values in a loop

From Dev

pythonic way of setting values in a dictionary

From Dev

pythonic way of reassigning values to dictionary

From Dev

Replacing values in dataframe in a Pythonic way

From Dev

Resample and select a given number of rows and calculate the mean, variance and confidence intervals?

From Dev

How to calculate mean and variance from pandas datetime object?

From Dev

Calculate mean, variance, covariance of different length matrices in a split list

From Dev

To calculate the mean and variance of a column of subset of a data using awk

From Dev

How to calculate mean/variance/standard deviation per index of array?

From Dev

How to simulate 50 random samples and calculate mean and variance of each sample

From Dev

Pythonic way to calculate streaks in pandas dataframe

From Dev

Calculate the mean values if the numbers are same

From Dev

Pythonic way to map enum to api values

From Dev

Pythonic way to populate a dictionary with count of values

From Python

Pythonic way to replace values in a list according to a dictionary

From Dev

How to Invert column values in pandas - pythonic way?

From Java

Test if all values are in an iterable in a pythonic way

From Dev

Pythonic way to check empty dictionary and empty values

Related Related

  1. 1

    Pandas - Calculate Mean and Variance

  2. 2

    Pythonic way to default loop counters

  3. 3

    Calculate Mean and Variance of function output

  4. 4

    How to calculate/normalize Zero mean and unit variance

  5. 5

    Using AWK to calculate mean and variance of columns

  6. 6

    Dplyr calculate mean and variance without all the data

  7. 7

    Calculate moments (mean, variance) of distribution in python

  8. 8

    How could I calculate the variance with specific mean

  9. 9

    What is the pythonic way to calculate argmax?

  10. 10

    Calculate mean of calculated values

  11. 11

    Calculate mean difference values

  12. 12

    Calculate the mean of values in a loop

  13. 13

    pythonic way of setting values in a dictionary

  14. 14

    pythonic way of reassigning values to dictionary

  15. 15

    Replacing values in dataframe in a Pythonic way

  16. 16

    Resample and select a given number of rows and calculate the mean, variance and confidence intervals?

  17. 17

    How to calculate mean and variance from pandas datetime object?

  18. 18

    Calculate mean, variance, covariance of different length matrices in a split list

  19. 19

    To calculate the mean and variance of a column of subset of a data using awk

  20. 20

    How to calculate mean/variance/standard deviation per index of array?

  21. 21

    How to simulate 50 random samples and calculate mean and variance of each sample

  22. 22

    Pythonic way to calculate streaks in pandas dataframe

  23. 23

    Calculate the mean values if the numbers are same

  24. 24

    Pythonic way to map enum to api values

  25. 25

    Pythonic way to populate a dictionary with count of values

  26. 26

    Pythonic way to replace values in a list according to a dictionary

  27. 27

    How to Invert column values in pandas - pythonic way?

  28. 28

    Test if all values are in an iterable in a pythonic way

  29. 29

    Pythonic way to check empty dictionary and empty values

HotTag

Archive