0
Vaccination dataset
Python data science It failed #your code goes here dataSet = [0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,3,3,3] ## MEAN OF DATASET sumData = 0 for i in range(len(dataSet)): sumData += dataSet[i] mean = sumData/len(dataSet) ## mean = 25/20 ## VARIANCE OF DATASET sumSqrt = 0 for i in range(len(dataSet)): diff = dataSet[i] - mean sqrt = diff * diff sumSqrt += sqrt variance = sumSqrt/len(dataSet) ## OUTPUT print(dataSet) print("\nMean is " + str(mean)) print("Variance is " + str(variance))
7 ответов
+ 2
The code looks good. Could it be the wrong variance formula? The formula seen in the code is to be used when the dataset is the entire population. If the data are only a subset (i.e., a sample) of the full population then it is customary to divide by (N - 1) instead of N.
Viz.
variance = sumSqrt/(len(dataSet)-1)
+ 1
Thanks
+ 1
The new code prints the same value as the original post. Eliminating the extra output text made the difference. Code Coach is very particular when comparing output.
0
I dont know what the problem is can someone help me
0
I solved it.
0
import math
#your code goes here
vax = [0, 0, 0, 0, 0, 1, 1,1,1,1,1,1,1,2,2,2,2,3,3,3]
def mean(list):
count = len(list)
total = 0
for i in list:
total += i
return total/count
def vari(mean, list):
count = len(list)
total = 0
for i in list:
total += (abs(i - mean)**2)
return (total/count)
print(vari(mean(vax), vax))
0
i can help you ;)
import math
vac_nums = [0,0,0,0,0,
1,1,1,1,1,1,1,1,
2,2,2,2,
3,3,3
]
#your code goes here
mean = sum(vac_nums)/len(vac_nums)
variance = [pow(mean-x,2) for x in vac_nums]
variance = sum(variance)/len(variance)
print(variance)