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))

11th Nov 2021, 3:25 AM
Kim Hammar-Milliner
Kim Hammar-Milliner - avatar
7 Respuestas
+ 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)
11th Nov 2021, 4:58 AM
Brian
Brian - avatar
+ 1
Thanks
11th Nov 2021, 5:09 AM
Kim Hammar-Milliner
Kim Hammar-Milliner - avatar
+ 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.
11th Nov 2021, 5:58 AM
Brian
Brian - avatar
0
I dont know what the problem is can someone help me
11th Nov 2021, 3:26 AM
Kim Hammar-Milliner
Kim Hammar-Milliner - avatar
0
I solved it.
11th Nov 2021, 5:26 AM
Kim Hammar-Milliner
Kim Hammar-Milliner - avatar
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))
11th Nov 2021, 5:26 AM
Kim Hammar-Milliner
Kim Hammar-Milliner - avatar
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)
3rd Mar 2022, 8:57 AM
achmad mustafa
achmad mustafa - avatar