Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Zero Division Error
#1
Hello everyone
This goal of this assignment is to use a separate text file and then use the numbers within it and compute the variance. My professor has barred us from using the variance library function, so we have to do it manually. I believe I'm close I just can't figure out why I'm getting a ZeroDivisionError.
This is what I have so far:
l = []
with open('in3.txt') as f:
    for line in f:
        numstr = line.rstrip().lstrip()
        numstr = int(numstr)
        l.append(numstr)
        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)
print(total3)
the text file is called 'in3' and it reads:

2
3
4
-1
3


Any help is greatly appreciated thanks for looking.
Reply
#2
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))
Output:
3.7 3.7 3.7 3.7
Some things to note:
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?
Reply
#3
(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))
Output:
3.7 3.7 3.7 3.7
Some things to note:
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding square roots using long division. jahuja73 10 5,458 Feb-24-2021, 01:25 PM
Last Post: jahuja73
  Division problem Eric7Giants 1 1,704 Nov-16-2019, 05:50 AM
Last Post: ndc85430
  Count how many carpets you need to fill room floor without multiplication/division Ech0ke 1 2,327 Apr-20-2019, 07:50 PM
Last Post: ichabod801
  Zero Division Error moga2003 4 3,093 Mar-07-2019, 02:15 AM
Last Post: moga2003

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020