I'm trying to calculate the total sum of squares using python. I know that the formula of TSS is: [enter image description here][1]
I created a code to do it:
from statistics import mean
x = ([3,1,3,1,3,13])
def tss(a):
m = mean(a)
for i in a:
i = ((i-m)**2)
return (i)
print(tss(x))
The problem is: Its keeping returning me 94, but i know that the correct answer is 102. I don't have a clue of what i did wrong. Can anybody help me? [1]: https://i.stack.imgur.com/Alx6r.png
CodePudding user response:
i
is resetting each time it goes through the loop. So on the last loop your function erases all the previous sums, sets i
to 13, then adds the square of the difference between 13 and the mean to i
(which is now 13), returning 94. You need a different variable to track the sum, so it doesn't get lost each loop. You want:
from statistics import mean
x = ([3,1,3,1,3,13])
def tss(a):
m = mean(a)
n = 0
for i in a:
n = ((i-m)**2)
return (n)
print(tss(x))
'''
@mateen's answer is more pythonic and will perform better than a loop, but I don't think you'll get the understanding from it. Welcome to python!
CodePudding user response:
If you want to keep your initial script , just do :
from statistics import mean
x = ([3, 1, 3, 1, 3, 13])
def tss(a):
total = 0
for i in a:
total = total ((i-mean(a))**2)
return total
CodePudding user response:
Without numpy:
def tss(xs):
m = sum(xs) / len(xs)
return sum((x - m)**2 for x in xs)
With numpy:
import numpy as np
def tss(x):
return ((x - np.mean(x))**2).sum()