Python: Variance of a list of defined numbers
Solution 1
First I would suggest using Python's 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
Second, I would strongly recommend looking into the NumPy library, as it has these methods built-in. numpy.mean()
and numpy.std()
would cover both these cases.
If you're interested in writing the code for yourself first, that's totally fine too. As for your specific error, I believe @gnibbler above nailed it. If you want to loop using an index, you can restructure the line in grades_variance
to be:
for i in range(0, len(my_list)):
As Lattyware noted, looping by index is not particularly "Pythonic"; the way you're currently doing it is generally superior. This is just for your reference.
Solution 2
Try numpy.
import numpy as np
variance = np.var(grades)
Solution 3
When you say
for i in my_list:
i
isn't the index of the item. i
is the item
for i in my_list:
variance += (average - i) ** 2
Solution 4
While gnibbler has solved the problem with your code, you can achieve this much more easily using built-in functions and a generator expression:
average = sum(grades) / len(grades)
varience = sum((average - value) ** 2 for value in grades) / len(grades)
It might look a little scary at first, but if you watch the video I link about list comprehensions and generator expressions - they are actually really simple and useful.
Solution 5
python 3.4 has a statistics lib which 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
![GiamPy](https://i.stack.imgur.com/qqhs1.jpg?s=256&g=1)
GiamPy
Experienced Back-end developer with PHP 5.x & 7.x and with MVC framework Laravel/Lumen 4.x & 5.x. I started programming as a passion when I was 14 years old with C-like scripting languages. I have been working for approximately 4 years and a half already, and currently I am working for Nike as a Senior DevOps Engineer, in Gdansk, Poland. As a passionate developer, I love to keep myself informed of the latest news about programming, and I always strive forward to improve as a developer. For me, writing code is an art, and code style is important. I also love working with languages like JavaScript (Node.js & AngularJs) and I'm a great passionate of Amazon Web Services! I believe that starting to program at such a young age has been one of the most satisfying decisions I have took. The feeling of seeing someone appreciating your creations is priceless. I also produce music electronically under the alias of YaiPMG. You can find me @ https://www.facebook.com/yaipmg/.
Updated on July 09, 2022Comments
-
GiamPy almost 2 years
I am trying to make a function that prints the variance of a list of defined numbers:
grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]
So far, I have tried proceeding on making 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)
When I try to execute the code, however, it gives me the following error at the following line:
Line: variance += (average - my_list[i]) ** 2 Error: list index out of range
Apologies if my current Python knowledges are limited, but I am still learning - so please if you wish to help solving this issue try not to suggest extremely-complicated ways on how to solve this, thank you really much.