lulu tieungu
lulu tieungu

Reputation: 31

Finding variance list of lists python

I am new to python, and I need help with finding average and variances in a list of lists. I have a list of lists like this:

data = [[1, 2, 3], [4, 5, 6], [7, 8, 9],
        [10, 11, 12]]

and I want to find the variances output like this:

[var_column_1, var_column_2, var_column_3, var_column_4]

I am able to find the average but for the variances, I have no luck on finding a solution. Here is my code:

def avg(allgroups):
    return [float(sum(i))/len(i) for i in zip(*allgroups)]
def variance(allgroups):
    summm = 0.0
    for i in zip(*allgroups):
        summm = summm + (i-avg(allgroups))**2
    return summm / (len(allgroups)-1)
    TheAvg=avg(allgroups)
    print(TheAvg)
    Variance=variance(allgroups)
    print(Variance)

I keep getting the screen error:

*summm = summm + (i-avg(allgroups))**2
TypeError: unsupported operand type(s) for -: 'tuple' and 'list'*

I will be grateful for any help. P/S: I can't use numpy for this problem.

Upvotes: 1

Views: 925

Answers (3)

alec
alec

Reputation: 6112

The one liner approach without importing statistics:

def variance(allgroups):
    return [sum((x - sum(group) / len(group)) ** 2 for x in group) / (len(group) - 1) for group in zip(*allgroups)]

Upvotes: 1

Mureinik
Mureinik

Reputation: 311326

You don't need to reinvent the wheel - statistics.variance could do the heavy lifting for you:

import statistics

data = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
variances = [statistics.variance(l) for l in zip(*data)]

Upvotes: 1

user_1177868
user_1177868

Reputation: 444

You can simply use the built-in library statistics (documentation).

import statistics

data = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]

def avg(all_groups):
    return [statistics.mean(i) for i in zip(*all_groups)]

def variance(all_groups):
    return [statistics.variance(i) for i in zip(*all_groups)]

print(avg(data))
print(variance(data))

Upvotes: 1

Related Questions