Hello, so I'm fairly new to python and I have become stumped on this one problem. As much as I would like you to solve the problem for me, I do not want you to do so without explanation, I would much rather have a step by step procedure. After all i'm here to learn python, not to pass a class.

The question is:

Write a program that graphically plots a regression line, that is, the line with the best fit through a collection of points. First ask the user to specify the data points by clicking on them in a graphics window. To find the end of input, place a small rectangle labeled "Done" in the lower left corner of the window; the program will stop gathering points when the user clicks inside that rectangle.

The regression line is the line with the following equation
y = (mean of y values) + m * (x - (mean of x values))

where
(not sure how to write this but)
m = Σxiyi - n(mean of x's) * (mean of y's) / Σxi^2 - n(mean of x's)^2
(I don't think thats written right, I haven't gotten that far in mathematics.)
n = number of points

As the user clicks on points, the program should draw them in the graphics window and keep track of the count of input values and the running sum of x, y, x^2, and xy values. When the user clicks inside the "Done" rectangle, the program then computes value of y (using the equations above) corresponding to the x values at the left and right edges of the window to compute the endpoints of the regression line spanning the window. After the line is drawn, the program will pause for another mouse click before closing the window and quitting.


Okay so heres what i have so far...

from graphics import*
def main():

    win=GraphWin("",400, 400)
    win.setCoords(-10,-10,10,10)

    button=Rectangle(Point(7,-9),Point(10,-10))
    button.setFill("Red")
    button.draw(win)

    done=Text(Point(8.5,-9.5),"Done")
    done.setTextColor("Black")
    done.draw(win)
    

    point1=win.getMouse()
    x=point1.getX()
    y=point1.getY()

    while x<=7 and y>= -9:
        Cir=Circle(p1,.2)
        Cir.setFill("Yellow")
        Cir.draw(win)
        point1=win.getMouse()
        x=point1.getX()
        y=point1.getY()

I have the GUI drawn up, I have my little rectangle box that says DONE, and whenever i click on the GUI, little circles draw up signifying points. Not exactly sure how go on to the next step, any help would be greatly appreciated!

The first thing you have to do is to store the points (usually in a list). Then calculate m. Σxiyi = sum of the xy products, and Σxi^2 = sum of the x's squared. There are many pages explaining regression lines on the web. If you aren't sure how to calculate m then calculate it in more than one way and provide an answer for each one, since this isn't a math class. That is not the formula that one usually sees for a regression line but it may have been simplified for purposes of the program.

The first thing you have to do is to store the points (usually in a list). Then calculate m. Σxiyi = sum of the xy products, and Σxi^2 = sum of the x's squared. There are many pages explaining regression lines on the web. If you aren't sure how to calculate m then calculate it in more than one way and provide an answer for each one, since this isn't a math class. That is not the formula that one usually sees for a regression line but it may have been simplified for purposes of the program.

Alright thanks, and yeah the formula is based out of the book Python Programming: An Introduction Into Computer Science by John Zelle.

& how exactly would I go about making a list, would I do something like this...

stored_points = []
    stored_points.append(point1)

Again thanks for the help, I really do appreciate it!!!

That should do it if point1 is the point in numeric format, usually inputs are taken as string and are transformed as integer by int(). In your case however, the clicking points are already numbers, you maybe have to transform the points as mathematical points as generally computer screen coordinates go from 0,0 top,left increasing to right (OK) and down (not probably what expected for mathematical graph). p1 in the code you sent is not defined.

Ok I think i have my list figured out. However, I'm trying to implement an if loop that will break the while loop once the "DONE" Button is clicked, and then it can go onto the equations and eventually draw the regression line.

heres my code so far. (Please tell me if something is wrong / missing)

from graphics import*
def main():

    win=GraphWin("",400, 400)
    win.setCoords(-10,-10,10,10)

    button=Rectangle(Point(7,-9),Point(10,-10))
    button.setFill("Red")
    button.draw(win)

    done=Text(Point(8.5,-9.5),"Done")
    done.setTextColor("Black")
    done.draw(win)
    

    point1=win.getMouse()
    x=point1.getX()
    y=point1.getY()
    stored_points = []
    stored_points.append(point1)
    while x<=7 and y>= -9:
        Cir=Circle(point1,.2)
        Cir.setFill("Yellow")
        Cir.draw(win)
        point1=win.getMouse()
        x=point1.getX()
        y=point1.getY()
        if button.getMouse():
            break

consider lines 19 and 20, are they correct number of times?

Should it be put after / in the while loop?

You can add some print statements and figure it out. len(mylist) returns length of the list.

okay, here is what i got now.

from graphics import*
def main():

    win=GraphWin("",400, 400)
    win.setCoords(-10,-10,10,10)

    button=Rectangle(Point(7,-9),Point(10,-10))
    button.setFill("Red")
    button.draw(win)

    done=Text(Point(8.5,-9.5),"Done")
    done.setTextColor("Black")
    done.draw(win)
    

    point1=win.getMouse()
    x=point1.getX()
    y=point1.getY()

    while x<=7 and y>= -9:
        Cir=Circle(point1,.2)
        Cir.setFill("Yellow")
        Cir.draw(win)
        point1=win.getMouse()
        x=point1.getX()
        y=point1.getY()
    stored_points = []
    stored_points.append(point1)
    len(stored_points)
    print stored_points

After I close the GUI I get this,
>>> main()
[<graphics.Point instance at 0x02A4DD50>]

Sorry for taking a while, i'm pretty noob at python, but hopefully i'll improve :D.
Would appreciate if you could lead me to the next step, which I think is getting that button to work, then leading to the equations. Again thank for the help though!!! appreciate it

So you got one point instance in your list, is it what you wanted? Do you need to execute both of these statements for every point or one of them only once? Line 29 produces length, but it does not print it (only interactive shell you can see results without explicit print). It would print 1, of course.

So you got one point instance in your list, is it what you wanted? Do you need to execute both of these statements for every point or one of them only once? Line 29 produces length, but it does not print it (only interactive shell you can see results without explicit print). It would print 1, of course.

Alright, yeah that makes sense, so i'd have to put it in the loop, that way it will register every click inside the loop, and take out the print. Thanks!

while x<=7 and y>= -9:
        Cir=Circle(point1,.2)
        Cir.setFill("Yellow")
        Cir.draw(win)
        point1=win.getMouse()
        x=point1.getX()
        y=point1.getY()
        stored_points = []
        stored_points.append(point1)
        len(stored_points)
        print (stored_points)

Now i get a bunch of different instances, seems like its working :)!

