+ 2
Variance
vac_nums = [0,0,0,0,0, 1,1,1,1,1,1,1,1, 2,2,2,2, 3,3,3 ] mean = sum(vac_nums)/len(vac_nums) for i in vac_nums: variance=sum((vac_nums[i]-mean)**2)/len(vac_nums) print(variance) #PLease, correct me https://code.sololearn.com/cUZ5BZ2d0H23/#py
12 Respuestas
+ 4
Maybe you were trying to do something like this?
https://code.sololearn.com/cC2CnB200Wwk/?ref=app
+ 10
Key- tune S ,
the issue with your code is, that it does not make sence to build the sum with sum() function, since the result will be calculated again in each iteration and it overwrites the content of the variable `variance`. sum() function makes sense when the numbers are `supplied` and processed in a list comprehension.
it can be done like:
variance = 0
vac_nums = [0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,3,3,3]
mean = sum(vac_nums)/len(vac_nums)
for i in vac_nums:
variance += (i - mean) ** 2
print(variance / len(vac_nums))
the result should be 0.9875
+ 5
Please LINK your code. Go to Code section, click +, select the programming language, insert your code, save. Come back to this thread, click +, Insert Code, sort for My Code Bits, select your code.
Also, please look up the formula for variance.
var = sum(xi - meanx)**2 / n
or
var = sum(xi - meanx)**2 / (n-1)
Variance is a property of a set of data, not of a single data point in isolation.
+ 3
Key- tune S
I couldn't understand your code so wrote my own based on a google link -> attached.
Read the link to understand the steps used and refer to the attached code to see how to implement.
I layed it out in simple steps & avoided a complex equation - because I am not that clever.
Also included Standard Deviation which you are about to learn.
https://code.sololearn.com/cx94DGp3Q78s/?ref=app
+ 3
Akash Prakash Hi, this is a thread about calculating the variance in Python. It is not clear how your code relates to this topic. Please do not advertise on other people's questions.
+ 1
Will do what both of you told me :)
I am not trying to be clever, just sorting my thoughts and logic until it clicks :D
THANK YOU
+ 1
the link doesn't work...?
+ 1
and this one works:
https://code.sololearn.com/cJhF9Lq3oaFg
+ 1
It is beautiful :D
I am not that good, but i think ahead of myself obviously. Thank you
+ 1
Nice.. it looks clean :)
I will try to implement your advice...probably in next problem that arises :D
+ 1
I saw the repetition already and this is one more answer that I didn't ask, but it was needed. :)
Thank you
0
sry
https://code.sololearn.com/cUZ5BZ2d0H23
Wanted to pack it like this...but it doesn't calculate variance.. :D