(Mar-25-2022, 05:00 AM)deanhystad Wrote: Why are you doing this:
total = 0 for i in l: total = i + total mean = total / len(l) total2 = 0 total3 = 0 for i in l: total2 = (i - mean) ** 2 total3 = total2 + total3 total3 = total3 / (len(l) - 1)Each time you do this:
for line in f: numstr = line.rstrip().lstrip() numstr = int(numstr) l.append(numstr)Was your intention to load all the values into "l" and then compute variance? That is not what you're doing.
And let's talk about your variable names. One letter variable names are bad, but two of them are particularly bad. l (lower case L) and O (upper case o). These are particularly bad because they look a lot line one and zero.
total, total2 and total3 aren't much better. What are these for? Why do you need so many totals? numstr is a little better. At least there is some indication of what it does (used to convert str to a num).
The answer for your data is 3.7. I computed this using statistics.variance and numpy.var. I know you cannot use these for this exercise, but it is good to know the right answer.
import numpy import statistics f = (" 2 \n", "3 \n", " 4\n", "-1\n", "3\n") numbers = [int(numstr) for numstr in f] def naive_variance(numbers): n = len(numbers) sum_num = sum(numbers) sum_sqr = sum(number**2 for number in numbers) return (sum_sqr - (sum_num**2)/n) / (n-1) def twopass_variance(numbers): # Leave this as an exercise for you ... return sum_norm_sqr / (n-1) def numpy_variance(numbers): return numpy.var(numbers, ddof=1) print(naive_variance(numbers),) print(twopass_variance(numbers)) print(numpy_variance(numbers)) print(statistics.variance(numbers))Some things to note:
Output:3.7 3.7 3.7 3.7
I didn't want to make a file, so I made a list of number strings. I added linefeeds and spaces to the strings and still didn't have to use strip to convert these strings to numbers. You probably don't have to either.
It looks like you are using the two-pass algorithm. You can use sum(...) to compute the sum of a list of numbers instead of using a for loop. If you can use a list comprehension you can compute this in one line:
for i in l: total2 = (i - mean) ** 2 # Can you combine these two lines? total3 = total2 + total3 total3 = total3 / (len(l) - 1) # Are you sure this belongs here?
Thank you for your brutal honesty. As you most likely already know, I don't really know what I'm doing. My professor is a nice guy, but he kind of threw us all into the deep end with this. His labs and assignments are pretty hard in my opinion. Sorry if reading my programs gives you a headache. I took what you said and got the variance for the text file so thanks. Now I got to complete the rest of the assignment. Wish me luck