Now to move on with the button, could i implement an if statement and if a registered click hits the statement, it will break the entire while loop? Then eventually python will move onto the equations?

Again, thank you so much for all your help, I really do appreciate it!

Alright, yeah that makes sense, so i'd have to put it in the loop, that way it will register every click inside the loop, and take out the print. Thanks!

while x<=7 and y>= -9:
        Cir=Circle(point1,.2)
        Cir.setFill("Yellow")
        Cir.draw(win)
        point1=win.getMouse()
        x=point1.getX()
        y=point1.getY()
        stored_points = []
        stored_points.append(point1)
        len(stored_points)
        print (stored_points)

Now i get a bunch of different instances, seems like its working :)!

Now to move on with the button, could i implement an if statement and if a registered click hits the statement, it will break the entire while loop? Then eventually python will move onto the equations?

Again, thank you so much for all your help, I really do appreciate it!

Okay scratch that, i implemented an if statement inside the while loop, and it successfully breaks the loop when clicked.

Heres the code up to date...

from graphics import*
def main():

    win=GraphWin("",400, 400)
    win.setCoords(-10,-10,10,10)

    button=Rectangle(Point(7,-9),Point(10,-10))
    button.setFill("Red")
    button.draw(win)

    done=Text(Point(8.5,-9.5),"Done")
    done.setTextColor("Black")
    done.draw(win)
    

    point1=win.getMouse()
    x=point1.getX()
    y=point1.getY()

    while x<=7 and y>= -9:
        Cir=Circle(point1,.2)
        Cir.setFill("Yellow")
        Cir.draw(win)
        point1=win.getMouse()
        x=point1.getX()
        y=point1.getY()
        stored_points = []
        stored_points.append(point1)
        len(stored_points)
        print (stored_points)
        if point1 == button:
            break
    len(stored_points)
    print 'success!'

*ignore the print 'success!', that was my way of finding out if the if statement worked, successfully breaking the while loop.

tsk, tsk, what it says if you put print in line 29 in front of len like I suggested? How many times and when must current line 27 be executed?

Oh okay sorry, i'm noob at python bare with me!

Okay, hows this look then

from graphics import*
def main():

    win=GraphWin("",400, 400)
    win.setCoords(-10,-10,10,10)

    button=Rectangle(Point(7,-9),Point(10,-10))
    button.setFill("Red")
    button.draw(win)

    done=Text(Point(8.5,-9.5),"Done")
    done.setTextColor("Black")
    done.draw(win)
    

    point1=win.getMouse()
    x=point1.getX()
    y=point1.getY()
    stored_points = []
    while x<=7 and y>= -9:
        Cir=Circle(point1,.2)
        Cir.setFill("Yellow")
        Cir.draw(win)
        point1=win.getMouse()
        x=point1.getX()
        y=point1.getY()
        stored_points.append(point1)
        print (stored_points)
        len(stored_points)
        if point1 == button:
            break
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.