0

Python for datascience

Hey, can someone tell, why my code isn't good for this problem? #In order to find the variance I have to find the mean, after do (elem( element of the list) - mean) **2 for each element of the list, then to add all this results and divide them by the total number of elements. (at I understand so) Using the same vaccinations dataset, which includes the number of times people got the flu vaccine. The dataset contains the following numbers: never: 5 once: 8 twice: 4 3 times: 3 Calculate and output the variance. Declare a list with the data and use a loop to calculate the value. We will soon learn about easier ways to calculate the variance and other summary statistics using Python. For now, use Python code to calculate the result using the corresponding equation. My code: arr=[5,8,4,3]; x=0; x =(8*1+4*2+3*3+5*0)/20; y=0; sum=0; for i in range(len(arr)): y+=(arr[i]-x)**2; sum+=1; print (y/sum);

28th Mar 2021, 10:24 PM
Zotta
Zotta - avatar
17 odpowiedzi
+ 6
@Endry Saputra This is my code and it´s working fine: vaccs = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3] mean = sum(vaccs)/len(vaccs) variance = (sum((v-mean)**2 for v in vaccs)/len(vaccs)) print(variance)
5th May 2021, 10:36 PM
Ralf
Ralf - avatar
+ 4
Here is my answer, hopefully this explains it for anyone: data = [0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,3,3,3] mean = sum(data) / len(data) sqr = 0 for i in data: sqr += (i - mean) ** 2 variance = sqr / len(data) print(variance)
23rd Jun 2021, 8:07 PM
Maikel
+ 1
Here's my solutions 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); count=0; for i in range(len(vac_nums)): variance = (vac_nums[i]-mean)**2; count += variance; print (count/len(vac_nums));
4th Feb 2022, 8:19 PM
Fatimah Mohammad Emad EL Den
+ 1
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 total=0 mean=0 for vac in vac_nums: total=total + vac mean=total/len(vac_nums) squared_differences = [ (vac-mean)**2 for vac in vac_nums ] total_squared_differences=0 for squared_difference in squared_differences: total_squared_differences=total_squared_differences + squared_difference variance=total_squared_differences/len(vac_nums) print(variance)
17th Nov 2022, 1:17 AM
Benjamin Fadina
Benjamin Fadina - avatar
0
The values are 0-3, the numbers 5,8,4,3 are only the number of occurrences. Use the values to calculate variance, not the occurrences. The values are grouped by no of vaccinations, think of them as a list of 20 values between 0 and 3.
28th Mar 2021, 11:46 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
0
Benjamin Jürgens sorry, my brain doesn't work very well right now, can you show me the code, it will help me better, or at least what to change
28th Mar 2021, 11:48 PM
Zotta
Zotta - avatar
0
Don't take the squared difference of arr[i]-x. X is the mean number of vaccinations (1.25), arr has values 5,8,4,3. Those numbers are not related, 1.25 isn't the mean of 5,8,4,3. The differences has to be between 0,1,2,3 and x (and then squared). You could either do that 20 times (5 times with 0, etc.). Or only once for each value 0-3 and after squaring multiply by no of occurrences. So four iterations of the loop, first will add (0-x)**2 * 5 to y (= 7.8125) In any case you have to divide by 20 (the sum of 5,8,4,3), because your dataset has 20 elements.
29th Mar 2021, 12:17 AM
Benjamin Jürgens
Benjamin Jürgens - avatar
0
Benjamin Jürgens thanks, it worked Can you give some material for better understanding of this topic?
29th Mar 2021, 10:20 AM
Zotta
Zotta - avatar
0
Zotta great that you found the solution! I don't have material to give you, but i don't think you need that. Just develop your understanding of the topic to be confident about right interpretation of problems and what different sets of numbers mean. You will surely find more material about variance by searching the web. W3schools, geeksforgeeks etc. should cover that. For practice codewars or similar should have some challenges on the topic
29th Mar 2021, 10:32 AM
Benjamin Jürgens
Benjamin Jürgens - avatar
0
Benjamin Jürgens thanks a lot
29th Mar 2021, 10:35 AM
Zotta
Zotta - avatar
0
I didnt got it bro... would u like to share that code so i can understand it perfectly
19th Apr 2021, 1:33 PM
Endry Saputra
0
data = [0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,3,3,3] rata = sum(data)/len(data) X = 0 jml = 0 for i in data: X += (data[i] - rata)**2 ; jml += X ; print (jml/len(data)) This is my code and its wrong
19th Apr 2021, 10:19 PM
Endry Saputra
0
This is my code it isn't working either. 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 sum = 0 count = 0 sum_squares = 0 for value in vac_nums: sum = sum + value count = count + 1 mean_value = (sum / count) for value in vac_nums: sum_squares = sum_squares + ((value - mean_value)** 2) var_value = sum_squares / (count-1) print (var_value)
5th Nov 2021, 10:25 AM
Isaac Otu
Isaac Otu - avatar
0
I hope this works. def variance(l): mean = sum(l) / len(l) var = sum((v - mean)**2 for v in l) / len(l) return var vac_nums = [0,0,0,0,0, 1,1,1,1,1,1,1,1, 2,2,2,2, 3,3,3 ] print(variance(vac_nums))
3rd Dec 2021, 2:45 PM
Joshua Hans C. Aringino
Joshua Hans C. Aringino - avatar
0
import numpy as np a = np.array([0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,3,3,3]) mean = np.sum(a)/a.size v = np.sum((a-mean)**2)/a.size print(v)
9th Apr 2022, 2:16 AM
Erhan TEZER
0
Here is my code in the simplest way but working: import statistics print (statistics.pvariance([0,0,0,0,0, 1,1,1,1,1,1,1,1, 2,2,2,2, 3,3,3 ]))
22nd Dec 2022, 6:20 PM
Cesar Isaac
0
Program
25th Jan 2023, 1:23 PM
VINOTH V
VINOTH V - avatar