Python初学者 - 如何将回归线与点击等同并以图形方式显示?

TyS*_*Shi 7 python zelle-graphics

我正在阅读John Zelle撰写的Python Programming,我被困在下图所示的练习中.

您可以在下面查看我的代码.我知道代码非常难看.(任何提示都表示赞赏)

回归练习的图片

到目前为止,这是我的代码:

from graphics import *

def regression():

# creating the window for the regression line
        win = GraphWin("Regression Line - Start Clicking!", 500, 500)
        win.setCoords(0.0, 0.0, 10.0, 10.0)

        rect = Rectangle(Point(0.5, 0.1), Point(2.5, 2.1))
        rect.setFill("red")
        rect.draw(win)
        Text(rect.getCenter(), "Done").draw(win)

        message = Text(Point(5, 0.5), "Click in this screen")
        message.draw(win)

        points = [] # list of points
        n = 0 # count variable
        sumX = 0
        sumY = 0

        while True:
                p = win.getMouse()
                p.draw(win)

# if user clicks in a red square it exits the loop and calculates the regression line
                if (p.getX() >= 0.5 and p.getX() <= 2.5) and (p.getY() >= 0.1 and p.getY() <= 2.1):
                        break

                n += 1 # count of the points

# get the sum of the X and Y points
                sumX = sumX + p.getX()
                sumY = sumY + p.getY()

# tuple of the X and Y points
                dot = (p.getX(), p.getY())
                points.append(dot)

        avgX = sumX / n
        avgY = sumY / n

        top = 0
        bottom = 0

# my ugly attempt at the regression equation shown in the book

        for i in points:
                gp = 0
                numer = points[gp][0] * points[gp][1]
                top = top + numer

                denom = points[gp][0] ** 2
                bottom = bottom + denom
                gp += 1

        m = (top - sumX * sumY) / (bottom - sumX ** 2)

        y1 = avgY + m * (0.0 - avgX)
        y2 = avgY + m * (10.0 - avgX)

        regressionline = Line(Point(0, y1), Point(10.0, y2))
        regressionline.draw(win)

        raw_input("Press <Enter> to quit.")
        win.close()

regression()
Run Code Online (Sandbox Code Playgroud)

当我运行程序时,回归线似乎永远不是最合适的真实线.我相信我在代码中错误地解释了回归方程.需要改变什么来获得正确的回归线?

Hug*_*ell 4

问题:

  • from my_library import *应该避免; 最好准确地指定您想要从中得到什么。这有助于保持您的命名空间整洁。

  • 你有一大堆代码;最好将其拆分为单独的功能。这使得思考和调试变得更加容易,并且可以帮助您以后重用代码。当然,这是一个玩具问题,你不会重复使用它 - 但做练习的重点是养成良好的习惯,以这种方式分解你的代码绝对是一个好习惯!一般经验法则 - 如果一个函数包含超过十几行代码,您应该考虑进一步拆分它。

  • 该练习要求您在获取输入点的同时跟踪 x、y、xx 和 xy 的运行总和。我认为这是一个坏主意 - 或者至少更像是 C 风格而不是 Python 风格 - 因为它迫使你同时执行两项不同的任务(获得分数并对其进行数学计算)。我的建议是:如果你得到积分,就得到积分;如果你得到积分,就得到积分;如果你得到积分,就得到积分。如果你正在做数学,就做数学;不要尝试同时做这两件事。

  • 同样,我不喜欢回归计算担心窗口两侧的位置的方式。为什么它应该了解或关心Windows?我希望你喜欢我的解决方案;-)

这是我对您的代码的重构版本:

from graphics import GraphWin, Point, Line, Rectangle, Text

def draw_window()
    # create canvas
    win = GraphWin("Regression Line - Start Clicking!", 500, 500)
    win.setCoords(0., 0., 10., 10.)
    # exit button
    rect = Rectangle(Point(0.5, 0.1), Point(2.5, 2.1))
    rect.setFill("red")
    rect.draw(win)
    Text(rect.getCenter(), "Done").draw(win)
    # instructions
    Text(Point(5., 0.5), "Click in this screen").draw(win)
    return win

def get_points(win):
    points = []
    while True:
        p = win.getMouse()
        p.draw(win)
        # clicked the exit button?
        px, py = p.getX(), p.getY()
        if 0.5 <= px <= 2.5 and 0.1 <= py <= 2.1:
            break
        else:
            points.append((px,py))
    return points

def do_regression(points):
    num = len(points)
    x_sum, y_sum, xx_sum, xy_sum = 0., 0., 0., 0.
    for x,y in points:
        x_sum += x
        y_sum += y
        xx_sum += x*x
        xy_sum += x*y
    x_mean, y_mean = x_sum/num, y_sum/num
    m = (xy_sum - num*x_mean*y_mean) / (xx_sum - num*x_mean*x_mean)
    def lineFn(xval):
        return y_mean + m*(xval - x_mean)
    return lineFn

def main():
    # set up
    win = draw_window()
    points = get_points(win)
    # show regression line
    lineFn = do_regression(points)
    Line(
        Point(0.,  lineFn(0. )),
        Point(10., lineFn(10.))
    ).draw(win)
    # wait to close
    Text(Point(5., 5.), "Click to exit").draw(win)
    win.getMouse()
    win.close()

if __name__=="__main__":
    main()
Run Code Online (Sandbox Code Playgroud)