+ 1
Number of Vaccinations Activity
In the new course "Python in Data Science", there is an activity called "Number of Vaccinations" which is very easy but I can't get the answer right. Here is the link for the activity, and any help would be highly appreciated, Thank you. The link: https://www.sololearn.com/learning/1161/4777/12269/1
13 Answers
+ 5
you must carefully read the hint provided:
Hint: Think about the data this way: it contains 20 values, each representing the number of vaccinations the corresponding person had.
then, you could visualize better the data set by writtng it:
ds = [ 3, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 ]
hope this help! :)
+ 5
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
print((0*5+8*1+4*2+3*3)/20)
+ 3
Note the variance is the squared difference between the mean and the values. So all you need to do is to subtract the mean from the value and square it.
def variance(X):
mean = sum(X)/len(X)
tot = 0.0
for x in X:
tot = tot + (x - mean)**2
return tot/len(X)
sample=[0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,3,3,3]
print(variance(sample))
This code works perfectly đ.
+ 2
Here's my solution using a dictionary to allow me to add the values easily, please consider reading the code carefully
https://code.sololearn.com/c8y85eVu6Z8i/?ref=app
+ 2
LIN, ZHI-JIA yeah I noticed it but it just didn't accept this way for a reason I don't know yet
+ 1
This is my solution !
Number of Vaccinations:
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)
diff = 0.0
for i in vac_nums:
diff = diff + (mean - i)**2
variance = diff / len(vac_nums)
print(variance)
0
this is your data [0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,3,3,3]
the result is
print ((0*5+1*8+2*4+3*3)/20)
0
nums={0:5,1:8,2:4,3:3}
arr=[]
for key,val in nums.items():
for i in range(key):
arr.append(val)
#arr will be [8,4,4,3,3,3]
#mean
print(sum(arr)/20)
0
vac = {
'never': 5,
'once': 8,
'twice': 4,
'3 times': 3
}
num_vac =[]
num_vac.append(vac['once'])
num_vac.append(vac['twice']*2)
num_vac.append(vac['3 times']*3)
sum_people = vac['once'] + vac['twice'] + vac['3 times'] + vac['never']
mean_vac = sum(num_vac)/sum_people
print(mean_vac)
0
sum=0;
for i in range(len(vac_nums)):
sum=sum+vac_nums[i]
print(sum/len(vac_nums))
0
Here is my solution!
vac_nums = [0,0,0,0,0,
1,1,1,1,1,1,1,1,
2,2,2,2,
3,3,3
]
def variance(vac_nums):
mean = sum(vac_nums)/len(vac_nums)
deviations = [(x - mean) ** 2 for x in vac_nums]
variance = sum(deviations) /len(vac_nums)
return(variance)
print(variance(vac_nums))
0
my solution is
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
print(mean:=sum(vac_nums)/len(vac_nums))
- 3
This is my solution!
import numpy as np
never = np.array([0 for x in range(5)])
once = np.array([1 for x in range(8)])
twice = np.array([2 for x in range(4)])
three = np.array([3 for x in range(3)])
total = np.concatenate((never, once, twice, three), axis=None)
print(np.mean(total))