Variance: Last Step: Review


#1



https://www.codecademy.com/en/courses/python-intermediate-en-7mgOa/2/3?curriculum_id=4f89dab3d788890003000096#

So, at the very end I am printing out all of the values for the list "grades" they ask for: all the grades, sum of the grades, average of the grades, variance of the grades, standard deviation of the grades. I was stuck on the sum and average of the grades both because codecademy wanted a very specifc number of decimal places to be printed for those values. I'm not sure if this is the same case for variance and standard deviation but for some reason it keeps giving the error: "Oops, try again. It looks like the variance was not printed! Make sure you are printing it on its own line".

Any help is appreciated. Thanks!


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

def print_grades(grades):
    for grade in grades:
        print grade

def grades_sum(grades):
    total = 0
    for grade in grades: 
        total += grade
    return total
    
def grades_average(grades):
    sum_of_grades = grades_sum(grades)
    average = sum_of_grades / float(len(grades))
    return average

def grades_variance(grades):
    average = grades_average(grades)
    variance = 0
    for grade in grades:
        variance +=((average - grade) ** 2)
    return variance/(len(grades))
    
def grades_std_deviation(variance):
    return variance**0.5

variance = grades_variance(grades)

print "Grades: "
for grade in grades:
    print grade
print "%.1f" % (grades_sum(grades))
print "%.10f" % (grades_average(grades))
print "%.10f" % (grades_variance(grades))
print "%.10f" % (grades_std_deviation(variance))


#2

If we examine the provided data, we see there is a float present... 50.5. This will force everything into a float in the first function, grades_sum so we don't have to invoke the the float() function.

average = sum_of_grades / len(grades)

Since length is a counting number, it doesn't really make sense to a reader why we would cast it to a float. It is an integer and should remain one.

I believe this exercise is written this way because the float() function may not have come up yet in any previous lessons. However, if it has come up and you are familiar with it, the place to explicitly declare it would be in the grades_sum function, since that is top of the chain. A float from that function will prevail through all the other functions.

total = float(0)    # will be `0.0`

It has never surfaced before that the author expected any specific number of decimal places, just the computed value. We are not asked to format our outputs.


#3

Just extra thing: You have a function that does exactly this called print_grades. It's the first function.


#4

That's interesting; during the creation of the "average" variable, the codecademy specifically stated to explicitly establish "len(grades)" as being a float by "float(len(grades))". You make some fair points, but it seems like I still am not able to pinpoint the error in my code.


#5

Nice catch! I have to learn to get into the midset of a programmer and quit insisting on making life harder for myself, haha!


#6

Yes, I see it does...

Computes the average of the grades by dividing that sum by float(len(grades)).

Bear in mind that this course was written by a lay person, not a teacher. This is not CC official material, but written by a volunteer. Now I see why so many people are casting a counting number to a float. It won't stop me harping about the semantics. The numerator is what we (the reader) expect to be a float.

Fortunately, Python 3 got rid of this hack. All quotients return as floats, now, and only integers need to be cast explicitly.


#7

Aside

I mentioned earlier about the chain.

Let,

f = grades_std_deviation

g = grades_variance

h = grades_average

k = grades_sum

Now all those functions are loaded into those simple variables.

Our chain will have some dependencies, so will take some tinkering of the existing functions to accomodate it, but even in their existing form, they represent a chain. This is just a putting a finer point on it.

f(g(h(k(x))))

Consider f(x)...

from math import sqrt

def f(x): return sqrt(x)

What we are passing f is the return value of g(x), the variance.

>>> def mean(x): return float(sum(x) / len(x))

>>> def variance(x, func=mean):
	avg = func(x)            # permits other functions, besides `mean`
	sig = 0
	for i in x:
		sig += (avg - i) ** 2
	return float(sig/len(x))

>>> variance([93,68,87,18,23,45,19])
897.1020408163265
>>>

g(x) is handed a number derived by h(x) which is the average of the sample space (list of values), the sum of which is handed to h(x) by k(x).


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.