回归方程不能正确计算线

Regression equation not correctly calculating line

我正在使用 Zelle 的 graphics.py 程序 (http://mcsp.wartburg.edu/zelle/python/graphics.py) 创建一个简单的回归图表。我已经检查了我的函数,它们 return 它们应该做什么,所以它一定是我的回归计算本身的一些错误。我正在使用这个公式进行一些小的调整来说明我的 window 尺寸:

这是我的代码:

"""
This program takes points from the user, and then plots a regression line
with the 'best fit' from the collection of points.
"""
import math
from graphics import *


def listMean(list1):
    mean = sum(list1)/len(list1)
    print('Sum of list is', sum(list1), 'Number of point is', len(list1))
    return(mean)


def squareList(list1):
    squaredList = [x**2 for x in list1]
    print('List 1 is', list1, 'SquaredList is', squaredList)
    listSumSquared = sum(squaredList)
    print('The sum of the x-values squared is {}.'.format(listSumSquared))
    return listSumSquared


def xAndY(list1, list2):
    xAndYCombined = map(lambda x,y: x*y, list1, list2)
    print('List 1 and 2: ', list1, list2, 'Combined: ', xAndYCombined)
    xAndYSum = sum(xAndYCombined)
    print(xAndYSum)
    return xAndYSum


def main():
    win = GraphWin('Regression Line', 500, 500)
    win.setCoords(-10, -10, 10, 10)
    win.setBackground('black')

    # create done button
    done_button = Rectangle(Point(-9, -9), Point(-7, -8))
    done_button.setFill('White')
    done_button.draw(win)
    done = Text(Point(-8, -8.5), 'Done')
    done.setFill('black')
    done.draw(win)

    # create lists to hold y and x values
    y_vals = []
    x_vals = []

    while True:
        usr_click = win.getMouse()
        usr_click.setFill('white')
        usr_click.draw(win)
        x_val = usr_click.getX()
        y_val = usr_click.getY()
        if (x_val >= -9 and x_val <= -7) and (y_val >= -9 and y_val <= -8):
            break
        else:
            x_vals.append(x_val)
            y_vals.append(y_val)

    xBar = listMean(x_vals)
    yBar = listMean(y_vals)
    x2Sum = squareList(x_vals)
    xySum = xAndY(x_vals, y_vals)
    slope = ((xySum - (len(x_vals) * xBar * yBar)) / (x2Sum - (len(x_vals) * x2Sum)))
    y1 = yBar + (slope * (-10 - xBar))
    y2 = yBar + (slope * (10 - xBar))

    reg_line = Line(Point(-10, y1), Point(10, y2))
    reg_line.setFill('white')
    reg_line.draw(win)

    win.getMouse()
    win.close()

if __name__ == '__main__':
    main()
slope = ((xySum - (len(x_vals) * xBar * yBar)) / (x2Sum - (len(x_vals) * x2Sum)))

应该是

slope = (xySum - len(x_vals) * xBar * yBar) / (x2Sum - len(x_vals) * (xBar)**2)

分母中的第二项不对,可能是因为您犯了混淆 sum(x**2) 和 (sum(x))**2

的常见错误