Python for datascience | Sololearn: Learn to code for FREE!

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

3/28/2021 10:24:09 PM

Zotta

14 Answers

New Answer

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

+2

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)

+1

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.

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

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

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.

0

Benjamin Jürgens thanks, it worked Can you give some material for better understanding of this topic?

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

0

Benjamin Jürgens thanks a lot

0

I didnt got it bro... would u like to share that code so i can understand it perfectly

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

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)

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

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)