Python: scatter a list of specific numbers

I am trying to create a function that prints the variance of a list of specific numbers:

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5] 

So far, I have tried to continue these three functions:

 def grades_sum(my_list): total = 0 for grade in my_list: total += grade return total def grades_average(my_list): sum_of_grades = grades_sum(my_list) average = sum_of_grades / len(my_list) return average def grades_variance(my_list, average): variance = 0 for i in my_list: variance += (average - my_list[i]) ** 2 return variance / len(my_list) 

However, when I try to execute the code, it causes the following error in the following line:

 Line: variance += (average - my_list[i]) ** 2 Error: list index out of range 

Sorry if my current knowledge of Python is limited, but I'm still involved - so please, if you want to help solve this problem, try not to offer extremely complex ways to solve this problem, thank you very much.

+7
source share
8 answers

First, I would suggest using the Python built-in sum method to replace your first custom method. grades_average then becomes:

 def grades_average(my_list): sum_of_grades = sum(my_list) average = sum_of_grades / len(my_list) return average 

Secondly, I highly recommend looking into the NumPy library, as it has these methods built-in. numpy.mean() and numpy.std() will cover both of these cases.

If you're interested in writing code for yourself first, that's fine too. As for your specific error, I believe that @gnibbler nailed it above. If you want to use the index cyclically, you can restructure the row in grades_variance to be:

 for i in range(0, len(my_list)): 

As Lattyware noted, the index loop is not particularly "Pythonic"; the way you do it now is usually superior. This is just for your reference.

+8
source

Try numpy .

 import numpy as np variance = np.var(grades) 
+17
source

When you speak

  for i in my_list: 

i not an element index. i is an element

 for i in my_list: variance += (average - i) ** 2 
+6
source

While gnibbler solved the problem with your code , you can achieve this much more easily using the built-in functions and the generator expression :

 average = sum(grades) / len(grades) varience = sum((average - value) ** 2 for value in grades) / len(grades) 

It may seem a little scary at first, but if you watch the video that I will link about understanding of lists and expressions of generators, they are actually very simple and useful.

+5
source

python 3.4 has lib stats that does this.

  import statistics grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5] statistics.pvariance(grades) => 334.07100591715977 

https://docs.python.org/3/library/statistics.html#statistics.pvariance

+4
source

I assume you need sample variance, i.e. an unbiased variance estimate. I think this feature can do the job. It prints the variance and the average of the vector n.

 n = [5, 3, 1, 2, 4] def variance1337(n): var1 = [] mean1 = sum(n)/len(n) for xs in n: var1.append((xs - mean1) ** 2) print(sum(var1)/(len(n) - 1)) print(mean1) 
0
source

without importing or using generators, you can use the following two functions:

  def mean(data): return sum(data)/len(data) def variance(data): ndata = [] for xi in data: ndata.append((xi - mean(data))**2) return (sum(ndata)/len(data)) 

You can squeeze the average function into the variance function, but it becomes difficult to read:

  def variance(data): ndata = [] for xi in data: ndata.append((xi - sum(data)/len(data))**2) return (sum(ndata)/len(data)) 
0
source

the code below is used to get the average

 def grades_average(my_list): sum_of_grades = sum(my_list) average = sum(my_list) / len(my_list) return average 

dispersion formula → The average value of the squares of the mean. This code below is used to get the variance of values.

 def grades_variance(my_list, average): variance = 0 for i in my_list: variance += (average - i) ** 2 return variance / len(my_list) 
-1
source

Source: https://habr.com/ru/post/945497/


All Articles