Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
division error
#1
Im trying to make a program that will compute a regression line and then display it. First I click on the window to get the points and then when I click on the rectangle that says 'done' the loop will stop and then display the line of best fit. However the loop only goes through one iteration before stopping and giving me a division by zero error(m=numerator/denominator) . Sorry if its not formatted right I asked ai to do it for me

def main():
    print("Click anywhere in graph window ten times to get points")

    x = 0
    y = 0
    click_count = 0
    avgOfX = 0
    avgOfY = 0
    productOfXandY = 0
    productOfX = 0
    m = 0
    numerator = 0
    denominator = 0
    y_eqOne = 0
    y_eqTwo = 0

    win = GraphWin("regression line", 400, 400)

    rect = Rectangle(Point(3, 3), Point(60, 65))
    rect.draw(win)

    label = Text(Point(30, 30), "Done")
    label.draw(win)

    while i <= 10:
        click_point = win.getMouse()
        click_count += 1
        i = i + 1

        mouse = click_point
        mouse.draw(win)

        click_x = mouse.getX()
        click_y = mouse.getY()

        p1 = rect.getP1()
        p2 = rect.getP2()

        rect_x1 = p1.getX()
        rect_y1 = p1.getY()
        rect_x2 = p2.getX()
        rect_y2 = p2.getY()

        x = x + click_x
        y = y + click_y

        # Stop if "Done" rectangle is clicked
        if (rect_x1 <= click_x <= rect_x2) and (rect_y1 <= click_y <= rect_y2):
            break

        avgOFx = x / click_count
        avgOFy = y / click_count

        productOFandY = click_x * click_y
        productOFx = click_x * click_x

        sumOFx = sumOFx + productOFx
        sumOFxandY = sumOFxandY + productOFandY

        numerator = sumOFxandY - click_count * (avgOFx * avgOFy)
        denominator = sumOFx - click_count * (avgOFx ** 2)

        m = numerator / denominator

        y_engOne = y - avgOFy + m * (x - avgOFx)
        y_eqOne = y_engOne
        y_eqTwo = y_eqOne

        ln = Line(Point(10, y_eqOne), Point(100, y_eqTwo))
        ln.draw(win)
Gribouillis write Dec-11-2025, 03:34 AM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Reply
#2
nathanael, check your other post about graphics error!

Monsieur Snippsat gave you some very good advice! Listen to him and your problems will melt away!
Reply
#3
Either your indenting or your logic is wrong. You don't want to compute the linear regression until you collect more than one point. You are doing this:
while count < 10:
    if click in stop rect:
        break
    process_point()
    compute_regression()
What you want is this:
while count < 10:
    if click in stop rect:
        break
    process_point()
if count > 1:
    compute_regression()
Once you compute the slope and offsets, you'll find that the equation for you line is incorrect.
y = mx + b where b = y_mean - m * x_mean
Compute y for two values of x (0 and 400 make sense)

I found your code difficult to read. Some of that is due to the variable names. if x is the sum of point.getX(), why sumOFx the sum of point.get(x)**2? Maybe name that xx? or x_squared? I should also mention that it is not the python convention to use camelCase for variables. Use snake_case instead. I know, I know that the graphics library you are using uses camel case, but that library is ancient. The common naming convention for python is snake case for variable and function names.

Another thing that makes you code hard to read is the way it is organized. The purpose of the program is to compute a line that fits the entered points. If that is the pourpose, why does this part of the code that checks if you want to exit the loop.
        click_x = mouse.getX()
        click_y = mouse.getY()
 
        p1 = rect.getP1()
        p2 = rect.getP2()
 
        rect_x1 = p1.getX()
        rect_y1 = p1.getY()
        rect_x2 = p2.getX()
        rect_y2 = p2.getY()
 
        x = x + click_x
        y = y + click_y
 
        # Stop if "Done" rectangle is clicked
        if (rect_x1 <= click_x <= rect_x2) and (rect_y1 <= click_y <= rect_y2):
            break
Take up almost as much space as the part that does the regression and draw the line.
        avgOFx = x / click_count
        avgOFy = y / click_count
 
        productOFandY = click_x * click_y
        productOFx = click_x * click_x
 
        sumOFx = sumOFx + productOFx
        sumOFxandY = sumOFxandY + productOFandY
 
        numerator = sumOFxandY - click_count * (avgOFx * avgOFy)
        denominator = sumOFx - click_count * (avgOFx ** 2)
 
        m = numerator / denominator
 
        y_engOne = y - avgOFy + m * (x - avgOFx)
        y_eqOne = y_engOne
        y_eqTwo = y_eqOne
 
        ln = Line(Point(10, y_eqOne), Point(100, y_eqTwo))
        ln.draw(win)
I would write like this:
    x, y, xx, xy = 0, 0, 0, 0
    n = 0
    while n < 10:
        p = win.getMouse()
        # break if point is inside Done rectangle
        if (done.getP1().getX() <= p.getX() <= done.getP2().getX()) and (
            done.getP1().getY() <= p.getY() <= done.getP2().getY()
        ):
            break
        p.draw(win)
        x += p.getX()
        y += p.getY()
        xx += p.getX() ** 2
        xy += p.getX() * p.getY()
        n += 1

    if n > 1:
        # Fit a line to the points.
        mean_x = x / n
        mean_y = y / n
        numerator = xy - n * mean_x * mean_y
        denominator = xx - n * mean_x**2
        m = numerator / denominator if denominator != 0 else 0
        b = mean_y - m * mean_x

        # Draw the regression line
        x1, x2 = 0, 400
        line = Line(Point(x1, m * x1 + b), Point(x2, m * x2 + b))
        line.setFill("red")
        line.draw(win)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Why was floor division used here? tmv 2 115 Jan-31-2026, 02:00 AM
Last Post: Pedroski55
  Division Mallard 9 891 Dec-17-2025, 03:47 PM
Last Post: Mallard
  Division questions Dionysis 5 3,605 Feb-14-2023, 02:02 PM
Last Post: Dionysis
  Division by zero and value of argument Lawu 5 10,682 Jul-01-2022, 02:28 PM
Last Post: Lawu
  Division calcuation with answers to 1decimal place. sik 3 3,557 Jul-15-2021, 08:15 AM
Last Post: DeaD_EyE
  Floor division return value Chirumer 8 7,826 Nov-26-2020, 02:34 PM
Last Post: DeaD_EyE
  Integer division plozaq 2 3,321 Sep-28-2020, 05:49 PM
Last Post: plozaq
  Overcoming ZeroDivisionError: division by zero Error dgrunwal 8 9,358 Jun-12-2020, 01:52 PM
Last Post: dgrunwal
  Division of an integer into sub-numbers Richard_SS 4 4,895 Jun-14-2019, 11:47 AM
Last Post: DeaD_EyE
  Logic of using floor division and modulus for a different variable at different time SB_J 2 3,933 Nov-01-2018, 07:25 PM
Last Post: SB_J

Forum Jump:

User Panel Messages

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